This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xml to string to calling a method from another class

 
Mark Foresman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Mark Foresman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the Nike class in the shoes package ?
Have you compiled the Nike class ?
Is the Nike class in your classpath ?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic