androidserviceandroid-asynctasknullpointerexception

Asynctask in service with an 'onPostExecute' error


Consider:

Error : NullPointerException
        can't get data in onPostExecute();

I create an AsyncTask in onStartCommand() of a Service;

After startService:

  1. LoadDataFromNetwork.getData runs in doInBackground. This method will get data from the Internet. It can be work, and I can see the Log;

  2. I can't get data in onPostExcute();

    Why?

  3. Error image

    Enter image description here

  4. Code:

     public class Service_LoadAndStoreData extends Service {
    
         @Override
         public void onCreate() {
             super.onCreate();
         }
    
         @Override
         public int onStartCommand(Intent intent, int flags, int startId) {
    
             Log.d(Application.TAG , "service start");
             new AsyncTask<Void ,Void ,List<ChannelBeans>>(){
                 @Override
                 protected List<ChannelBeans> doInBackground(Void... params) {
                     return LoadDataFromNetwork.getData();//it can be work
                 }
    
                 @Override
                 protected void onPostExecute(List<ChannelBeans> channelBeanses) {
                     super.onPostExecute(channelBeanses);
                     Log.d(Application.TAG , channelBeanses.toString()); //Error :NullPointerException;
                 }
             }.execute();
    
             return super.onStartCommand(intent, flags, startId);
         }
    
         @Override
         public IBinder onBind(Intent intent) {
             return null;
         }
     }
    

Solution

  • The problem is at the getData() method from LoadDataFromNetwork class, I think.