0

The code here is to update user data when logged into my app. A problem is that when im setting the userData the program throws a null pointer error even though my Log says that the object exists?

JsonObjectRequest userDataRequest = new JsonObjectRequest(Request.Method.GET, dataUrl, null, new Response.Listener < JSONObject > () {
    @Override
    public void onResponse(JSONObject response) {
        Log.i("Success", String.valueOf(response));
        // Set the user data in the live model when logging in
        try {
            userViewModel.setUserData(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

This is the setUserData code and error

public void setUserData(JSONObject userObject) throws JSONException {
        HashMap<String, String> userDataMap = new HashMap<>();
        userDataMap.put("username", userObject.get("username").toString());
        userDataMap.put("first_name", userObject.get("first_name").toString());
        userDataMap.put("last_name", userObject.get("last_name").toString());
        userDataMap.put("email", userObject.get("email").toString());
        userDataMap.put("phone", userObject.get("phone").toString());

        userLiveData.setValue(userDataMap);
    }


E/AndroidRuntime: FATAL EXCEPTION: main
    Process: se.liu.robn725.tddd80_projekt, PID: 25928
    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.setValue(java.lang.Object)' on a null object reference
        at se.liu.robn725.tddd80_projekt.UserViewModel.setUserData(UserViewModel.java:42)
        at se.liu.robn725.tddd80_projekt.LoginFragment$6.onResponse(LoginFragment.java:122)
        at se.liu.robn725.tddd80_projekt.LoginFragment$6.onResponse(LoginFragment.java:116)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 25928 SIG: 9
fallensach
  • 11
  • 3
  • 2
    Please post the full stacktrace, my guess is that `userViewModel` is null and the best place to determine that is the stacktrace. – MidasLefko Apr 29 '21 at 09:05
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Apr 29 '21 at 09:05
  • ive added further information in the post – fallensach Apr 29 '21 at 09:09
  • 1
    As you can see from this line `Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.setValue(java.lang.Object)' on a null object reference` the variable `userLiveData` is null. Do you initialize it in the viewmodel ? – MidasLefko Apr 29 '21 at 09:15
  • The `userLiveData` is null. – Hadas Apr 29 '21 at 10:24
  • @fallenchng chang happy to help – MidasLefko Apr 29 '21 at 19:16

0 Answers0