Из this tutorial, я получил эту ошибкуJSON: Исключение нулевого указателя
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
Он пришел из MainActivity.java
class AttemptLogin extends AsyncTask<String, String, String> { // <--- cause of error
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Attempting for login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show(); }
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// here Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
// checking log for json response
Log.d("Login attempt", json.toString()); // <--- cause of error
// success tag for json
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successfully Login!", json.toString());
Intent ii = new Intent(MainActivity.this,OtherActivity.class);
startActivity(ii);
// this finish() method is used to tell android os that we are done with current
// activity now! Moving to other activity
finish();
return json.getString(TAG_MESSAGE);
}
else{
return json.getString(TAG_MESSAGE);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* Once the background process is done we need to Dismiss the progress dialog asap
**/
protected void onPostExecute(String message) {
pDialog.dismiss();
if (message != null){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
}
}
Там уже много людей задают тот же вопрос, но до сих пор нет ответа для этой конкретной проблемы.
Это мой первый раз, когда я использую JSON, поэтому, возможно, что-то не так в коде, и я его еще не нашел.
Надеюсь, что кто-то поможет мне разобраться, где я сделал неправильно. Спасибо.
Вы должны проверить, почему это jsonParser.makeHttpRequest (LOGIN_URL, "POST", PARAMS); возвращает NULL. –
Я пытаюсь понять, почему – August
является вызовом 'makeHttpRequest' на нити ui? –