NullPointerException NetAsync错误:在联系服务器屏幕上,应用程序始终崩溃

时间:2014-09-04 19:14:07

标签: java php android nullpointerexception registration

我正在努力连接移动应用和服务器Apache。我想,我试过调试并解决了所有拼写错误。这是我的代码,请帮忙:

    @Override
     protected void onPostExecute(JSONObject json) {
        // Checks for success message
        try {
            Log.e(KEY_ERROR, "Comes to Here");
            if (json.getString(KEY_SUCCESS) != null) {
                registerErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS);
                String red = json.getString(KEY_ERROR);
                if (Integer.parseInt(res) == 1) {
                    pDialog.setTitle("Getting Data");
                    pDialog.setMessage("Loading Info");
                    registerErrorMsg.setText("Successfully Registerd");
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Removes all the previous data in the SQlite database
                    UserFunctions logout = new UserFunctions();
                    logout.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_FIRSTNAME), json_user.getString(KEY_LASTNAME),   json_user.getString(KEY_EMAIL), json_user.getString(KEY_USERNAME), json_user.getString(KEY_UID),  json_user.getString(KEY_CREATED_AT));

                    // Stores registered data in SQLite Database
                    //Launch Registered screen
                    Intent registered = new Intent(getApplicationContext(), Registered.class);

                    // Close all views before launching Registered Screen
                    registered.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    pDialog.dismiss();
                    startActivity(registered);
                    finish();
                } else if (Integer.parseInt(red) == 2) {
                    pDialog.dismiss();
                    registerErrorMsg.setText("User already exists");
                } else if (Integer.parseInt(red) == 3) {
                    pDialog.dismiss();
                    registerErrorMsg.setText("Invalid Email id");
                } else {
                    pDialog.dismiss();
                    registerErrorMsg.setText("Error occurred in registration");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

public void NetAsync(View view){
    new NetCheck().execute();
}

logcat的:

09-04 15:07:53.245    1995-1995/com.knedl.testloginapp E/error﹕ Comes to Here
09-04 15:07:53.245    1995-1995/com.knedl.testloginapp D/AndroidRuntime﹕ Shutting down VM
09-04 15:07:53.245    1995-1995/com.knedl.testloginapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2cc1b20)
09-04 15:07:53.245    1995-1995/com.knedl.testloginapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.knedl.testloginapp, PID: 1995
java.lang.NullPointerException
        at com.knedl.testloginapp.Register$ProcessRegister.onPostExecute(Register.java:181)
        at com.knedl.testloginapp.Register$ProcessRegister.onPostExecute(Register.java:149)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

抱歉,我是新来的。我从这里得到了json JSONObject:

  private class ProcessRegister extends AsyncTask<String, String, JSONObject> {
    // Defining Process dialog
    private ProgressDialog pDialog;
    String email, password, fname, lname, uname;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        fname = inputFirstName.getText().toString();
        lname = inputLastName.getText().toString();
        email = inputEmail.getText().toString();
        uname = inputUsername.getText().toString();
        password = inputPassword.getText().toString();
        pDialog = new ProgressDialog(Register.this);
        pDialog.setTitle("Contacting Servers");
        pDialog.setMessage("Registering ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {
        UserFunctions userFunctions = new UserFunctions();
        JSONObject json = userFunctions.registerUser(fname, lname, email, uname, password);
        return json;
    }

这是来自userFunctions库的方法registerUser。当我进行调试时,我得到空的json:

 public JSONObject registerUser(String fname, String lname, String email, String uname, String password){
    // Building Parameters
    List params = new ArrayList();
    params.add(new BasicNameValuePair("tag", register_tag));
    params.add(new BasicNameValuePair("fname", fname));
    params.add(new BasicNameValuePair("lname", lname));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("uname", uname));
    params.add(new BasicNameValuePair("password", password));
    JSONObject json = jsonParser.getJSONFromUrl(registerURL,params);
    return json;
}

1 个答案:

答案 0 :(得分:0)

如果这是NullPointerException所在的行:

if (json.getString(KEY_SUCCESS) != null) {

最有可能是您的json参数传递给您的方法

protected void onPostExecute(JSONObject json)

为空。如果是这种情况,则声明

json.getString(KEY_SUCCESS)

将抛出NullPointerException,因为您正在尝试访问null对象的方法。

设置断点,调用此方法,并确保传递给它的值不为空。