Runtime error when i convert/cast Object to Date

Posted on 2006-05-23
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;

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

    Accepted Solution

    The object txnFromDate is not of type Date. It is probably a String. Try casting to String instead.
    LVL 12

    Assisted Solution

    Try to see what is it's class:
    // then I cast From Object to DAte
    Date aFromDate  = (Date)txnFromDate;

    Bye, Giant.
    LVL 86

    Expert Comment

    Holding it as Date would be more flexible
    LVL 24

    Expert Comment

    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.

    LVL 1

    Author Comment

    tnx for ur comments. My problem been solved using

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

    tnx a lot.....
    LVL 12

    Expert Comment

    new Date(String) is deprecated:
    As of JDK version 1.1, replaced by DateFormat.parse(String s)

    Bye, Giant.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now