java.lang.NullPointerException error- Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)'

  • Replies:4
Sam Smith
  • Forum posts: 6

Dec 15, 2015, 11:33:40 AM via Website

Am trying to develop a register system in Android studio, however it registers users but the application closes down as it gives java.lang.NullPointerException error.

Errro Messages

E/JSON Parser﹕ Error parsing data org.json.JSONException: Value
2015-12-09 of type java.lang.String cannot be converted to JSONObject

E/AndroidRuntime﹕ FATAL EXCEPTION: mainPID: 2386
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on
a null object reference [CODE]> at com.bradvisor.bradvisor.Register$ProcessRegister.onPostExecute(Register.java:214)
if (json.getString(KEY_SUCCESS) != null) { - Line 214: where I get an
error message, not to sure why

        at com.bradvisor.bradvisor.Register$ProcessRegister.onPostExecute(Register.java:171)

private class ProcessRegister extends AsyncTask JSONObject> { - Line 171: where I get an error message, not to sure
why.[/CODE]

Reply
nilofar shaikh
  • Forum posts: 581

Dec 15, 2015, 1:50:08 PM via Website

A NullPointerException means that one of the variables you are passing is null, but the code tries to use it like it is not. This error occures when you try to refer to a null object instance. I cant tell you what causes this error by your given information, but you can debug it easily in your IDE. I strongly recommend you that use exception handling to avoid unexpected program behavior.

Looking for security app? Try Leo Privacy Guard 3.0.

Join Our Christmas Contest by visiting our Facebook page "LEO Privacy Guard"

Reply
Sam Smith
  • Forum posts: 6

Dec 15, 2015, 2:39:21 PM via Website

This is what I get when I debug the app.

{"tag":"register","success":1,"error":0,"user":{"fname":"Katherine","lname":"Johnson","email":"Katherine-johnson@gmail.com","uname":"Kathy","uid":"5670166f83c2b1.35739889","created_at":"2015-12-15 13:32:31"}}
12-15 13:31:46.110    2346-2390/com.bradvisor.bradvisor E/JSON Parser﹕ Error parsing data org.json.JSONException: Value 2015-12-15 of type java.lang.String cannot be converted to JSONObject
12-15 13:31:46.118    2346-2346/com.bradvisor.bradvisor E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bradvisor.bradvisor, PID: 2346
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
            at com.bradvisor.bradvisor.Register$ProcessRegister.onPostExecute(Register.java:214)
            at com.bradvisor.bradvisor.Register$ProcessRegister.onPostExecute(Register.java:171)

Reply
Gábor Auth
  • Forum posts: 6

Dec 15, 2015, 4:42:05 PM via Website

Paste the snippet of source code of the Register.java at line 214... may be you pass a null value as a String.

Reply
Sam Smith
  • Forum posts: 6

Dec 18, 2015, 4:06:54 PM via Website

if (json.getString(KEY_SUCCESS) != null) {- Line 214 pasted.

Reply