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!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Split those bits up and test it.
The class defined by "settingsClass" does not extend or implement "Settings"


System.out.println( settingsClass ) ;

before you do the Class.forName

I reckon it's "null"
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.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

The three musketeers strike again  :)
>>I reckon it's "null"

So do I
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?
package com.goals ;

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

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

else {
    log.fatal( o + " IS NOT A SETTINGS OBJECT!" ) ;
ericworldzAuthor Commented:
It is implementing Settings
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?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>>does "Site" have an empty constructor?

Must have, or couldn't be non-null ;-) (according to eric)
Please, for the benefit of others, explain what the solution to the problem was eric
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.