aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Class Cast Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Class Cast Exception" Watch "Class Cast Exception" New topic
Author

Class Cast Exception

Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Hi, I did a rough implementation of my book method in my DBDataAdapter class. Here is a snippet for the method:

I try calling the book method, but I get a Class Cast Exception when my lock() method calls my method isRecordValid();

Here is a snippet of my isRecordValid() method:


However, I'm getting a Class Cast Exception null at the line

Does anyone have any idea why that would be? Before using the book method, I had tested my read method and other methods that called isRecordValid, and I had no problems whatsoever...weird....Thanks!
[ December 02, 2004: Message edited by: Daniel Simpson ]

SCJP 1.4<br />SCJD 1.4
Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
What's your Contractor class? Is it the data structure you used to store in your records data structure? Is your temp defined as Contractor?


--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Andy- My contractor class stores the record number and data values of a specific record. I use a cache full of contractor objects.
[ December 03, 2004: Message edited by: Daniel Simpson ]
James Clinton
Ranch Hand

Joined: Jun 23, 2003
Posts: 190
Are you certain this is where the problem is occuring?

I'd advise wrapping that line either side with sysouts because it seems fine to me.
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Gosh, this is really frustrating me. I want to get this figured out. So I have a DBDataAdapter class that has a book method which hides my methods of my Data class. I've done a lot of testing and here is what I notice. When I don't instantiate an instance of my DBDataAdapter class, it runs fine. However, when I do, I get that Class Cast Exception. For easy testing purposes, I have my main method in my Data class. Here is the line of code for it...

This is in the main method in my Data class. Here is the framework for my DBDataAdapter class:

And I am getting that Class Cast Exception for some odd reason, which I don't know why. I even did many Sys.outs to check the size of my cache and it looked fine. Any ideas!?!?
[ December 05, 2004: Message edited by: Daniel Simpson ]
Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
Daniel: Does Java support nested functions or you have misplaced indention in your code? (are you new to multi-threading?)

You have a run() which implements runnable, that is fine. But you have your book method definition inside your run(): that is nested function. Try a function call in your runnable, that is the usuall way to start running a new thread. The logic in multiple threading programming is a bit different from single threading programming.

good luck
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Hey Andy, sorry, I only showed snippets of code, so it looks different than what it should be. My run method only calls the book method. My actual book method is outside of the run method. I updated my code snippet above to show you what it looks like.

I've run more tests today, and it seems like there are problems with multi-concurrent access. For instance, if I run one thread doing the book method, it will run fine. However, when I have two threads running, it seems like there is some weird problem with my cache. My cache is a static cache that every Data instance accesses. I did a bunch of Sys.outs at different points and here is what I got....


Does the cache need to be refreshed or something? Because I got that error (using no threads) when I tried to create a new record and then display the entire cache in a JTextArea. The create method worked fine, but it seems it wouldnt display it without throwing that error. I commented out the display and it ran fine, then i commented out the create and had the display, and it ran fine. However, when both were in my main method, it would mess up. Any ideas, anyone?
[ December 05, 2004: Message edited by: Daniel Simpson ]
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
Strange. Did you check to see if what you are trying to cast is always a Contractor? Like this:
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Okay, sweet, Matt. Now we are getting some where. Look what happens with my Sys.Outs:


I'm not quite sure why it is doing this. The first time I run my isRecordValid method, it says that the object at arraylist.get(recNo) is a Contractor. However, when I do subsequent calls, I get [Ljava.lang.String; And that is why it is messing up. I can't figure out why it is returning that though. There aren't any glitches when caching my records, nor reading them, however, subsequent calls mess that up. When i do arraylist.get(x), i'm positive it won't remove that element unless i called .remove(x). Any ideas? I know cached records makes things so much easier, but I am beginning to question whether I should screw the whole cache idea because these problems are starting to get annoying...We'll see...
[ December 06, 2004: Message edited by: Daniel Simpson ]
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
The only way this seems possible is if you are calling records.add(recNo, someString) in your lock or some other method after you have first retrieved the record. That would insert a String into that index and shift the records after it by 1.

Also, it might help if you would print the String and see what it says.
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Gosh, Matt, I feel like an idiot. I made an ID 10T Error. Here's what I did (boy am i stupid):



From your last post, it wasn't exactly what you said, but it opened my eyes to my costly error. Here is what it should be:


Thank you so much for all of your help in helping me figure out what was wrong. I really appreciate it.
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
Oh yeah. I forgot about the set method. Glad you got it figured out. By the way you may just want to update the values using some method in the Contractor class instead of creating a new one.
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Good idea. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Cast Exception