Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ClassCastException Error

Posted on 2003-12-03
15
Medium Priority
?
304 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

688 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