java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int org.json.JSONObject.getInt(java.lang.String)'

时间:2017-04-18 11:11:45

标签: java php android json nullpointerexception

我正在尝试send data from Android app to Raspberry pi from this link。 我已经执行了它中提到的所有步骤,但是当我尝试运行应用程序时,它在android studio logcat中给出了一个错误。

错误如下:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int org.json.JSONObject.getInt(java.lang.String)'。

有错误的行是102& 80这是

第80行: - > int success = json.getInt(“success”);

第102行: - > if(success == 1)

这是我的Android Studio代码。我在MainActivity.java类中遇到错误。:

MainActivity.java

public class MainActivity extends ActionBarActivity {

private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();

EditText editText;

@Override
protected void onDestroy() {
    try {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onDestroy();
}

EditText editText2;
String String_name;
String Int_Part;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.Name);
    editText2 = (EditText) findViewById(R.id.part_nr);
    String_name = editText.getText().toString();
    Int_Part = editText2.getText().toString();
}


public void Send(View view) {
    new Create_Part().execute();
}

class Create_Part extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Sending part to the database...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {


        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("Name", String_name));
        params.add(new BasicNameValuePair("part_nr", Int_Part));

        JSONObject json = jsonParser.makeHttpRequest("192.168.0.190/db_create.php.php", "POST", params);

        try {
            int success = json.getInt("success");

            if(success == 1){
                finish();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url){
        pDialog.dismiss();
    }
}
}

Logcat错误:

04-18 16:26:11.493 2818-3010/com.example.androidphp2 E/AndroidRuntime: FATAL 
EXCEPTION: AsyncTask #4
         Process: com.example.androidphp2, PID: 2818
         java.lang.RuntimeException: An error occurred while executing doInBackground()                                                
         at android.os.AsyncTask$3.done(AsyncTask.java:309)
         at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
         at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
         at java.util.concurrent.FutureTask.run(FutureTask.java:242)
         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
         at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference
         at com.example.androidphp2.MainActivity$Create_Part.doInBackground(MainActivity.java:102)
         at com.example.androidphp2.MainActivity$Create_Part.doInBackground(MainActivity.java:80)
         at android.os.AsyncTask$2.call(AsyncTask.java:295)
         at java.util.concurrent.FutureTask.run(FutureTask.java:237)
         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
         at java.lang.Thread.run(Thread.java:818) 

1 个答案:

答案 0 :(得分:0)

尝试这样:

int success = Integer.parseInt(json.getString("success"));

希望它能运作