my dog learned polymorphism*
The moose likes Java in General and the fly likes xml to string to calling a method from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "xml to string to calling a method from another class" Watch "xml to string to calling a method from another class" New topic
Author

xml to string to calling a method from another class

Mark Foresman
Greenhorn

Joined: Dec 15, 2011
Posts: 3
Ok i am taking an input from and xml doc which gets turning in to a string variable. now i am trying to turn that string into a method and i am getting a mess of errors.

This is just an example


create "hightops" which is an object of nike

so the xml input is hightops and color i need to call

hightops.getColor();

This is what i have so far



this is mainly from a few example on the internet and i am not sure how to go any farther

i am getting these errors

java.lang.IllegalArgumentException: type mismatch

&
java.lang.InstantiationException:



Thanks,

Mark
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
You can't use a string read in from an xml file as a variable name. iClass is a reference to your class instance. Use this to call your getColor (not hightops.getColor) method


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
And welcome to the Ranch
Mark Foresman
Greenhorn

Joined: Dec 15, 2011
Posts: 3
so your saying disregard the hightops

I ve tried the below code and i still get

java.lang.InstantiationException: shoes.Nike

String aClass;
String aMethod;

Class params[] = {};
Object paramsObj[] = {};

aClass = "shoes.Nike";
aMethod = "getColor";

Class thisClass;
try {
thisClass = Class.forName(aClass);
Object iClass;
iClass = thisClass.newInstance();
Method thisMethod;
thisMethod = thisClass.getDeclaredMethod(aMethod, params);
System.out.println(thisMethod.invoke(iClass, paramsObj));
}
catches .....
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Is the Nike class in the shoes package ?
Have you compiled the Nike class ?
Is the Nike class in your classpath ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Neither of these would lead to an java.lang.InstantiationException but instead a ClassNotFoundException. It's not the Class.forName where this exception is coming from but newInstance(). So Mark, can you show us all of the constructors of class Nike?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Good point. I just based the answer on the javadoc that said
InstantiationException - ... or if the instantiation fails for some other reason.
. Based on the code posted for the Nike class which showed no constructor, I just made a few suggestions of things to check without thinking too much about it. As the Nike class doesn't have a package statement either you are probably right that we are not being shown everything.
Mark Foresman
Greenhorn

Joined: Dec 15, 2011
Posts: 3
well i was told that using reflection for this purpose isnt the best thing so i am just going to keep it simple and create an arraylist of the method strings and inconjuction with a compare loop with the method i am looking for i can make a switch case statement and pull the method i am looking for.


ArrayList a = new ArrayList();

a.add("getColor");
a.add("getModel");

for(int i = 0; i > a.size(); i++)
aMethod.equals((String)a.get(i));
return index;

Switch(index)
case 0: getcolor(); break;
case 1: getModel(); break;

this is kinda what i did .. and it seems to work well thanks for yalls time.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xml to string to calling a method from another class