java.lang.NullPointerException:尝试调用虚方法

时间:2015-08-23 23:09:54

标签: android nullpointerexception

我正在尝试使用注册和mysql登录创建一个登录功能,但我的应用程序每次都崩溃。我无法找到问题!

这是教程中的代码,我根据自己的需要进行了调整。

这是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();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

任何正在做这件事的事情都失败了:

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

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

您需要做的是提供ifnull条件。因此,如果它们永远为null,您可以决定不做任何事情或给它一个默认值

答案 1 :(得分:0)

好像你的JSONObject json是null。当你在它下面的日志语句中调用json.toString()时会发生这种情况。