• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Class Cast Exception

 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange. Did you check to see if what you are trying to cast is always a Contractor? Like this:
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good idea. Thanks!
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic