reflection problem

X

xareon

hi all, i got a problem with this bunch of code:

Code:
Manager boss = new Manager("Paul Brown", 1000, 500);

Class c = boss.getClass();

try
{
Field field = c.getField("bonus");

Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus);

}
catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();
}
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();
}

i got a class Manager with a public (otherwise i'd get a
NoSuchFieldException) Integer bonus field. i'd like to modify it via
reflection, but i get an error at this point:

Integer newBonus = field.getInt(boss);

Code:
Exception in thread "main" java.lang.IllegalArgumentException:
Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus"
with illegal data type conversion to int
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown
Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)

illegal data type conversion to int? what does it mean? how can i fix
this? thank you all :)
 
D

Daniel Pitts

hi all, i got a problem with this bunch of code:

Code:
Manager boss = new Manager("Paul Brown", 1000, 500);

Class c = boss.getClass();

try
{
Field field = c.getField("bonus");

Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus);

}

catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();}

catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();}

i got a class Manager with a public (otherwise i'd get a
NoSuchFieldException) Integer bonus field. i'd like to modify it via
reflection, but i get an error at this point:

Integer newBonus = field.getInt(boss);

Code:
Exception in thread "main" java.lang.IllegalArgumentException:
Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus"
with illegal data type conversion to int
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown
Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)

illegal data type conversion to int? what does it mean? how can i fix
this? thank you all :)

getInt returns (int), where as your Bonus value is an Integer.

You can either change the type of your bonus to int, or you can use
Integer newBonus = (Integer)field.get(boss);


Although, I'm curious why you wish to use Reflection. If your project
is simply to learn more about reflection, thats fine, but usually
using reflection for this sort of thing is a Bad Idea (TM). Perhaps
if you shared your goal with the group, we could suggest a better way.
 
X

xareon

hi all, i got a problem with this bunch of code:
Code:
Manager boss = new Manager("Paul Brown", 1000, 500);[/QUOTE]
[QUOTE]
Class c = boss.getClass();[/QUOTE]
[QUOTE]
try
{
Field field = c.getField("bonus");[/QUOTE]
[QUOTE]
Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus); 

catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();}[/QUOTE]
[QUOTE]
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();} [QUOTE]

i got a class Manager with a public (otherwise i'd get a
NoSuchFieldException) Integer bonus field. i'd like to modify it via
reflection, but i get an error at this point:
Integer newBonus = field.getInt(boss);
Code:
Exception in thread "main" java.lang.IllegalArgumentException:
Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus"
with illegal data type conversion to int
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown
Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)
illegal data type conversion to int? what does it mean? how can i fix
this? thank you all :)

getInt returns (int), where as your Bonus value is an Integer.

You can either change the type of your bonus to int, or you can use
Integer newBonus = (Integer)field.get(boss);

Although, I'm curious why you wish to use Reflection. If your project
is simply to learn more about reflection, thats fine, but usually
using reflection for this sort of thing is a Bad Idea (TM). Perhaps
if you shared your goal with the group, we could suggest a better way.[/QUOTE]

thank you man. although it sounds me funky to get an error with the
int/Integer assignaments (wrapper classes shouldn't give such
problems), i've tried your solution, and this one too:

int newBonus = f.getInt(boss);

but i keep on getting the same error at the same line. and finally
yes, my project is simply to learn more about reflection. i know i
could have simply done this via standard set/get methods :)
plus, to allow a pure OO programming, i'd avoid to change the type of
bonus from Integer to int :)
 
X

xareon

hi all, i got a problem with this bunch of code:
Code:
Manager boss = new Manager("Paul Brown", 1000, 500);[/QUOTE]
[QUOTE]
Class c = boss.getClass();[/QUOTE]
[QUOTE]
try
{
Field field = c.getField("bonus");[/QUOTE]
[QUOTE]
Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus); 

catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();}[/QUOTE]
[QUOTE]
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();} [QUOTE]

i got a class Manager with a public (otherwise i'd get a
NoSuchFieldException) Integer bonus field. i'd like to modify it via
reflection, but i get an error at this point:
Integer newBonus = field.getInt(boss);
Code:
Exception in thread "main" java.lang.IllegalArgumentException:
Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus"
with illegal data type conversion to int
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown
Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)
illegal data type conversion to int? what does it mean? how can i fix
this? thank you all :)

getInt returns (int), where as your Bonus value is an Integer.

You can either change the type of your bonus to int, or you can use
Integer newBonus = (Integer)field.get(boss);

Although, I'm curious why you wish to use Reflection. If your project
is simply to learn more about reflection, thats fine, but usually
using reflection for this sort of thing is a Bad Idea (TM). Perhaps
if you shared your goal with the group, we could suggest a better way.[/QUOTE]

thank you man. although it sounds me funky to get an error with the
int/Integer assignaments (wrapper classes shouldn't give such
problems), i've tried your solution, and this one too:

int newBonus = f.getInt(boss);

but i keep on getting the same error at the same line. and finally
yes, my project is simply to learn more about reflection. i know i
could have simply done this via standard set/get methods :)
plus, to allow a pure OO programming, i'd avoid to change the type of
bonus from Integer to int :)
 
X

xareon

