dongye4192 2014-09-04 19:14
浏览 69
已采纳

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

I'm working on connecting mobile app and server Apache. I tried debug and solved all the misspelling, I think. This is my code, please help:

    @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)

Sorry i'm new here. I get the json JSONObject from here:

  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;
    }

And this is method registerUser from userFunctions library. I get empty json when I go through debug:

 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条回答 默认 最新

  • doumi4676 2014-09-04 19:34
    关注

    If this is the line where your NullPointerException is:

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

    Most probably your json parameter passed to your method

    protected void onPostExecute(JSONObject json)
    

    is null. If this is the case, the statement

    json.getString(KEY_SUCCESS)
    

    will throw a NullPointerException since you are trying to access the method of a null object.

    Set a breakpoint where you call this method, and make sure that the value you pass to it is not null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!