Solved

ClassCastException Error

Posted on 2003-12-03
15
296 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

786 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