• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need to resolve ClassCastException

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
Please find the below code.it is self explanatory but still wanted to ask why am i getting ClassCastException.
For information -->Code is ran on JDK1.5 update 17 environment.



Help provided will be highly appreciated.
Please help me Ranchers.



--
Deepak Lal
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Edit]Cleared Away my post based on EFH's suggestion[/Edit]
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please help me with this.
how should i override the toString() method ???
sample code provided will be helpful for guiding me..
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should know how to override the toString() method: it's easy. All you do is create a String which describes your object, then return it. I suspect you have actually achieved that bit.

But that is probably not the true problem. The toString() method always returns something, but if you put that into a Map, which is what a resources bundle object probably is, you either get the right object or null. If you had null you wouldn't get a ClassCastException, but a NullPointerException when you try to use the object returned. If you go through the ResourceBundle API it doesn't say anything about ClassCastExceptions, but it mentions other Exceptions.

You need to check that what you are returning really is a Frequency. Getting something different out of your resources bundle is the most likely cause for such an Exception.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Campbell says (in his overly nice way) the suggestion about fixing toString() is complete rubbish - ignore it.

You don't show a class that extends ResourceBundle here, or any code that creates a ListResourceBundle. If your bundle is being created by loading a properties file directly, then it will be a PropertyResourceBundle, and contain Strings only. In fact, you display as much at one point in your debug messages:

# resources bundle is ::java.util.PropertyResourceBundle@7d772e

Did you make special arrangements, somewhere, to create a ResourceBundle which actually contains Frequency objects? Where is that code? You can't use a cast to turn a String into a Frequency!
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

My Comments: The problem is not with the toString() Method as campbell has poited out and as i have understood from the below api.



Java 1.5 api for ResourceBundle

getObject
public final Object getObject(String key)

Parameters:
key - the key for the desired object
Returns:
the object for the given key
Throws:
NullPointerException - if key is null
MissingResourceException - if no object for the given key can be found



Yes you are absolutely correct campbell.It returns an Object,now my requirement is to type cast this Object into a type of Frequency class
Like below statement


so my question is that in my further class(NotesProducer.java) i have to pass of type only Frequency and not of type Object.so how do i achieve it.??? how do i type cast an Object back to Frequency type...is this not possible.???


@Ernest Friedman-Hill ---->
I have pasted the entire code for ResourceBundle.Please see it again.I have not missed it.i have imported java.util.ResourceBundle and used it in the below fashion


Hope it is clarified that the problem is not with resourceBundle.i have checked it.

Please help me Ranchers.




--
Deepak Lal
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the line throwing the Exception to this lot

That will print the type of Object you are getting back, then wait 0.5 seconds to make sure the printing and Exception don't occur at the same time. If what you get is Frequency or a subclass of Frequency, then the error is elsewhere. If it isn't a Frequency object, then you have found the error.

You may be able to extends the ResourceBundle class (as EFH has hinted) with type parameters. Then you can have a DeepaksResourceBundle<String, Frequency> class.

And I'm sorry for being overly nice, Ernest! Like most people in this country I am stressed because I have to get all my figures for tax onto the Tax website by tomorrow.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@Ernest Friedman-Hill ---->
I have pasted the entire code for ResourceBundle.Please see it again.I have not missed it.i have imported java.util.ResourceBundle and used it in the below fashion
view plaincopy to clipboardprint?
resources = ResourceBundle.getBundle("Interpretor.MusicalNotes");


Hope it is clarified that the problem is not with resourceBundle.i have checked it.


The very valid point that EFH was making is that the line of code you are using to load a ResourceBundle is returning a PropertyResourceBundle. This is a resource bundle backed by a text file with key-value pairs of strings. If you have your own type of resource bundle that returns Frequency objects then it isn't being found. The API docs say:

For each candidate bundle name, it attempts to create a resource bundle:
First, it attempts to load a class using the candidate bundle name. If such a class can be found and loaded using the specified class loader, is assignment compatible with ResourceBundle, is accessible from ResourceBundle, and can be instantiated, getBundle creates a new instance of this class and uses it as the result resource bundle.
Otherwise, getBundle attempts to locate a property resource file. It generates a path name from the candidate bundle name by replacing all "." characters with "/" and appending the string ".properties". It attempts to find a "resource" with this name using ClassLoader.getResource. (Note that a "resource" in the sense of getResource has nothing to do with the contents of a resource bundle, it is just a container of data, such as a file.) If it finds a "resource", it attempts to create a new PropertyResourceBundle instance from its contents. If successful, this instance becomes the result resource bundle.


You either need to change the name you are passing to getBundle() so that it matches your resource bundle class or create your own resource bundle class that returns Frequency objects.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell and Tony,
Could you please give me the syntax for extending to a resource Bundle.Since im new to generics i need some help on syntax.

