Originally Posted by
kamalivy
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.