On Jul 25, 7:33 am, (e-mail address removed) wrote:
hi all, i got a problem with this bunch of code:
Code:
Manager boss = new Manager("Paul Brown", 1000, 500); 
Class c = boss.getClass(); 
try
{
Field field = c.getField("bonus"); 
Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus); 
} 
catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();} 
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();}
i got a class Manager with a public (otherwise i'd get a
NoSuchFieldException) Integer bonus field. i'd like to modify it via
reflection, but i get an error at this point:
Integer newBonus = field.getInt(boss);
Code:
Exception in thread "main" java.lang.IllegalArgumentException:
Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus"
with illegal data type conversion to int
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown
Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)
illegal data type conversion to int? what does it mean? how can i fix
this? thank you all :)
getInt returns (int), where as your Bonus value is an Integer.
You can either change the type of your bonus to int, or you can use
Integer newBonus = (Integer)field.get(boss);
Although, I'm curious why you wish to use Reflection. If your project
is simply to learn more about reflection, thats fine, but usually
using reflection for this sort of thing is a Bad Idea (TM). Perhaps
if you shared your goal with the group, we could suggest a better way.

thank you man. although it sounds me funky to get an error with the
int/Integer assignaments (wrapper classes shouldn't give such
problems), i've tried your solution, and this one too:

int newBonus = f.getInt(boss);

but i keep on getting the same error at the same line. and finally
yes, my project is simply to learn more about reflection. i know i
could have simply done this via standard set/get methods :)
plus, to allow a pure OO programming, i'd avoid to change the type of
bonus from Integer to int :)


ty, i found the problem: it was just that getInt() method returns an
int, while my field was an Integer. i should have used

Integer newBonus = (Integer)f.get(boss);

get returns an object, that i just cast to Integer. sometimes i feel
so stupid :x
 
X

xareon

On Jul 25, 7:33 am, (e-mail address removed) wrote:
hi all, i got a problem with this bunch of code:
Code:
Manager boss = new Manager("Paul Brown", 1000, 500); 
Class c = boss.getClass(); 
try
{
Field field = c.getField("bonus"); 
Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus); 
} 
catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();} 
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();}
i got a class Manager with a public (otherwise i'd get a
NoSuchFieldException) Integer bonus field. i'd like to modify it via
reflection, but i get an error at this point:
Integer newBonus = field.getInt(boss);
Code:
Exception in thread "main" java.lang.IllegalArgumentException:
Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus"
with illegal data type conversion to int
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown
Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)
illegal data type conversion to int? what does it mean? how can i fix
this? thank you all :)
getInt returns (int), where as your Bonus value is an Integer.
You can either change the type of your bonus to int, or you can use
Integer newBonus = (Integer)field.get(boss);
Although, I'm curious why you wish to use Reflection. If your project
is simply to learn more about reflection, thats fine, but usually
using reflection for this sort of thing is a Bad Idea (TM). Perhaps
if you shared your goal with the group, we could suggest a better way.

thank you man. although it sounds me funky to get an error with the
int/Integer assignaments (wrapper classes shouldn't give such
problems), i've tried your solution, and this one too:

int newBonus = f.getInt(boss);

but i keep on getting the same error at the same line. and finally
yes, my project is simply to learn more about reflection. i know i
could have simply done this via standard set/get methods :)
plus, to allow a pure OO programming, i'd avoid to change the type of
bonus from Integer to int :)


ty, i found the problem: it was just that getInt() method returns an
int, while my field was an Integer. i should have used

Integer newBonus = (Integer)f.get(boss);

get returns an object, that i just cast to Integer. sometimes i feel
so stupid :x
 
X

xareon

On Jul 25, 7:33 am, (e-mail address removed) wrote:
hi all, i got a problem with this bunch of code:
Code:
Manager boss = new Manager("Paul Brown", 1000, 500); 
Class c = boss.getClass(); 
try
{
Field field = c.getField("bonus"); 
Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus); 
} 
catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();} 
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();}
i got a class Manager with a public (otherwise i'd get a
NoSuchFieldException) Integer bonus field. i'd like to modify it via
reflection, but i get an error at this point:
Integer newBonus = field.getInt(boss);
Code:
Exception in thread "main" java.lang.IllegalArgumentException:
Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus"
with illegal data type conversion to int
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown
Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)
illegal data type conversion to int? what does it mean? how can i fix
this? thank you all :)
getInt returns (int), where as your Bonus value is an Integer.
You can either change the type of your bonus to int, or you can use
Integer newBonus = (Integer)field.get(boss);
Although, I'm curious why you wish to use Reflection. If your project
is simply to learn more about reflection, thats fine, but usually
using reflection for this sort of thing is a Bad Idea (TM). Perhaps
if you shared your goal with the group, we could suggest a better way.

thank you man. although it sounds me funky to get an error with the
int/Integer assignaments (wrapper classes shouldn't give such
problems), i've tried your solution, and this one too:

int newBonus = f.getInt(boss);

but i keep on getting the same error at the same line. and finally
yes, my project is simply to learn more about reflection. i know i
could have simply done this via standard set/get methods :)
plus, to allow a pure OO programming, i'd avoid to change the type of
bonus from Integer to int :)


ty, i found the problem: it was just that getInt() method returns an
int, while my field was an Integer. i should have used

Integer newBonus = (Integer)f.get(boss);

get returns an object, that i just cast to Integer. sometimes i feel
so stupid :x
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top