473,465 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reflection passing arrays of primitives as argument

Hi All, I am trying to create a BeanHelper class which will set a beans
values from the request much the same as it would be form a jsp using
'usebean'.

I can get everything to work except for methods which take arrays of
primitives. If my bean method takes an array of primitves I get a
"java.lang.IllegalArgumentException: argument type mismatch" when
method.invoke(bean, args) is called ,where 'bean' is my bean to set & 'args'
is an array of Objects.

Here is a snippet from the code which creates an Object for the args array
which works with Objects & primitives

private static Object getArgument(String parameter, Class paramType) {
Object returnObject = null;
if (paramType.equals(Boolean.class) ||
paramType.equals(boolean.class)) {
System.out.println("returning Boolean");
returnObject = new Boolean(parameter);
}
return returnObject;
}

here's some code from the method which creates Object arrays if the bean
method takes an array. This works if the method argument takes Integer[],
but not if it takes int[].

private static Object[] getArrayArgument(String[] parameters, Class
paramType) {
Object[] returnObject = null;
if (paramType.equals(Integer[].class) ||
paramType.equals(int[].class)) {
System.out.println("returning Integer[]");
Integer[] intArray = new Integer[parameters.length];
for (int i = 0; i < parameters.length; i++) {
intArray[i] = new Integer(parameters[i]);
}
returnObject = intArray;
}
return returnObject;
}
Any help greatly appreciated :-)
--
-P
Jul 17 '05 #1
1 5415
Worked it out :-)
Object returnObject = null;
if (paramType.equals(int[].class)) {
System.out.println("returning int[]");
int[] intArray = new int[parameters.length];
for (int i = 0; i < parameters.length; i++) {
intArray[i] = Integer.parseInt(parameters[i]);
}
returnObject = intArray;
}
--
-P
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mark | last post by:
hello! I have a class method that takes a variable number of parameters and correctly deals with them with func_get_args, etc ... i.e. class ABC { public function MooSaysTheCow()
5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
2
by: aliensite | last post by:
I am trying to pass arrays of various lengths to a function. However the values are read as a string. What is the easiest way to pass the values? <script type="text/javascript"> <!-- function...
1
by: Kurt Richardson | last post by:
Hi all Sorry to bother you with what is probably a really trivial question for you C++ experts. My programming skill are pretty amateur, but I'm pretty good at VB.NET. However, I'm wanting to...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
7
by: Rik | last post by:
Hi, I have an Multithreaded application in VB.NET. I inherited a Sub Class and which define an Array variable of string in that Sub Class. Now I want to Carry that Sub Class Array back to...
1
by: Eric Capps | last post by:
This may be more of a Java question, but I feel that JavaScript experts may be more qualified to help me find a solution. In short: is it possible to call a Java method from JavaScript, passing...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
1
by: Fizzics | last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.