0

I am learning mobile app development When I have implemented Async Task it was not at all working when I have added onPostExecute and onPreExecute apart from doInBackground it worked but it stops working after a while. Actually I am trying to save data from EditText clicking save button and trying to get it back using Load button and view it in Text View. Now I have also added a progress bar to this. It is throwing null pointer exception.

public class loadSomeStuff extends AsyncTask<String, Integer, String>{
        ProgressDialog dialog;

    protected void onPreExecute(String f) {
        dialog = new ProgressDialog(FileOperations.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.show();
        f="whatever";
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        FileInputStream fis=null;
        String dataResult=null;

        for(int i=0;i<20;i++){
            publishProgress(5);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        dialog.dismiss();
        try {
            fis = openFileInput(FILENAME);
            byte[] dataArray = new byte[fis.available()];
            while(fis.read(dataArray) != -1){
                dataResult= new String(dataArray);

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            try {
                fis.close();
                return dataResult;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return "no data to handle";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        dialog.incrementProgressBy(values[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        viewResults.setText(result);
    }

}
}

This is my logcat:

08-28 00:34:42.718: W/EGL_emulation(1430): eglSurfaceAttrib not implemented
08-28 00:34:49.528: D/AndroidRuntime(1430): Shutting down VM
08-28 00:34:49.528: W/dalvikvm(1430): threadid=1: thread exiting with uncaught exception (group=0xada39ba8)
08-28 00:34:49.538: E/AndroidRuntime(1430): FATAL EXCEPTION: main
08-28 00:34:49.538: E/AndroidRuntime(1430): Process: com.tarragon.tmessenger, PID: 1430
08-28 00:34:49.538: E/AndroidRuntime(1430): java.lang.NullPointerException
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.tarragon.tmessenger.FileOperations$loadSomeStuff.onProgressUpdate(FileOperations.java:154)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.tarragon.tmessenger.FileOperations$loadSomeStuff.onProgressUpdate(FileOperations.java:1)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.os.Looper.loop(Looper.java:136)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invokeNative(Native Method)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invoke(Method.java:515)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at dalvik.system.NativeStart.main(Native Method)
08-28 00:34:51.598: W/dalvikvm(1430): threadid=12: thread exiting with uncaught exception (group=0xada39ba8)
08-28 00:34:51.598: I/Process(1430): Sending signal. PID: 1430 SIG: 9
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
akhil
  • 347
  • 1
  • 3
  • 16

4 Answers4

4

Your onPreExecute() has the wrong signature. It should not take any arguments. As such, it does not override a method and is not invoked, and hence your dialog remains null.

Also note the missing @Override annotation - IDEs with default configuration add it automatically to methods that override a method and yours doesn't have it.

Remove the String f argument from the method.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Cool!! Thank you very much. Do you think you could help me with this question : http://stackoverflow.com/questions/25549679/how-can-i-split-a-long-single-sqliteopenhelper-into-serveral-classes-one-for-e – eddy Aug 28 '14 at 14:00
2

Just remove the dialog.dismiss(); from your doInBackground method and write it in onPostExecute method. You are getting Nullpointer error because you have already dismissed a dialog and at the same time you are trying to update its progress. That is why its not able to get the dialog.

I have removed the dialog.dismiss(); method from doInBackground and kept it in onPostExecute();

Try out as below:

 @Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    FileInputStream fis=null;
    String dataResult=null;

    for(int i=0;i<20;i++){
        publishProgress(5);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
  ...........................................

    return "no data to handle";
}

@Override
protected void onProgressUpdate(Integer... values) {
    dialog.incrementProgressBy(values[0]);
}

@Override
protected void onPostExecute(String result) {
      dialog.dismiss();
    viewResults.setText(result);
}

}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
2

Your onPreExecute method is wrong it like as

ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(FileOperations.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.show();

    }

and remove the dialog.dismiss() from doInBackground and add it to onPostExecute method it will like this ..

@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        viewResults.setText(result);

        if (dialog.isShowing())
            dialog.dismiss();
    }
Sachin
  • 528
  • 5
  • 17
0

write Post method like this:

@Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
   }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291