Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Runtime error when i convert/cast Object to Date

Posted on 2006-05-23
8
Medium Priority
?
1,546 Views
Last Modified: 2008-01-09


// I set the value from a dropdown box to txnFromDate
Object txnFromDate =  cboDateFrom.getSelectedItem();

// then I cast From Object to DAte
Date aFromDate  = (Date)txnFromDate;
Sytem.out.println(aFromDate);

// No error found while compile time but theres an error in runtime
0
Comment
Question by:mon_pacey8
6 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 80 total points
ID: 16740388
The object txnFromDate is not of type Date. It is probably a String. Try casting to String instead.
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 80 total points
ID: 16740497
Try to see what is it's class:
// then I cast From Object to DAte
System.out.prinrln(txnFromDate.getClass().getName());
Date aFromDate  = (Date)txnFromDate;

Bye, Giant.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16740526
Holding it as Date would be more flexible
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 24

Expert Comment

by:sciuriware
ID: 16740576
Run-time error is exactly as it says: error at runtime.

Of course the compiler could not know that at runtime
you have a non-Date-derived object in 'txnFromDate' which is totally legal (it's an Object-derived object!),
that you would put in 'aFromDate'.
Therefore the      (Date)       is translated by javac into runtime checks to make sure
txnFromDate contains a reference to an object extended from Date.

;JOOP!
0
 
LVL 1

Author Comment

by:mon_pacey8
ID: 16748512
tnx for ur comments. My problem been solved using

Date dFromDate = new Date(oFromDate.toString());

tnx a lot.....
0
 
LVL 12

Expert Comment

by:Giant2
ID: 16749107
Warning!
new Date(String) is deprecated:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#Date(java.lang.String)
...
As of JDK version 1.1, replaced by DateFormat.parse(String s)

Bye, Giant.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question