Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

ClassCastException in the put() method of hashtable.

Thread view: 
geek - 21 Oct 2005 20:06 GMT
Hi all,
I am getting this error:

java.lang.ClassCastException: java.lang.Integer

for this snippet
======================================================
public Hashtable ReqHash = new Hashtable();
for(Iterator It = Queue.iterator();It.hasNext();)
       {
           Element =(Site)It.next();
           System.out.println("The element is "+Element.PId);
           System.out.println("The element is "+Element.TimeStamp);
           ReqHash.put(new Long(Element.TimeStamp),new
Integer(Element.PId));
       }
Can anybody please help me with this.

thanks in advance
HalcyonWild - 21 Oct 2005 20:34 GMT
> Hi all,
> I am getting this error:
[quoted text clipped - 12 lines]
> Integer(Element.PId));
>         }

What is the data type of PId.

Integer has two constructors only

Integer(int value)
Integer(String s)

Make sure Pid is either of int or String.
geek - 21 Oct 2005 20:38 GMT
PId is of type int.
geek - 21 Oct 2005 20:47 GMT
PId is of type int still it is giving this error.
I cannot understand what is going wrongs.
Please help.
Thanks.
HalcyonWild - 21 Oct 2005 20:58 GMT
> PId is of type int still it is giving this error.
> I cannot understand what is going wrongs.
> Please help.
> Thanks.

[QUOTE] #####

Hi all,
I am getting this error:

java.lang.ClassCastException: java.lang.Integer

for this snippet
======================================================
public Hashtable ReqHash = new Hashtable();
for(Iterator It = Queue.iterator();It.hasNext();)
       {
           Element =(Site)It.next();
           System.out.println("The element is "+Element.PId);
           System.out.println("The element is "+Element.TimeStamp);
           ReqHash.put(new Long(Element.TimeStamp),new
Integer(Element.PId));
       }
Can anybody please help me with this.

thanks in advance

[QUOTE END] ###

I tried out this piece of code.
public void tryHTInsert()
{
   Hashtable ReqHash = new Hashtable();
   int PId = 34;
   long timeStamp = 20051021334455000L;
   ReqHash.put(new Long(timeStamp),new Integer(PId));
   System.out.println(" hashtable contents-- "+ReqHash);
}

Nothing wrong with Hashtable

My output.
hashtable contents-- {20051021334455000=34}

More code please. Go through your Element class and check PId value.
geek - 21 Oct 2005 21:03 GMT
Thanks for the help.

Element is an object of class Site
import java.io.*;
import java.net.*;
import java.io.Serializable;
import java.util.*;

public class Site implements Serializable{
public int PId;
public long TimeStamp;
// public ArrayList Queue = new ArrayList();

public Site(int pid, long timestamp){
    PId = pid;
    TimeStamp = timestamp;
    System.out.println("My ProcessId is "+PId);
    System.out.println("My TimeStamp is "+TimeStamp);

}
}

class RequestQueue{
   public ArrayList Queue = new ArrayList();
   public Site SObj;
   public Site Element;
   public Hashtable ReqHash = new Hashtable();
   public TreeMap ReqHashSorted = null;
   public Site foo;
   public RequestQueue(Site sobj){
       SObj = sobj;
       /* Get the object and add it to the queue */
       Queue.add(SObj);
       for(Iterator It = Queue.iterator();It.hasNext();)
       {
           Element =(Site)It.next();
           System.out.println("The element is "+Element.PId);
           System.out.println("The element is "+Element.TimeStamp);
           ReqHash.put(new Long(Element.TimeStamp),new
Integer(Element.PId));
       }
       ReqHashSorted = new TreeMap(ReqHash);
       Iterator HashIt = ReqHashSorted.keySet().iterator();
       while(HashIt.hasNext())
       {
           foo = (Site)ReqHashSorted.get(HashIt.next());
           System.out.println("The smallest TimeStamp is "+foo.PId );
           System.out.println("The processid corres to smallest
TimeStamp is "+foo.TimeStamp );
       }
  }
   
   
}

Here are the two classes that I am working with.
Thanks.
Chris Smith - 21 Oct 2005 21:11 GMT
> Thanks for the help.
>
> Element is an object of class Site

Well, Element is a reference of type Site, anyway.  But that's
immaterial.  The problem is apparently that, somewhere along the line,
an object of class Integer is getting added to Queue.  When you get it
out and cast to class Site, the error occurs.  This hypothesis could be
confirmed by looking at the line numbers in the stack trace, but you
only posted the first line of the trace.

Incidentally, it would be easier if you would follow the standard Java
naming conventions by not capitalizing the names of fields.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

geek - 21 Oct 2005 21:19 GMT
>This hypothesis could be
>confirmed by looking at the line numbers in the stack trace, but you
>only posted the first line of the trace.

How do I see the complete stack trace.
Thats the only error line I am getting.
Chris Smith - 22 Oct 2005 02:53 GMT
> >This hypothesis could be
> >confirmed by looking at the line numbers in the stack trace, but you
> >only posted the first line of the trace.
>
> How do I see the complete stack trace.
> Thats the only error line I am getting.

Somewhere, there is code that's swallowing the stack trace then.  Try
wrapping the entire method that fails in this:

