3

I'm writing a program that download data from a database, and when I was using the onPostExecutemethod for elaborate data everything works fine. Because I needed this data in the class that calls the AsynkTask class, I used the method new AsynkTaskClass.execute().get() and it worked. Ultimately I implemented an Interface instead of the .get() method and the program crashes. When I debug it, it crashes in a line of the method updateJSONData(), called in doInBackground, and the strange part is that in that method I didn't changed a line when I implemented interfaces. I implemented interface as in this answer.

I put here some pieces of code:

Register.java : the class from which I call the class that extends AsynkTask.

package com.PacchettoPrincipale.app;

import [...]

public class Register extends Activity implements OnClickListener{

    private EditText user, pass, codDoc;
    private Button  mRegister;

    private ProgressDialog pDialog;

    // Variabile utilizzata per ricevere le fermate attraverso l'interfaccia retrieveHashMapFromAsynkTask
    ArrayList<HashMap<String, String>> fermate;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    //testing on Emulator:
    private static final String LOGIN_URL = "http://10.0.2.2/PrimaAppAndroid/register.php";

    //ids
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";


    //not important
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        user = (EditText)findViewById(R.id.username);
        pass = (EditText)findViewById(R.id.password);
        codDoc = (EditText)findViewById(R.id.codDoc);

        mRegister = (Button)findViewById(R.id.register);
        mRegister.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        // HERE is where I call the AsynkTask class
        new LoadFermate(Register.this, new retrieveHashMapFromAsynkTask() {
            @Override
            public void assignment(ArrayList<HashMap<String, String>> returnedValues) {
                fermate = returnedValues;
            }
        }).execute();

        for (HashMap<String, String> fermata:fermate){
            Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show();
        }
    }

    // is another AsynkTask class, don't look at it.
    class CreateUser extends AsyncTask<String, String, String> {[...]}

}

LoadFermate.java is the class that extends AsynkTask, I put an uppercase comment at the line where the program crashes.

package com.PacchettoPrincipale.app;

import [...]

public class LoadFermate extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {

    private Context context;
    private retrieveHashMapFromAsynkTask retrieveListener;

    public LoadFermate(Context context, retrieveHashMapFromAsynkTask retrieveListener){
        this.context = context;
        this.retrieveListener = retrieveListener;
    }

    private ProgressDialog pDialog;

    private JSONArray mComments = null;
    private ArrayList<HashMap<String, String>> mCommentList;

    //testing on Emulator:
    private static final String DOWNLOAD_FERMATE_URL = "http://10.0.2.2/PrimaAppAndroid/get/fermate.php";

    //JSON IDS:
    private static final String TAG_COD = "CodFermata";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_NOME = "Nome";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Download fermate...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(Void... voids) {
        updateJSONData();
        return mCommentList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> hashMaps) {
        super.onPostExecute(hashMaps);

        pDialog.dismiss();

        if(retrieveListener != null){
            retrieveListener.assignment(hashMaps);
        }
    }

    private void updateJSONData() {

        mCommentList = new ArrayList<HashMap<String, String>>();

        JSONParser jParser = new JSONParser();

        //THE PROGRAM CRASHES AT THIS LINE
        JSONObject json = jParser.getJSONFromUrl(DOWNLOAD_FERMATE_URL);

        try {

            mComments = json.getJSONArray(TAG_POSTS);

            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);

                int CodFermata = c.getInt(TAG_COD);
                String Nome = c.getString(TAG_NOME);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_COD, String.valueOf(CodFermata));
                map.put(TAG_NOME, Nome);

                mCommentList.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}

retrieveHashMapFromAsynkTask.java : is the interface.

package com.PacchettoPrincipale.app;

import java.util.ArrayList;
import java.util.HashMap;

public interface retrieveHashMapFromAsynkTask {
    public void assignment(ArrayList<HashMap<String, String>> returnedValues);
}

I also put the LogCat filtred by the word Exception, where there are many NullPointerException. I think that this is the key, but I can't understand where it is risen.

Logcat :

01-05 05:46:18.460        52-52/? W/dalvikvm﹕ Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Landroid/media/videoeditor/MediaArtistNativeHelper;
01-05 05:47:31.270      382-397/system_process W/BroadcastQueue﹕ Failure sending broadcast Intent { act=android.intent.action.CONFIGURATION_CHANGED flg=0x70000010 }
    android.os.TransactionTooLargeException
            at android.os.BinderProxy.transact(Native Method)
            at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1059)
            at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:421)
            at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:507)
            at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:555)
            at com.android.server.am.BroadcastQueue$1.handleMessage(BroadcastQueue.java:141)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1870)
01-05 05:47:31.290      382-397/system_process W/BroadcastQueue﹕ Failure sending broadcast Intent { act=android.intent.action.CONFIGURATION_CHANGED flg=0x70000010 }
    android.os.TransactionTooLargeException
            at android.os.BinderProxy.transact(Native Method)
            at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1059)
            at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:421)
            at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:507)
            at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:555)
            at com.android.server.am.BroadcastQueue$1.handleMessage(BroadcastQueue.java:141)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1870)
01-05 05:47:31.310      382-397/system_process W/BroadcastQueue﹕ Failure sending broadcast Intent { act=android.intent.action.CLOSE_SYSTEM_DIALOGS flg=0x50000010 (has extras) }
    android.os.TransactionTooLargeException
            at android.os.BinderProxy.transact(Native Method)
            at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1059)
            at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:421)
            at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:507)
            at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:555)
            at com.android.server.am.BroadcastQueue$1.handleMessage(BroadcastQueue.java:141)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1870)
