1

When I try to register a new user in my app, throw mySQL db the application doesn't work and sends this error message:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.io.biolite6, PID: 17633 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) at org.json.JSONTokener.nextValue(JSONTokener.java:94) at org.json.JSONObject.(JSONObject.java:156) at org.json.JSONObject.(JSONObject.java:173) at com.example.io.biolite6.BackgroundsTask.onPostExecute(BackgroundsTask.java:114) at com.example.io.biolite6.BackgroundsTask.onPostExecute(BackgroundsTask.java:30) at android.os.AsyncTask.finish(AsyncTask.java:636) at android.os.AsyncTask.access$500(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5637) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) I/Process: Sending signal. PID: 17633 SIG: 9

public class BackgroundsTask extends AsyncTask<String,Void,String> {
String register_url = "http://192.168.1.104/loginapp/register.php";
Context ctx;
ProgressDialog progressDialog;
Activity activity;
AlertDialog.Builder builder;
public BackgroundsTask(Context ctx)
{
    this.ctx = ctx;
    activity = (Activity) ctx;
}

@Override
protected void onPreExecute()
{
   builder = new AlertDialog.Builder(activity);
    progressDialog = new ProgressDialog(ctx);
    progressDialog.setTitle("Please wait");
    progressDialog.setMessage("Connecting to server...");
    progressDialog.setIndeterminate(true);
    progressDialog.setCancelable(false);
    progressDialog.show();
}

@Override
protected String doInBackground(String... params) {
    String method = params[0];

    if (method.equals("register"))
    {
        try {
            URL url = new URL(register_url);
            HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String name = params[1];
            String email = params[2];
            String password = params[3];
            String data = URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
                    URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
                    URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line = "";
           while ((line = bufferedReader.readLine())!=null)
           {

               stringBuilder.append(line+"\n");

           }
            httpURLConnection.disconnect();
            Thread.sleep(5000);
            return stringBuilder.toString().trim();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return null;

}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String json) {

    try {
        progressDialog.dismiss();
        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray("server_response");
        JSONObject JO = jsonArray.getJSONObject(0);
        String code = JO.getString("code");
        String message = JO.getString("message");
        if (code.equals("reg_true"))
        {
            showDialog("Registration Success", message,code);
        }
        else if (code.equals("reg_false"))
        {
            showDialog("Registration Failed",message,code);
        }


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

}

public void showDialog(String title, String message, String code)
{
    builder.setTitle(title);
    if (code.equals("reg_true")||code.equals("reg_false"))
    {
        builder.setMessage(message);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                activity.finish();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();


    }

}

}

Phil3992
  • 1,059
  • 6
  • 21
  • 45
tommya5
  • 11
  • 1
  • Possible duplicate of [java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference](http://stackoverflow.com/questions/30684581/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-boolean-java-l) – Simo Apr 12 '17 at 13:31
  • So what i should do ? thanks simo – tommya5 Apr 12 '17 at 13:58
  • the error "Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference" says somewhere in the rest of the code you may have tried to get the string length of an empty string. check out for it and try to initialize the string variables(if not already). – Simo Apr 12 '17 at 14:01
  • Thanks you i try !!! – tommya5 Apr 12 '17 at 14:20
  • Sorry i'm new in java arg...what do you mean when you say...initialize the string variables? – tommya5 Apr 13 '17 at 10:28

0 Answers0