saeed khalafinejad saeed khalafinejad - 5 months ago 21
Android Question

Java Calendar.getTime() returns null exception message in Android but not Java project

I have the following Java code in Eclipse:

public void onClick(View v) {
if (v.getId()==R.id.button1){
System.out.println("start...");
GregorianCalendar cl = new GregorianCalendar(Locale.US);
cl.setLenient(false);
cl.clear();
cl.set(2000, 10, 33);
System.out.println("date set");
try {
cl.getTime();//validating the date
} catch (Exception e) {
String err = e.getMessage();
System.out.println("error:"+err);
if (err.equals("DAY_OF_MONTH")) System.out.println("wrong day");
}
System.out.println("finished");
}
}


When I run the above code in an Android project I encounter a strange error as follows:

start...
date set
error:null
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.tstcal.MainActivity.onClick
... (see complete stack trace at the end)


When I run the same code in a Java project then every thing is fine:

start...
date set
error:DAY_OF_MONTH
wrong day
finished


It is obvious that e.getMessage() is null in Android but WHY?

Complete stack trace:

09-22 16:52:27.960: I/System.out(1696): start...
09-22 16:52:27.960: I/System.out(1696): date set
09-22 16:52:28.004: I/System.out(1696): error:null
09-22 16:52:28.004: D/AndroidRuntime(1696): Shutting down VM
09-22 16:52:28.010: W/dalvikvm(1696): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-22 16:52:28.021: E/AndroidRuntime(1696): FATAL EXCEPTION: main
09-22 16:52:28.021: E/AndroidRuntime(1696): java.lang.NullPointerException
09-22 16:52:28.021: E/AndroidRuntime(1696): at com.example.tstcal.MainActivity.onClick(MainActivity.java:42)
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.view.View.performClick(View.java:4084)
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.view.View$PerformClick.run(View.java:16966)
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.os.Handler.handleCallback(Handler.java:615)
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.os.Looper.loop(Looper.java:137)
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-22 16:52:28.021: E/AndroidRuntime(1696): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 16:52:28.021: E/AndroidRuntime(1696): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 16:52:28.021: E/AndroidRuntime(1696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-22 16:52:28.021: E/AndroidRuntime(1696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 16:52:28.021: E/AndroidRuntime(1696): at dalvik.system.NativeStart.main(Native Method)


The output of e.printStackTrace():

09-22 17:50:19.400: W/System.err(1936): java.lang.IllegalArgumentException
09-22 17:50:19.411: W/System.err(1936): at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:834)
09-22 17:50:19.411: W/System.err(1936): at java.util.Calendar.getTimeInMillis(Calendar.java:1099)
09-22 17:50:19.421: W/System.err(1936): at java.util.Calendar.getTime(Calendar.java:1085)
09-22 17:50:19.421: W/System.err(1936): at com.example.tstcal.MainActivity.onClick(MainActivity.java:38)
09-22 17:50:19.421: W/System.err(1936): at android.view.View.performClick(View.java:4084)
09-22 17:50:19.421: W/System.err(1936): at android.view.View$PerformClick.run(View.java:16966)
09-22 17:50:19.421: W/System.err(1936): at android.os.Handler.handleCallback(Handler.java:615)
09-22 17:50:19.431: W/System.err(1936): at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 17:50:19.464: W/System.err(1936): at android.os.Looper.loop(Looper.java:137)
09-22 17:50:19.464: W/System.err(1936): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-22 17:50:19.464: W/System.err(1936): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 17:50:19.464: W/System.err(1936): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 17:50:19.464: W/System.err(1936): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-22 17:50:19.471: W/System.err(1936): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 17:50:19.471: W/System.err(1936): at dalvik.system.NativeStart.main(Native Method)

Answer

My guess is that your current version of Android is using an alternative java source code, that differs from the java source in your Java project.

I was observing the two source codes (Android java library and the source Java) and there are a few things different.

Android Java Library and Java Library:

/**
 * Computes {@code time} from the Calendar fields.
 *
 * @throws IllegalArgumentException
 *                if the time cannot be computed from the current field
 *                values.
 */
protected abstract void computeTime();

The GregorianCalendar in Android Java source throws exceptions like this:

//If an exception occurs...
throw new IllegalArgumentException();

The GregorianCalendar in Java Source code throws exceptions like this:

//If an exception occurs...
throw new IllegalArgumentException(getFieldName(field));

And the class IllegalArgumentException receives an parameter for the message, like this:

/**
 * Constructs a new {@code IllegalArgumentException} with the current stack
 * trace and the specified detail message.
 *
 * @param detailMessage
 *            the detail message for this exception.
 */
public IllegalArgumentException(String detailMessage) {
    super(detailMessage);
}

So, using the Android Java source does not have a detailed message, and Java does.

Comments