1

I'm currently trying to display the current time to my custom ListView but I keep getting a NullPointerException and was wondering what I was doing wrong since I was already adding to the array and grabbing its value.

The custom Adapter and setter/getters are here: 1 2

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list_view);
    boss_title = getResources().getStringArray(R.array.boss_array);
    bossTime = (TextView) findViewById(R.id.boss_time);

    adapter = new BossAdapter(getApplicationContext(), R.layout.boss_layout);
    listView.setAdapter(adapter);
    int i = 0;


    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Calendar c = Calendar.getInstance();
                            SimpleDateFormat df = new SimpleDateFormat("HH-mm-ss");
                            String formattedTime = df.format(c.getTime());

                            String [] boss_time_array = new String[6];
                            for(int i =0; i < 6; i++){
                                boss_time_array[i] = formattedTime;
                            }

                            boss_time = boss_time_array;

                        }
                    });
                }
            } catch (InterruptedException e) {
            }
        }
    };

    t.start();

    for (String boss : boss_title) {
        Boss bossObject = new Boss(boss_icon[i], boss, boss_time[i]);

        adapter.add(bossObject);
        i++;
    }

}

Logcat

 8541-8541/baegmon.com.bosstimer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: baegmon.com.bosstimer, PID: 8541
java.lang.RuntimeException: Unable to start activity ComponentInfo{baegmon.com.bosstimer/baegmon.com.bosstimer.MainActivity}:     java.lang.NullPointerException: Attempt to read from null array
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to read from null array
        at baegmon.com.bosstimer.MainActivity.onCreate(MainActivity.java:67)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

How do I modify it so that it doesn't return a null-pointer error and returns the correct time to my list view?

Simon Baeg
  • 69
  • 5

1 Answers1

0

You get the NullPointerException because the Thread isn't finished when you create the Adapter. After the Thread was started you are immediately reading the results from boss_title. This not possible because you have to wait for it to be finished.

You should use an AsyncTask for this and create your Adapter inside the onPostExecute (http://developer.android.com/reference/android/os/AsyncTask.html).

Also the SimpleDateFormat class is not thread-safe:

public static Date dateToString(Date date, final String format, final Locale locale)
  ThreadLocal<SimpleDateFormat> formater = new ThreadLocal<SimpleDateFormat>() {
    @Override
    protected SimpleDateFormat initialValue() {
      return new SimpleDateFormat(format, locale);
    }
  };
  return formater.get().format(string);
}
  • ah Ok I can see why that is a problem. I'm slightly confused on how the AsyncTask will work because the time will be constantly updating so technically the thread will never end meaning postexecute will not be possible? – Simon Baeg Jul 20 '15 at 08:20
  • Then you might use the CountDownTimer which can be executed repeatedly aftere a specified amount of time (http://developer.android.com/reference/android/os/CountDownTimer.html). In the onTick method you can retrieve the new timestamp. –  Jul 20 '15 at 08:29