01-05 05:47:42.750      382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '18 interface fwmark exempt add 10.0.2.2/32' failed with '400 18 Failed to add exemption rule (File exists)'
01-05 05:47:42.870      382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '20 interface fwmark exempt add 10.0.2.3/32' failed with '400 20 Failed to add exemption rule (File exists)'
01-05 05:47:42.920      382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '21 interface route add eth0 secondary 10.0.2.2 32 0.0.0.0' failed with '400 21 ip route modification failed (No such device)'
01-05 05:47:42.960      382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '22 interface route add eth0 secondary 0.0.0.0 0 10.0.2.2' failed with '400 22 ip route modification failed (No such device)'
01-05 05:47:52.480      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '30 interface fwmark exempt add 10.0.2.2/32' failed with '400 30 Failed to add exemption rule (File exists)'
01-05 05:47:52.720      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '32 interface fwmark exempt add 173.194.40.6/32' failed with '400 32 Failed to add exemption rule (File exists)'
01-05 05:47:55.950      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '34 interface fwmark exempt add 10.0.2.2/32' failed with '400 34 Failed to add exemption rule (File exists)'
01-05 05:47:56.080      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '36 interface fwmark exempt add 173.194.40.0/32' failed with '400 36 Failed to add exemption rule (File exists)'
01-05 05:47:57.240      663-678/com.android.systemui W/Binder﹕ Binder call failed.
    android.os.DeadObjectException
            at android.os.BinderProxy.transact(Native Method)
            at android.content.IIntentReceiver$Stub$Proxy.performReceive(IIntentReceiver.java:124)
            at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:816)
            at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:394)
            at android.os.Binder.execTransact(Binder.java:404)
            at dalvik.system.NativeStart.run(Native Method)
01-05 05:47:57.270      663-678/com.android.systemui E/JavaBinder﹕ *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
    java.lang.RuntimeException: android.os.DeadObjectException
            at android.os.Parcel.writeException(Parcel.java:1366)
            at android.os.Binder.execTransact(Binder.java:410)
            at dalvik.system.NativeStart.run(Native Method)
     Caused by: android.os.DeadObjectException
            at android.os.BinderProxy.transact(Native Method)
            at android.content.IIntentReceiver$Stub$Proxy.performReceive(IIntentReceiver.java:124)
            at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:816)
            at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:394)
            at android.os.Binder.execTransact(Binder.java:404)
            at dalvik.system.NativeStart.run(Native Method)
01-05 05:47:59.220      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '38 interface fwmark exempt add 10.0.2.2/32' failed with '400 38 Failed to add exemption rule (File exists)'
01-05 05:47:59.320      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '40 interface fwmark exempt add 173.194.40.5/32' failed with '400 40 Failed to add exemption rule (File exists)'
01-05 05:48:02.400      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '42 interface fwmark exempt add 10.0.2.2/32' failed with '400 42 Failed to add exemption rule (File exists)'
01-05 05:48:02.480      382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '44 interface fwmark exempt add 173.194.40.7/32' failed with '400 44 Failed to add exemption rule (File exists)'
01-05 05:57:13.340    1104-1104/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a5cba8)
01-05 05:57:22.380    1104-1104/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.PacchettoPrincipale.app, PID: 1104
    java.lang.NullPointerException
            at com.PacchettoPrincipale.app.Register.onClick(Register.java:87)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
01-05 05:58:03.480      382-571/system_process W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 1104 uid 10052
01-05 05:58:03.500      530-546/com.android.inputmethod.latin W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
    java.lang.NullPointerException
            at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
            at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
            at android.os.Binder.execTransact(Binder.java:404)
            at dalvik.system.NativeStart.run(Native Method)
01-05 05:58:16.800      382-575/system_process W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 1136 uid 10052
01-05 05:58:16.890      530-543/com.android.inputmethod.latin W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
    java.lang.NullPointerException
            at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
            at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
            at android.os.Binder.execTransact(Binder.java:404)
            at dalvik.system.NativeStart.run(Native Method)

Another weird part is that The nullPointer is located at Register line 87, a line where I'm not arrived with the debug. It crashes before arriving there: It crashes during doInBackground, and Register:87 is executed after onPostExecute.

Thank you for the answer in advance!

Community
  • 1
  • 1
giacomotb
  • 607
  • 4
  • 10
  • 24
  • Looks like it all started here: java.lang.NullPointerException at com.PacchettoPrincipale.app.Register.onClick(Register.java:87) – marcinj Jan 05 '14 at 11:49
  • I updated the question, I've forgot a part. Thank you for remind me it! – giacomotb Jan 05 '14 at 11:55

1 Answers1

2

Here is the NullPointerException...

   for (HashMap<String, String> fermata : fermate){
        Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show();
   }

here fermate is null. You are initializing fermate in retrieveHashMapFromAsynkTask callback which will be called by AsyncTask in it's onPostExecute(). you should wait for the AsyncTask to complete it's task or move that Toast to retrieveHashMapFromAsynkTask implementation...

    new LoadFermate(Register.this, new retrieveHashMapFromAsynkTask() {
        @Override
        public void assignment(ArrayList<HashMap<String, String>> returnedValues) {
            fermate = returnedValues;
            for (HashMap<String, String> fermata:fermate){
                Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show();
            }
        }
    }).execute();
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • how can I wait for it? – giacomotb Jan 05 '14 at 12:00
  • just an advice... In my real project I leave the Toast, and I'll do all this stuff 3 times(I'll load 'fermate', 'tratta' and 'linea'), and then I'll work with this 3 arrays. How can I wait that all 3 will be loaded and avoid that one of it raises NullPointerException? – giacomotb Jan 05 '14 at 16:42