• Post Reply Bookmark Topic Watch Topic
  • New Topic

IllegalArgumentException.. while using Reflection. please help  RSS feed

 
Pavan Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I was trying to set values read from a properties file to java-bean properties using reflection. and when I have primitive data types I'm experiencing this problem. Please suggest what I should be doing or what I'm doing wrong.
Thanks in advance.
cnu
Class Address address = Class.forName("de.freenet.oms.entity.Address");
Address obj = (Address)address.newInstance();
Field fields[] = address.getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
Field field = fields[i];
field.setAccessible(true);
System.out.println("Field Name:"+field.getName());
if(field.getType()==long.class){field.set(obj,test.longValue(props.getProperty(field.getName())));
}elsefield.set(obj,props.getProperty(field.getName()).trim());
}
I have a convenience method
public Long longValue(Strring str){
return new Long(str.trim());
}

The Exception stacktrace:
--------------------------------------------------------------------------
java.lang.IllegalArgumentException
at sun.reflect.UnsafeLongFieldAccessorImpl.set(UnsafeLongFieldAccessorImpl.java:84)
at java.lang.reflect.Field.set(Field.java:519)
at de.freenet.oms.beanutils.UpdateBeanProperties.main(UpdateBeanProperties.java:244)
Exception in thread "main"
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Exception seems to be complaining about the usage of this method:
UnsafeLongFieldAccessorImpl
However, I don't see where in your code that's being used.
It sounds like you might be using a primitive value as a parameter to a method when it only takes Object parameters. Do you need to put your primitive value in an object wrapper before trying to call the method (e.g. put a long into a Long object)?
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cnu,
It's hard to tell what's going on because the code looks OK. You'll probably have to use a debugger or instrument the code to find out.
If you get tired of banging head your head against the wall, take a look at my response to your previous post on this same subject.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see what the problem is either. However it might be useful to double check this:
at de.freenet.oms.beanutils.UpdateBeanProperties.main(UpdateBeanProperties.java:244)
Exactly which line of your code is this? Look at line 244 of your code - we can't tell from here. It's one of the set() calls, it's probably the one where ou set a long value rather than a String - but I'd check it to be safe. Your code seems to assume that all fields are either long or String - if that's not the case, you will get an IllegalArgumentException. Which if course is what you're getting now, so maybe that's why.
Assuming the problem really is with the line

try replacing this with

This should be equivalent in effect, but who knows, maybe not; give it a try.
Can you show the class definition of the object whose fields you're trying to set? Just the field declarations will be enough, probably. Perhaps a field is listed as final? (I'm not sure what error that would give, but you probably can't set it.)
[ January 30, 2004: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!