saeed khalafinejad saeed khalafinejad - 4 months ago 14
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(){
GregorianCalendar cl = new GregorianCalendar(Locale.US);
cl.set(2000, 10, 33);
System.out.println("date set");
try {
cl.getTime();//validating the date
} catch (Exception e) {
String err = e.getMessage();
if (err.equals("DAY_OF_MONTH")) System.out.println("wrong day");

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

date set
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:

date set
wrong day

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(
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.view.View.performClick(
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.view.View$
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.os.Handler.handleCallback(
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.os.Handler.dispatchMessage(
09-22 16:52:28.021: E/AndroidRuntime(1696): at android.os.Looper.loop(
09-22 16:52:28.021: E/AndroidRuntime(1696): at
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(
09-22 16:52:28.021: E/AndroidRuntime(1696): at$
09-22 16:52:28.021: E/AndroidRuntime(1696): at
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(
09-22 17:50:19.411: W/System.err(1936): at java.util.Calendar.getTimeInMillis(
09-22 17:50:19.421: W/System.err(1936): at java.util.Calendar.getTime(
09-22 17:50:19.421: W/System.err(1936): at com.example.tstcal.MainActivity.onClick(
09-22 17:50:19.421: W/System.err(1936): at android.view.View.performClick(
09-22 17:50:19.421: W/System.err(1936): at android.view.View$
09-22 17:50:19.421: W/System.err(1936): at android.os.Handler.handleCallback(
09-22 17:50:19.431: W/System.err(1936): at android.os.Handler.dispatchMessage(
09-22 17:50:19.464: W/System.err(1936): at android.os.Looper.loop(
09-22 17:50:19.464: W/System.err(1936): at
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(
09-22 17:50:19.464: W/System.err(1936): at$
09-22 17:50:19.471: W/System.err(1936): at
09-22 17:50:19.471: W/System.err(1936): at dalvik.system.NativeStart.main(Native Method)


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) {

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