Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ClassCastException

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default ClassCastException

    Hi,

    I have the following piece of code. It's basically a method which formats a message (text) containing date.

    public static String fmtDate(String fmt, Object arg, Locale locale, String fe) {
    MessageFormat mf = new MessageFormat(fmt, locale);
    return mf.format(arg);
    }
    }

    where 'arg' can either be and instance of 'TimeStamp' or 'Date'.

    But during runtime, I get the following exception

    java.lang.ClassCastException: java.sql.Timestamp cannot be cast to [Ljava.lang.Object;
    at java.text.MessageFormat.format(MessageFormat.java: 836)
    at java.text.Format.format(Format.java:140)
    at com.pg.site.common.ApplicationBean.fmtDate(Applica tionBean.java:758)
    at com.pg.site.common.PATxtProvider.getKeyValue(PATxt Provider.java:139)
    at com.pg.webframework.utils.TxtProvider.customGetTex t(TxtProvider.java:15)
    at com.pg.webframework.common.CommonAction.customGetT ext(CommonAction.java:46)
    at com.pg.webframework.common.CommonAction.getText(Co mmonAction.java:91)
    at com.pg.webframework.common.CommonAction.getText(Co mmonAction.java:80)
    at com.pg.webframework.common.CommonAction.getText(Co mmonAction.java:75)
    at com.pg.site.actions.bonus.TransHistoryAction.trans History(TransHistoryAction.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.opensymphony.xwork2.DefaultActionInvocation.in vokeAction(DefaultActionInvocation.java:406)
    at com.opensymphony.xwork2.DefaultActionInvocation.in vokeActionOnly(DefaultActionInvocation.java:269)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:231)
    at com.pg.site.interceptors.RedirectToCashout.interce pt(RedirectToCashout.java:33)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.pg.webframework.sso.SSOSessionInterceptor.inte rcept(SSOSessionInterceptor.java:27)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.pg.site.interceptors.LocaleInterceptor.interce pt(LocaleInterceptor.java:222)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.pg.site.interceptors.PopulateRequiredParams.in tercept(PopulateRequiredParams.java:204)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.interceptor.ConversionErro rInterceptor.intercept(ConversionErrorInterceptor. java:123)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)




    Can anyone please help me out here?


    Thanks in advance,
    Kamal


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException

    What is the value of "arg" when the method is called? Is it an array?

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: ClassCastException

    I've realized my mistake. I'm passing an Object but MessageFormat.format() is expecting an Object[]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException

    Reading the API doc can help.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Post Re: ClassCastException

    Quote Originally Posted by kamalivy View Post
    Hi,

    I have the following piece of code. It's basically a method which formats a message (text) containing date.

    public static String fmtDate(String fmt, Object arg, Locale locale, String fe) {
    MessageFormat mf = new MessageFormat(fmt, locale);
    return mf.format(arg);
    }
    }

    where 'arg' can either be and instance of 'TimeStamp' or 'Date'.

    But during runtime, I get the following exception

    java.lang.ClassCastException: java.sql.Timestamp cannot be cast to [Ljava.lang.Object;
    at java.text.MessageFormat.format(MessageFormat.java: 836)
    at java.text.Format.format(Format.java:140)
    at com.pg.site.common.ApplicationBean.fmtDate(Applica tionBean.java:758)
    at com.pg.site.common.PATxtProvider.getKeyValue(PATxt Provider.java:139)
    at com.pg.webframework.utils.TxtProvider.customGetTex t(TxtProvider.java:15)
    at com.pg.webframework.common.CommonAction.customGetT ext(CommonAction.java:46)
    at com.pg.webframework.common.CommonAction.getText(Co mmonAction.java:91)
    at com.pg.webframework.common.CommonAction.getText(Co mmonAction.java:80)
    at com.pg.webframework.common.CommonAction.getText(Co mmonAction.java:75)
    at com.pg.site.actions.bonus.TransHistoryAction.trans History(TransHistoryAction.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.opensymphony.xwork2.DefaultActionInvocation.in vokeAction(DefaultActionInvocation.java:406)
    at com.opensymphony.xwork2.DefaultActionInvocation.in vokeActionOnly(DefaultActionInvocation.java:269)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:231)
    at com.pg.site.interceptors.RedirectToCashout.interce pt(RedirectToCashout.java:33)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.pg.webframework.sso.SSOSessionInterceptor.inte rcept(SSOSessionInterceptor.java:27)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.pg.site.interceptors.LocaleInterceptor.interce pt(LocaleInterceptor.java:222)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.pg.site.interceptors.PopulateRequiredParams.in tercept(PopulateRequiredParams.java:204)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.in voke(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.interceptor.ConversionErro rInterceptor.intercept(ConversionErrorInterceptor. java:123)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:226)
    at com.opensymphony.xwork2.DefaultActionInvocation$2. doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.util.profiling.UtilTimerSt ack.profile(UtilTimerStack.java:455)




    Can anyone please help me out here?


    Thanks in advance,
    Kamal
    I believe, though I could be wrong , that a ClassCastException comes when you try to make a reference variable of a subclass type into a reference variable of a superclass type.

    You can do this:

    public class blaBlaBla

    public class yakYakYak extends blaBlaBla


    blaBlaBla refVarOne;

    yakYakYak refVarTwo;

    refVarOne.methodFromYakYakYak();

    but not
    refVarTwo.methodFromBlaBlaBla();

    doing refVarTwo.methodFromBlaBlaBla() will throw a ClassCastException, at least I think that's what it'll do if you do that.



    It appears that I wasn't far off.

    A Class Cast Exception is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
    Object x = new Integer(0);
    System.out.println((String)x);

    Woops, you can call methods of the superclass since they'll be inherited by the subclass. You can't make a reference variable of subclass type be a reference variable of superclass type.
    Last edited by javapenguin; July 21st, 2010 at 10:43 AM. Reason: Here's what a ClassCastException is