如何修复java.lang.String org.json.JSONObject.toString()'在空对象


How to Fix java.lang.String org.json.JSONObject.toString()' on a null object reference?

我的android模拟器,当我试图运行一个登录活动时,抛出一个空对象引用。

一旦到达以下行,它就会这样做:

 // check your log for json response
            Log.d("Login attempt", json.toString());

完整的login.java代码:

public class login extends Activity implements View.OnClickListener {
    private AnimatedGifImageView animatedGifImageView;
    public EditText user, pass;
    private Button mSubmit, mRegister;
    // Progress Dialog
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    //php login script location:
    //localhost :
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
    // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
    //testing on Emulator:
    private static final String LOGIN_URL = "http://www.samplephppage.com";
    //testing from a real server:
    //private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";
    //JSON element ids from repsonse of php script:
    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.activity_login);
        user = (EditText)findViewById(R.id.etUsername);
        pass = (EditText)findViewById(R.id.etPassword);
        animatedGifImageView = ((AnimatedGifImageView) findViewById(R.id.animatedGifImageView));
        animatedGifImageView.setAnimatedGif(R.raw.animated_gif_big, TYPE.AS_IS);

        //setup input fields
        //setup buttons
        mSubmit = (Button) findViewById(R.id.btnLogin);
        //register listeners
        mSubmit.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btnLogin:
                new AttemptLogin().execute();
                break;
            case R.id.textView6:
                Intent i = new Intent(this, forgotpassword.class);
                startActivity(i);
                break;
            default:
                break;
        }
    }

    class AttemptLogin extends AsyncTask<String, String, String> {
        /**
         * Before starting background thread Show Progress Dialog
         * */
        boolean failure = false;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(login.this);
            pDialog.setMessage("Checking Credentials...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;
            String etUsername = user.getText().toString();
            String etPassword = pass.getText().toString();

            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<>();
                params.add(new BasicNameValuePair("username", etUsername));
                params.add(new BasicNameValuePair("password", etPassword));
                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);
                // check your log for json response
                **Log.d("Login attempt", json.toString());** <--- **Crashes Here**
                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    Intent i = new Intent(login.this, LoginLoading.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
下面是来自php页面 的代码
   <?php
if($count == 1){
    $response['success'] = 1;
    $response['message'] = "Login Successful"
    die(json_encode($response));
    }
    ?>

我似乎可以弄明白为什么它给我这个异常了。

只需检查您的web服务实现是否通过在任何浏览器中单击LOGIN_URL以JSON形式给出响应。如果出现响应,那么也可以在应用程序中尝试,否则检查web服务中的错误

确保您的登录URL +参数具有逻辑意义。在代码中注销它,并在浏览器中执行post请求,以查看请求是否实际上返回了所需的JSON。

因为:

private static final String LOGIN_URL = "my php page";

不是一个有效的URL。

请阅读该行上方的评论,以获取有关您的LOGIN_URL应该分配给什么的建议。