   try
   {
       ... original code here ...
   }
   catch (ClassCastException e)
   {
       e.printStackTrace();
       throw e;
   }

This is just a temporary change.  It will cause the full exception
message (including stack trace) to be printed to the standard error
stream (usually the console) when the problem occurs.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

HalcyonWild - 21 Oct 2005 22:42 GMT
> Thanks for the help.
>
[quoted text clipped - 28 lines]
>         SObj = sobj;
>         /* Get the object and add it to the queue */

The problem lies here it seems. You are passing Site sobj as a
parameter to the constructor. This sobj probably has some wrong values.

More code please. Who is calling your classes. Where is Site sobj being
initialized.

Try to give more meaningful names to your variables. Queue is also an
existing class in Java. Also , avoid capitalizing variable names.

>         Queue.add(SObj);
>         for(Iterator It = Queue.iterator();It.hasNext();)
[quoted text clipped - 20 lines]
> Here are the two classes that I am working with.
> Thanks.
geek - 21 Oct 2005 23:41 GMT
But the code is printing the correct value of foo.PId and foo.TimeStamp
so I
am assuming that it is getting right values.
Roedy Green - 22 Oct 2005 02:52 GMT
>But the code is printing the correct value of foo.PId and foo.TimeStamp
I think a "geek" is formally defined as a sideshow performer who
bites the heads off chickens to horrify.  Your flat refusal to follow
naming conventions has a similar effect.

see http://mindprod.com/codingconventions.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

geek - 22 Oct 2005 03:00 GMT
I am not refusing to follow naming convention.I am a newbie to java and
for the last message I just copied the fields from my code.

Thanks for help anyways.
Roedy Green - 22 Oct 2005 03:54 GMT
>I am not refusing to follow naming convention.I am a newbie to java and
>for the last message I just copied the fields from my code.

Chris pointed out the problem, but you ignored his advice.  Your code
still ignores the conventions. That to me sounds like a refusal.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

geek - 22 Oct 2005 04:31 GMT
I really dont have time right now to change all the variables in my
thousands of lines of code.
Will definitely keep in mind from my next project.Thanks.
Roedy Green - 22 Oct 2005 05:01 GMT
>I really dont have time right now to change all the variables in my
>thousands of lines of code.

if you were using an IDE this job is greatly simplified.  For example,
in Eclipse you use Refactor | rename and magically a variable or class
is universally renamed.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

HalcyonWild - 25 Oct 2005 21:55 GMT
> I really dont have time right now to change all the variables in my
> thousands of lines of code.
> Will definitely keep in mind from my next project.Thanks.

yeah, sure. take care next time about the variables.

but I am curious. What is the answer to your problem. What was the
reason for the exception.
geek - 07 Nov 2005 21:13 GMT
I haven't been able to figure out that one .
Noodles Jefferson - 08 Nov 2005 05:08 GMT
> I haven't been able to figure out that one .

ClassCastException means that whatever type you're casting to isn't the
type the variable needs.

For example

int c = (String) whatever.getSomething();

Mr. Compiler says "nuh-uh!" because c's an int and you're trying to cast
it as a string and assign it to c. Can't do that.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.