ClassCastException Error

I get the ClassCastException for the line below:

String settingsClass = config.getInitParameter("SettingsClass");
                  
Settings settings = (Settings) Class.forName(settingsClass).newInstance(); //<-- this is where the exception error occurs

Thanks for the help!
ericworldzAsked:
Who is Participating?
 
TimYatesCommented:
can you post the code for the Site class?

It's either in the wrong package, or implementing the wrong settings class or something...

Do you only have one Settings class/interface?

try putting the full classname in the cast:

com.goals.Settings settings = (com.goals.Settings)Class.forName( settingsClass ).newInstance() ;

does "Site" have an empty constructor?
0
 
CEHJCommented:
Split those bits up and test it.
0
 
TimYatesCommented:
The class defined by "settingsClass" does not extend or implement "Settings"

Try

System.out.println( settingsClass ) ;

before you do the Class.forName

I reckon it's "null"
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
imladrisCommented:
The nature of the exception indicates that the forName method is picking up a class, then newInstance is creating an instance of it, but it isn't a class of type Settings, so when it tries to cast it to a class of that type, it throws an exception.

I'm guessing that you are expecting it to be a class of type Settings. If so, perhaps you could print out the string you are getting from the getInitParameter call (System.out.println("class name: "+settingsClass);), and post the code (at least the class definition and the constructor) of the class you are trying to load.
0
 
imladrisCommented:
The three musketeers strike again  :)
0
 
TimYatesCommented:
:-D
0
 
CEHJCommented:
>>I reckon it's "null"

So do I
0
 
ericworldzAuthor Commented:
I try to print out the settingsClass and it's not null.
It returns settingsClass=com.goals.Site, so I guess when I try to cast it to Settings it gives me the ClassExceptionError.

Below is the code for the Settings class:
public interface Settings {
public Site getSite(Hashtable initParams) throws Exception;      
}

So what is the way to get around this?
Thanks!
0
 
TimYatesCommented:
package com.goals ;

public class Site implements Settings
0
 
TimYatesCommented:
make sure it implements "Settings" :-)
0
 
CEHJCommented:
T's always a good idea to type-check anyway

Object o = Class.forName(settingsClass).newInstance();
if (o instanceof Settings) {
     Settings s = (Settings)o;
}
0
 
TimYatesCommented:

else {
    log.fatal( o + " IS NOT A SETTINGS OBJECT!" ) ;
}
0
 
ericworldzAuthor Commented:
It is implementing Settings
0
 
CEHJCommented:
>>does "Site" have an empty constructor?

Must have, or couldn't be non-null ;-) (according to eric)
0
 
CEHJCommented:
Please, for the benefit of others, explain what the solution to the problem was eric
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.