Solved

ClassCastException Error

Posted on 2003-12-03
15
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

636 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