How do i use a ResourceBundle in this case that has type Paramters.Sample Code provided will be helpful.

Thanks in Advance.

--
Deepak Lal
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know offhand, sorry. You can probably find useful details from the ResourceBundle class and its two subclasses inthe API. As Ernest said yesterday, you might be able to use the ListResourceBundle class without modification.

I think a trip to the Java Tutorials will help you learn generics: also google for Angelika Langer Java generics FAQ which is a very useful website.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:Hi Campbell and Tony,
Could you please give me the syntax for extending to a resource Bundle.Since im new to generics i need some help on syntax.



ResourceBundle is not generic, so you don't need to learn anything about generics to complete this task.

Here is the Javadoc for ListResourceBundle. To use it, you must define a subclass. The Javadoc page I've pointed to shows an explicit example of exactly what to do.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:Could you please give me the syntax for extending to a resource Bundle.Since im new to generics i need some help on syntax.


First of all forget about Generics, the problem has nothing to do with Generics and the ResourceBundle classes do not support Generics.

The problem is that you need to return a Frequency Object and given that a PropertyResourceBundle can only return a String you can't directly use this approach. Looking through the API you should see there is another type of resource bundle class called a ListResourceBundle that allows you to return any Object type. If you read the docs for this class it clearly explains how to use it. The drawback is all the ResourceBundle subclasses return type Object (this can be cast back to whatever the object type is) but you can't use generics to make it type safe.

The main use of the ResourceBundle subclasses is to get the automatic lookup of the most locale specific variation of the data. That is, it's great for when need to supply different data for different locales, if you only ever have one set of data then this isn't necessarily the best way to go and surely frequencies for specific notes are the same no matter where you are in the world.

Another valid use for the PropertyResourceBundle has been to allow data to be extracted from the code and placed into a text file, although generally this is done using XML files these days. If this was your intention then you can stick with what you already have but you need to convert the String value to a Frequency object. You could do this several ways such as having a constructor in the Frequency class that takes a String or convert the String to a numeric value ( eg Integer.parseInt(myString) ) and construct a Frequency object from that.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can you please tell me how to achieve it,i have gone through the API for List Resource Bundle.

In List Resource Bundle API ,There is no method to obtain a handle to properties file.so if i dont get a handle to properties file how can i proceed further.?

Can you please modify the below code using ListResourceBundle and help me out.





--
Deepak Lal



 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Map<String, Frequency> might be much easier.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,Im Struggling to solve this issue since yesterday,Could you please help me out.
i want to achieve this using a Resource Bundle.

--
Deepak Lal
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid I don't know more than you about ResourceBundles. Read carefully what Ernest and Tony have written.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and we've reached the stage where the discussion is too difficult for beginners. Moving.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:Hi,
Can you please tell me how to achieve it,i have gone through the API for List Resource Bundle.

In List Resource Bundle API ,There is no method to obtain a handle to properties file.


That's because a ListResourceBundle isn't backed by a properties file. If you want to use a propeties file then you have to use a different approach. This maybe the other approach I outlined, but without seeing more details (such as a section of the properties file) it's hard to give more specific help.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,
I have modified the code with respect to List Resource Bundle and im still encounteriing the same issue...Please see below

Code Written as suggested by you With respect to LIST RESOURCE BUNDLE CLASS..
Please see below.



Need Help...whats wrong with the code now.I have implemented using List Resource Bundle.

--
Deepak Lal

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:Hi Tony,
Code Written as suggested by you With respect to LIST RESOURCE BUNDLE CLASS..


Except you aren't using it as a ResourceBundle. You are not supposed to directly instantiate a ResourceBundle, as I said before the point of them is the most appropraite one for the locale will be instantiated when you call getBundle().

This isn't the problem with your code but it does negate the whole point of a ListResourceBundle.

Deepak Lal wrote:
Need Help...whats wrong with the code now.I have implemented using List Resource Bundle.


I'm not sure how you can even think it will work. All along we having been telling you you can't just cast a String to a Frequency, the reason for suggesting using a ListResourceBundle was so you could store your Frequency objects in the array (You've stored Strings). You need to change the contents array so it contains String keys and Frequency values. I've not seen your Frequency class so I've no idea how it's constructed but assuming it takes an int parameter you need to do something like:

But, as I said before, if there aren't locale dependant values then a ListResourceBundle isn't a good way of solving your problem.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
A special Thanks to Ernest Friedman-Hill ,Campbell ritchie and Tony Tony Docherty for their valuable input and precious time for this CLASS CAST EXCEPTION POST.All in all i have learnt a lot from eack one of you and i want to thank all of you for sparing your valuable time and efforts to make me understand each and every post.

Thanks Once gain.
Its Accomplished.




--
Deepak Lal
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
It will give me the powers of the gods. Not bad for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic