java.lang.NullPointerException和JSON

时间:2013-10-14 15:37:15

标签: java android json nullpointerexception

     // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    Log.d("DEBUG", "jSON");
                      loginErrorMsg.setText("");

                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                        // user successfully logged in
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous data in database
                        UserFunctions.logoutUser(getApplicationContext());
                        db.addSubscriber( json.getString(KEY_RegIDSubscribers), json_user.getString(KEY_EMAIL),json_user.getString(KEY_NAME), json_user.getString(KEY_Password), json_user.getString(KEY_Phone),json_user.getString(KEY_University),json_user.getString(KEY_Major), json.getString(KEY_GcmLocations));                        

                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);

                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);

                        // Close Login Screen
                        finish();
                    }else{
                        // Error in login
                        loginErrorMsg.setText("Incorrect username/password");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

我在启动“try”行时出错,错误是java.lang.NullPointerException,我也得到java.lang.String无法转换为JSONObject,我真的不知道如何修复他们,请帮忙吗?

1 个答案:

答案 0 :(得分:0)

没有堆栈跟踪很难说,但我猜测loginErrorMsgnull。无论在何处检索视图,您都不能初始化它,也不能将错误的viewId传递给findViewById方法。