Solved

ClassCastException Error

Posted on 2003-12-03
15
293 Views
Last Modified: 2010-03-31
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!
0
Comment
Question by:ericworldz
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867264
Split those bits up and test it.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9867283
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
 
LVL 16

Expert Comment

by:imladris
ID: 9867285
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
 
LVL 16

Expert Comment

by:imladris
ID: 9867294
The three musketeers strike again  :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9867333
:-D
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867340
>>I reckon it's "null"

So do I
0
 

Author Comment

by:ericworldz
ID: 9867400
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:TimYates
ID: 9867432
package com.goals ;

public class Site implements Settings
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9867433
make sure it implements "Settings" :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867466
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9867479

else {
    log.fatal( o + " IS NOT A SETTINGS OBJECT!" ) ;
}
0
 

Author Comment

by:ericworldz
ID: 9867589
It is implementing Settings
0
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 9867621
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867656
>>does "Site" have an empty constructor?

Must have, or couldn't be non-null ;-) (according to eric)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867748
Please, for the benefit of others, explain what the solution to the problem was eric
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now