views:

55

answers:

1

I was able to fix this, but I am wondering if I can get a good explanation for why this was broken.

I created a utility function to handle my date conversions with the database for an android application. This code failed the second time it was accessed:

public class Util {

  private static final ParsePosition pos = new ParsePosition(0);

public static String isoDateFormat(Date d) {
      SimpleDateFormat databaseformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return databaseformat.format(d);
  }

  public static Date isoToDate(String isodate) {
      SimpleDateFormat databaseformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return databaseformat.parse(isodate, pos);
  } 
}

I understand that the various SimpleDateFormat features are not thread safe, but (as far as I know) my program was running inside a single thread. I am new to android and maybe I'm mistaken. I do know that I was able to fix this by moving the ParsePosition into the methods and newing it. Even if it's not thread safe, isn't it pretty much just a constant? Why would a read only constant break a thread?

Thanks!

EDIT: Here is the stacktrace:

E/AndroidRuntime( 2753): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobile/com.example.mobile.ExampleSelector}: java.lang.NullPointerException
E/AndroidRuntime( 2753):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 2753):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 2753):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 2753):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 2753):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2753):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2753):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 2753):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2753):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2753):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 2753):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 2753):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2753): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 2753):    at Util.isoToDate(Util.java:34)
A: 

In isoToDate, make sure that isodate is not null. Something like:

public static Date isoToDate(String isodate) {
   if (isodate == null) {
      // Raise exception, or return default date
   }
}
sh_kamalh
While I was debugging, I had logging statements. I can confirm that it wasn't null. Thanks for your help though!
SapphireSun
Can you run isoToDate and databaseformat.parse with hard-coded parameters and check the result?
sh_kamalh