Solved

ClassCastException Error

Posted on 2003-12-03
15
300 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

734 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