• Post Reply Bookmark Topic Watch Topic
  • New Topic

!Help! Error: Exception in thread "main" java.lang.ClassCastException  RSS feed

 
Alex Hays
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of the classes in my program but when I try and run it the console spits out:



The first thing works (Travelers, Boris Badenov 972-555-3003) and then it dies, and I cant for the life of me figure out why. Ive been altering stuff for hours .

Any help would be greatly appreciated <3333

Here is a link to code: http://alexhays.com/java
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the line 41 of the Traveler.java file (hint from the line : Traveler.compareTo(Traveler.java:41))

The look what it does. Probably a cast since it's a ClassCastException.
You are casting the wrong thing. (something like (Traveler)obj maybe?). So to know what you are supposed to cast try the obj.getClass().getName() and print this.
so :
compareTo takes a Object obj in parameter
so just before casting it into a Traveler
print obj.getClass().getName()

hope it's clear

Julien
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Julien points out, the error message tells you exactly WHERE the error occurs: line 41 in a file named Traveler.java in the method named compareTo(). If you post the code for this method, we will be glad to help you figure out WHY the error occurs. Be sure you indicate where line 41 is when you post your code.

Layne
 
Alex Hays
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
As Julien points out, the error message tells you exactly WHERE the error occurs: line 41 in a file named Traveler.java in the method named compareTo(). If you post the code for this method, we will be glad to help you figure out WHY the error occurs. Be sure you indicate where line 41 is when you post your code.

Layne


here is the code for the Traveler.java method:

htttp://www.alexhays.com/java - all the files are here

I have to run right now, but I will be back at 8 and try some stuff out!!

Thanks a lot guys, I would never have thought to try the above stuff mentioned.
 
Alex Hays
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Julien Grenier:
Look at the line 41 of the Traveler.java file (hint from the line : Traveler.compareTo(Traveler.java:41))

The look what it does. Probably a cast since it's a ClassCastException.
You are casting the wrong thing. (something like (Traveler)obj maybe?). So to know what you are supposed to cast try the obj.getClass().getName() and print this.
so :
compareTo takes a Object obj in parameter
so just before casting it into a Traveler
print obj.getClass().getName()

hope it's clear

Julien


This was what the console put out after adding that in the loop, just before everything:


://
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you noted, line 41 is

From your error message, it looks like you are trying to cast a String object to a Traveler object. In other words, on this line, o is really a String and you are trying to force it to be a Traveler, which is obviously incorrect. So where is this String coming from? To figure that out, you need to work backwards through the stack trace. That means the next place to look is given by

What is on line 69 of Reservation.java? Does this line give any clue to where the String comes from? If not, look at the next line in the stack trace.

You may also find it helpful to add System.out.println() calls to print out values of the variables that might cause this problem.

Let us know what you find out. If I have some time later tonight, I'll check out the URL that you gave for all of your code.

Layne
 
Alex Hays
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoaaa, on line 69 of travelers I was (for some reason) comparing traveler to .getReservationType, instead of .getTraveler ~

Thanks alot for the help and the advice to go down the stack to previous stuff as the answer lay there ~ thanks <333333333333333


Now my only problem is, the stuff isnt showing up at all, and the cost is messed up. Here is the output:



http://alexhays.com/java - updated

[ September 20, 2005: Message edited by: Alex Hays ]
[ September 20, 2005: Message edited by: Alex Hays ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now my only problem is, the stuff isnt showing up at all, and the cost is messed up.


What "stuff" isn't showing? You need to be more specific for us to be able to help you.

As for the cost, how is this value obtained? Is it calculated somewhere? Can you post the code that does this? We can try to help you from there.

Layne
 
Alex Hays
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:


What "stuff" isn't showing? You need to be more specific for us to be able to help you.

As for the cost, how is this value obtained? Is it calculated somewhere? Can you post the code that does this? We can try to help you from there.

Layne


Ahh, I pretty much figured it all out. I forgot to do the calculations, so I put them in and now it shows the actual cost, and the 'stuff' was the things like Arrival city, because I had super.toString and there wasnt a toString, I had to print out each method, like thus: + "Traveler:" + getTraveler() + "\n"
+ "Departure City" + getDepartureCity() + "\n"
+ "Arrival City" + getArrivalCity() + "\n"
+ "Travel Date" + getTravelDate() + "\n"
+ "Number of Days" + getNumDays() + "\n\n\n";


I have pretty much got this thing solved!!

Thanks for all your help guy +++++++
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!