• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xml to string to calling a method from another class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Mark Foresman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic