File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Class.forName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Class.forName" Watch "Class.forName" New topic
Author

Class.forName

Naveen R R
Greenhorn

Joined: May 04, 2001
Posts: 20
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

Joined: Apr 03, 2001
Posts: 178
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

Joined: May 04, 2001
Posts: 20
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

Joined: Jul 18, 2001
Posts: 49
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

Joined: Jun 13, 2001
Posts: 84
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

Joined: May 04, 2001
Posts: 20
Thank you very much. This works fine:
wbImpl = (WorkerBeanImpl)Class.forName(navRow.getWorkerBean()).newInstance();
Naveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.forName