Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName

 
Naveen R R
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could anyone help me with this?
It is giving an error:-
Type mismatch: cannot convert from java.lang.Class to navigation.WorkerBeanImpl
public String processRequest(javax.servlet.http.HttpServletRequest request, java.sql.Connection connection) throws Exception {
WorkerBeanImpl wbImpl = null;
try {
for (int row=0; row < wbInfo.getRowCount(); row++) {
NavigationDataRow navRow = wbInfo.getRow(row);
wbImpl = Class.forName(navRow.getWorkerBean()); //ERROR
-------
-------
Thanks,
Naveen
 
Aleksey Matiychenko
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName() creates an instance of "Class" class.
To create a new instance of your object you have to call newInstance() method
 
Naveen R R
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this too:
wbImpl = Class.forName(navRow.getWorkerBean()).newInstance();
Now, it gives the following error:-
Type mismatch: cannot convert from java.lang.Object to navigation.WorkerBeanImpl
Thanks,
Naveen
 
Robert Brunner
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The newInstance method of Class returns an Object, you will need to cast this to your desired class.
wbImpl = (WorkerBeanImpl)Class.forName(navRow.getWorkerBean()).newInstance();
See:
http://www.javasoft.com/j2se/1.3/docs/api/java/lang/Class.html#newInstance()
 
Trevor Dunn
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naveen R R:
[B]Hi,
Could anyone help me with this?
It is giving an error:-
Type mismatch: cannot convert from java.lang.Class to navigation.WorkerBeanImpl
public String processRequestwbImpl = Class.forName(navRow.getWorkerBean()); //ERROR
-------
Class.forName takes a fully qualified paackage name as a String for an argument and returns the Class Object representing that name. If getWorkerBean is not doing this then you will get an error
Trevor
 
Naveen R R
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. This works fine:
wbImpl = (WorkerBeanImpl)Class.forName(navRow.getWorkerBean()).newInstance();
Naveen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic