我制作了一个应用程序,通过使用Web服务(在PHP中)访问网络上的数据。在onPostExecute()方法中,应用程序崩溃,因为结果为null并抛出空指针异常。
我知道有很多类似的问题,但我仍然没有得到它。请帮帮我!谢谢!
代码
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) { // error**
// Do JSON Parsing
}else{
//Show Warning
}
}
LogCat: -
11-09 16:26:43.279: I/System.out(19358): Result =
11-09 16:26:43.279: I/System.out(19358): null
11-09 16:26:43.289: W/System.err(19358): org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray
11-09 16:26:43.289: W/System.err(19358): at org.json.JSON.typeMismatch(JSON.java:111)
11-09 16:26:43.289: W/System.err(19358): at org.json.JSONArray.<init>(JSONArray.java:91)
11-09 16:26:43.289: W/System.err(19358): at org.json.JSONArray.<init>(JSONArray.java:103)
11-09 16:26:43.289: W/System.err(19358): at com.MyDreamAuto.Login$AppTask.onPostExecute(Login.java:452)
11-09 16:26:43.289: W/System.err(19358): at com.MyDreamAuto.Login$AppTask.onPostExecute(Login.java:1)
11-09 16:26:43.289: W/System.err(19358): at android.os.AsyncTask.finish(AsyncTask.java:631)
11-09 16:26:43.289: W/System.err(19358): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-09 16:26:43.289: W/System.err(19358): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-09 16:26:43.289: W/System.err(19358): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 16:26:43.289: W/System.err(19358): at android.os.Looper.loop(Looper.java:137)
11-09 16:26:43.289: W/System.err(19358): at android.app.ActivityThread.main(ActivityThread.java:4935)
11-09 16:26:43.289: W/System.err(19358): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:26:43.289: W/System.err(19358): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:26:43.299: W/System.err(19358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-09 16:26:43.299: W/System.err(19358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-09 16:26:43.299: W/System.err(19358): at dalvik.system.NativeStart.main(Native Method)
11-09 16:26:43.399: D/AndroidRuntime(19358): Shutting down VM
11-09 16:26:43.399: W/dalvikvm(19358): threadid=1: thread exiting with uncaught exception (group=0x4106a2a0)
11-09 16:26:43.409: E/AndroidRuntime(19358): FATAL EXCEPTION: main
11-09 16:26:43.409: E/AndroidRuntime(19358): java.lang.NullPointerException
11-09 16:26:43.409: E/AndroidRuntime(19358): at com.Auto.Login$AppTask.onPostExecute(Login.java:489)
11-09 16:26:43.409: E/AndroidRuntime(19358): at com.Auto.Login$AppTask.onPostExecute(Login.java:1)
11-09 16:26:43.409: E/AndroidRuntime(19358): at android.os.AsyncTask.finish(AsyncTask.java:631)
11-09 16:26:43.409: E/AndroidRuntime(19358): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-09 16:26:43.409: E/AndroidRuntime(19358): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-09 16:26:43.409: E/AndroidRuntime(19358): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 16:26:43.409: E/AndroidRuntime(19358): at android.os.Looper.loop(Looper.java:137)
11-09 16:26:43.409: E/AndroidRuntime(19358): at android.app.ActivityThread.main(ActivityThread.java:4935)
11-09 16:26:43.409: E/AndroidRuntime(19358): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:26:43.409: E/AndroidRuntime(19358): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:26:43.409: E/AndroidRuntime(19358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-09 16:26:43.409: E/AndroidRuntime(19358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-09 16:26:43.409: E/AndroidRuntime(19358): at dalvik.system.NativeStart.main(Native Method)
编辑: - 整个代码
public class AppTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
if (forgt == 1) {
if (result.contains("")) {
t5.setVisibility(View.VISIBLE);
et_f.setText("");
// dialog.dismiss();
} else {
Toast.makeText(Login.this, "Mail Sent",
Toast.LENGTH_LONG).show();
myDialog.dismiss();
// dialog.dismiss();
forgt = 0;
}
} else {
// dialog.dismiss();
// Toast.makeText(Login.this, "Mail sent..!", 1000).show();
System.out.println("Result = " + result);
if (result.equalsIgnoreCase("null")) {
t4.setVisibility(View.VISIBLE);
et2.setText("");
} else {
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
s_id = jObject.getString("seller_id");
usr = jObject.getString("email");
pwd = jObject.getString("password");
type = jObject.getString("seller_Type");
code = jObject.getString("verification_Code");
}
} catch (JSONException e) {
e.printStackTrace();
}
Editor edit = sp.edit();
edit.putString("logID", s_id);
edit.commit();
Editor edit2 = sp1.edit();
edit2.putString("logPass", pwd);
edit2.commit();
Editor edit3 = sp2.edit();
edit3.putString("logEmail", usr);
edit3.commit();
Editor edit4 = sp3.edit();
edit4.putString("logType", type);
edit4.commit();
String ty = sp1.getString("logType", "");
Toast.makeText(getApplicationContext(), "Type: " + ty,
Toast.LENGTH_LONG).show();
if (code.contains("Verified")) {
V = new view_cars();
V.SetId(s_id);
Intent myIntent = new Intent(Login.this,
UserActivity.class);
startActivity(myIntent);
}
else {
// dialog.dismiss();
// Toast.makeText(getApplicationContext(),
// "NOT VERIFIED",
// Toast.LENGTH_LONG).show();
myDialog = new Dialog(Login.this);
myDialog.setContentView(R.layout.ask_pin);
myDialog.setTitle("Confirmation");
myDialog.setCancelable(true);
// for OK
Button ok = (Button) myDialog
.findViewById(R.id.button1);
et_pin = (EditText) myDialog
.findViewById(R.id.editText1);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pin = et_pin.getText().toString();
Toast.makeText(getApplicationContext(),
"CLICKED OK= " + pin,
Toast.LENGTH_LONG).show();
if (pin.contains("null")) {
Toast.makeText(getApplicationContext(),
"Please enter the Pin",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
getApplicationContext(),
"email,pass,pin= " + str1 + ","
+ str2 + "," + pin,
Toast.LENGTH_LONG).show();
new App_pin_Task()
.execute(FILENAME_pin);
}
}
});
Button resend = (Button) myDialog
.findViewById(R.id.button2);
resend.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new App_resend_Task()
.execute(FILENAME_resend);
}
});
Button cancel = (Button) myDialog
.findViewById(R.id.button3);
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"CLICKED CANCEL", Toast.LENGTH_LONG)
.show();
et2.setText("");
myDialog.dismiss();
// Intent intent = new
// Intent(Dealer_details.this,
// Login.class);
// startActivity(intent);
}
});
myDialog.show();
}
}
}
} else {
Toast.makeText(getApplicationContext(),
"Network Congestion! Please try again later!",
Toast.LENGTH_LONG).show();
}
dialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... params) {
// String is = null;
if (forgt == 1) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(FILENAME_f);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("email", email));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
}
} else {
str1 = et1.getText().toString();
str2 = et2.getText().toString();
if (str1.length() > 0 && str2.length() > 0) {
A = str1;
B = str2;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://animsinc.com/login.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("username",
str1));
nameValuePairs.add(new BasicNameValuePair("password",
str2));
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
return is;
}
}
答案 0 :(得分:1)
11-09 16:26:43.289: W/System.err(19358): at com.MyDreamAuto.Login$AppTask.onPostExecute(Login.java:452)
该行的内容是什么? 您发布的代码有225行。
您可能正在使用View
(setText()
或其他)但未找到它。
答案 1 :(得分:1)
我认为当结果为空时,问题可能是新的JSONArray(结果)。
http://www.json.org/javadoc/org/json/JSONArray.html#JSONArray(java.lang.String)
你可能想要的是!result.isEmpty()而不是equalsIgnoreCase(“null”)。
答案 2 :(得分:0)
我怀疑你是否在注释行中添加了以下语句
//String is = null
那么变量如何存储值???
答案 3 :(得分:0)
根据您发布的内容,您似乎认为
result != null
与
相同!result.contains(null)
不是。后者可以抛出NPE。
编辑你正在'结果'处理非结果,即使你在'else'分支中知道它是null。你的代码真的没有多大意义。