1

I have a problem with populating my ListView with AsyncTask. Normally it work fine but it's extremely slow. So I implemented AsyncTask which should return the values and then the Adapter could use them to display in the ListView.

However if I run this it return nullPointerException on the values.

The both values and datasource are global variables so it should do any harm there. They both work when the Threads are not being used.

Could that be that the thread as it is asynchronous does not have the values created and therefore it goes to nullPointer, when should I be loading the values then?

        MyAsynch myAs = new MyAsynch();
        myAs.execute();



        MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);

   private class MyAsynch extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void...strings) { // run time intensive task in separate thread

            datasource = new ReadingsDataSource(getApplicationContext());
            datasource.open();
            values = datasource.getAllReadings();
            datasource.close();
            return null;
        }
    };

Here is the error:

08-20 12:21:32.107: E/AndroidRuntime(6896): FATAL EXCEPTION: main
08-20 12:21:32.107: E/AndroidRuntime(6896): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3952)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.ActivityThread.access$1300(ActivityThread.java:128)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at   android.os.Handler.dispatchMessage(Handler.java:99)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.os.Looper.loop(Looper.java:137)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.ActivityThread.main(ActivityThread.java:4514)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at java.lang.reflect.Method.invoke(Method.java:511)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at dalvik.system.NativeStart.main(Native Method)
08-20 12:21:32.107: E/AndroidRuntime(6896): Caused by: java.lang.NullPointerException
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
08-20 12:21:32.107: E/AndroidRuntime(6896):     at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
08-20 12:21:32.107: E/AndroidRuntime(6896):     ... 11 more
totpiko
  • 259
  • 2
  • 4
  • 9

1 Answers1

0

The reason is because you are trying to access your values when its empty, when you use AsyncTask the code in doInBackground is performed in background and you get call backs. So your code should be like this

    private class MyAsynch extends AsyncTask<Void, Void, Void>{

    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
     super.onPreExecute();
    pd=ProgressDialog.show(/*you context variable*/,/*Your Title*/,/* Your message while data   is loaded*/);
    }



            @Override
            protected Void doInBackground(Void...strings) { // run time intensive task in separate thread

                datasource = new ReadingsDataSource(getApplicationContext());
                datasource.open();
                values = datasource.getAllReadings();
                datasource.close();
                return null;
            }
        };


    @Override 

    protected void onPostExecute(String result) {
   super.onPostExecute(result);
    pd.dismiss();
    // Give the data to you adapter from here,instead of the place where you gave it earlier
      MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
    }
  }
Rohit
  • 1,001
  • 1
  • 10
  • 19