473,427 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,427 software developers and data experts.

Why am I getting java.lang.ClassCastException ?

108 100+
Here are the codes I just compiled in java and I am getting a java.lang.ClassCastException ...


Expand|Select|Wrap|Line Numbers
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutput;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8. import java.util.Date;
  9.  
  10. public class test_serialisation implements Serializable{
  11.  
  12.     /**
  13.      * 
  14.      */
  15.     private static final long serialVersionUID = 1L;
  16.  
  17.     /**
  18.      * @param args
  19.      */
  20.     public static void main(String[] args) {
  21.         // TODO Auto-generated method stub
  22.         FileOutputStream f;
  23.         try {
  24.             f = new FileOutputStream("tmp");
  25.             ObjectOutput s = new ObjectOutputStream(f);
  26.             s.writeObject("Today");
  27.             s.writeObject(new Date());
  28.             s.flush();
  29.             FileInputStream in = new FileInputStream("tmp");
  30.             ObjectInputStream ss = new ObjectInputStream(in);
  31.  
  32.             Object dt=(Object)ss.readObject();
  33.             Date dtt=((Date) dt);
  34.  
  35.             System.out.println(dtt);
  36. } catch (Exception e) {
  37.             // TODO Auto-generated catch block
  38.             e.printStackTrace();
  39.         }
  40.  
  41.     }
  42.  
  43. }
  44.  
  45.  
and my printstack is
Expand|Select|Wrap|Line Numbers
  1. java.lang.ClassCastException: java.lang.String
  2.     at test_serialisation.main(test_serialisation.java:33)
  3.  
I cannot understand properly what it means since I am now learning java serialisation.. I awould appreciate any help..
Oct 9 '10 #1
1 6252
improvcornartist
303 Expert 100+
The full error is
Expand|Select|Wrap|Line Numbers
  1. java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
  2.         at test_serialisation.main(test_serialisation.java:33)
and occurs on
Expand|Select|Wrap|Line Numbers
  1. Date dtt=((Date) dt);
It doesn't like that you are trying to convert a string to a date.
Oct 12 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shawn | last post by:
I am getting the following error with a Java Applet being served out by IIS over HTTPS/SSL using a Verisign certificate: java.lang.NoClassDefFoundError: javax/help/HelpSetException at...
3
by: NathanK | last post by:
Hi Guys, im starting out in java and i'm getting the error: Exception in thread "main" java.lang.NoClassDefFoundError: blurf if i run "javac blurf.java", it compiles fine and without any...
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
1
by: kanthi84 | last post by:
hi, while i was trying to insert value into a blob column, i got an exception saying java.lang.ClassCastException: com.evermind.sql.OrclResultSet
0
by: gezkk | last post by:
hi, i m using http proxy service to get proxy and host to applet, its working fine with jdk1.5 but getting java.lang.NullPointerException in jdk1.6 And the following is mseeage contains java...
1
by: GayatriS | last post by:
Hi, I have written a jni program to call my vc++ dll. My java program is as below: //CODE import java.util.*; class callVcc { native int sum(int i, int j);
1
by: raagadeepthi | last post by:
Getting below error when iam trying to sort the values: Oct 29 04:33:52 WARN cb.DefaultController - Cannot forward after response has been committed java.lang.IllegalStateException: Cannot...
1
by: rashmiks | last post by:
Hi All, From our application we are making connection to a bank site with the API provided by them . For some reason in one of server we are getting error message ...
1
by: James Kalmadge | last post by:
I have a test program that compiles but does not run. Here is the code which I grabbed from from a web site: import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*;...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.