0

I am trying to create a sign in function with register and login by mysql, but my app crashes everytime. I can't find the issue!

It is a code from a tutorial, and I adjusted it to my own needs.

Here is the Logcat:

08-23 18:59:43.828    7756-7756/com.example.boss.partyradarvol4 I/art﹕ Late-enabling -Xcheck:jni         
08-23 18:59:43.933    7756-7774/com.example.boss.partyradarvol4 D/OpenGLRenderer﹕ Render dirty regions requested: true
08-23 18:59:43.942    7756-7756/com.example.boss.partyradarvol4 D/﹕ HostConnection::get() New Host Connection established 0xabc785e0, tid 7756
08-23 18:59:43.974    7756-7756/com.example.boss.partyradarvol4 D/Atlas﹕ Validating map...
08-23 18:59:44.239    7756-7774/com.example.boss.partyradarvol4 D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
08-23 18:59:44.240    7756-7774/com.example.boss.partyradarvol4 D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
08-23 18:59:44.251    7756-7774/com.example.boss.partyradarvol4 D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
08-23 18:59:44.260    7756-7774/com.example.boss.partyradarvol4 D/﹕ HostConnection::get() New Host Connection established 0xa3316120, tid 7774
08-23 18:59:44.297    7756-7774/com.example.boss.partyradarvol4 I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-23 18:59:44.332    7756-7774/com.example.boss.partyradarvol4 D/OpenGLRenderer﹕ Enabling debug mode 0
08-23 18:59:44.352    7756-7774/com.example.boss.partyradarvol4 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-23 18:59:44.352    7756-7774/com.example.boss.partyradarvol4 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa33192c0, error=EGL_SUCCESS
08-23 18:59:49.134    7756-7774/com.example.boss.partyradarvol4 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-23 18:59:49.135    7756-7774/com.example.boss.partyradarvol4 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabcff960, error=EGL_SUCCESS
08-23 18:59:53.490    7756-7817/com.example.boss.partyradarvol4 D/Anfrage﹕ startet
08-23 18:59:53.492    7756-7817/com.example.boss.partyradarvol4 E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
08-23 18:59:53.492    7756-7817/com.example.boss.partyradarvol4 E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
08-23 18:59:53.517    7756-7817/com.example.boss.partyradarvol4 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5
    Process: com.example.boss.partyradarvol4, PID: 7756
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
            at com.example.boss.partyradarvol4.Login.Register$CreatUser.doInBackground(Register.java:92)
            at com.example.boss.partyradarvol4.Login.Register$CreatUser.doInBackground(Register.java:62)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
08-23 18:59:53.576    7756-7774/com.example.boss.partyradarvol4 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-23 18:59:53.577    7756-7774/com.example.boss.partyradarvol4 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabcff540, error=EGL_SUCCESS
08-23 18:59:54.094    7756-7778/com.example.boss.partyradarvol4 D/Anfrage﹕ startet
08-23 18:59:54.094    7756-7778/com.example.boss.partyradarvol4 E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
08-23 18:59:54.094    7756-7778/com.example.boss.partyradarvol4 E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
08-23 18:59:54.095    7756-7778/com.example.boss.partyradarvol4 I/Process﹕ Sending signal. PID: 7756 SIG: 9

Register.java:

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.boss.partyradarvol4.R;
import com.example.boss.partyradarvol4.SupportKlassen.JSONEncorder;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Register extends Activity implements View.OnClickListener{

    private EditText eUsername, ePasswort;
    private Button  bRegestrieren;

    private ProgressDialog pDialog;

    //Encoder
    JSONEncorder jsonEncorder = new JSONEncorder();

    // Verbindung zum Server
    private static final String Reg_URL = "http://127.0.0.1:80/webservice/login.php";

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_);

        // Editfelder
        eUsername = (EditText)findViewById(R.id.FELD_Reg_username);
        ePasswort = (EditText)findViewById(R.id.FELD_Reg_passwort);

        bRegestrieren = (Button)findViewById(R.id.BUTTON_Reg_regestrieren);
        bRegestrieren.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        new CreatUser().execute();
    }

    class CreatUser extends AsyncTask<String,String,String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            super.onPreExecute();
            pDialog = new ProgressDialog(Register.this);
            pDialog.setMessage("erstelle user...");
            //unbestimmt
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {

            int success;
            String username = eUsername.getText().toString();
            String passwort = ePasswort.getText().toString();

            try {
                List<NameValuePair> parameter = new ArrayList<NameValuePair>();
                parameter.add(new BasicNameValuePair("username", username));
                parameter.add(new BasicNameValuePair("password", passwort));

                Log.d("Anfrage", "startet");

                JSONObject json = jsonEncorder.makeHttpRequest(Reg_URL, null ,parameter);

                Log.d("Restrierung vorbreitet", json.toString());

                success = json.getInt(TAG_SUCCESS);
                if(success == 1){
                    Log.d("User erstellt", json.toString());
                    finish();
                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Registrierung fehlgeschlagen!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            if(file_url != null){
                Toast.makeText(Register.this, file_url, Toast.LENGTH_LONG).show();
            }
        }
    }
}
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
Jay Di
  • 19
  • 2
  • 5
  • 4
    Please do a search first. `NullPointerException`s have been explained to death. – Karakuri Aug 23 '15 at 23:16
  • Can you put the code? The problem is in the line 92 of your Register class. In that line you are calling a toString method in an null object – Aracem Aug 23 '15 at 23:17
  • You have a null JSONObject that you're calling `toString()` on, likely due to not getting a valid response back from the server. It also looks like you're using JSONParser, take a look at my blog post: http://danielnugent.blogspot.com/2015/06/updated-jsonparser-with.html – Daniel Nugent Aug 23 '15 at 23:17
  • Ok Thx guys i try it later.. but i have a other qustion too. NameValuePair and BasicNameValuePair is in my code strike! And i dont know why this so is. Any answer? – Jay Di Aug 24 '15 at 14:13

2 Answers2

0

It's failing on anything that's doing this:

JSONObject json = jsonEncorder.makeHttpRequest(Reg_URL, null ,parameter);

Log.d("Restrierung vorbreitet", json.toString());

What you need to do is provide ifnull conditions. So in case they are ever null you can decide to do nothing or give it a default value

FirebladeDan
  • 1,069
  • 6
  • 14
  • i dont sure if i understand it right. What u mean with default value, can u give me pls a example ? – Jay Di Aug 24 '15 at 08:52
  • JSONObject json = jsonEncorder.makeHttpRequest(Reg_URL, "POST",parameter); Log.d("Restrierung vorbreitet", json.toString()); U mean so ? – Jay Di Aug 24 '15 at 09:12
0

Seems like your JSONObject json is null. It is happening when you call json.toString() in your log statement right under it.

inder_gt
  • 492
  • 1
  • 7
  • 9