在检查空值时 onPostExecute 中的 AsyncTask 中的空指针异常
Posted
技术标签:
【中文标题】在检查空值时 onPostExecute 中的 AsyncTask 中的空指针异常【英文标题】:Null Pointer exception in AsyncTask in onPostExecute while checking for null value 【发布时间】:2013-11-21 09:42:34 【问题描述】:我制作了一个应用程序,它通过使用 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;
【问题讨论】:
您必须提供更多 onPostExecute 方法的代码。 (Login.java:423) 需要onPostExecute()方法代码.. 我已经编辑了这个问题。我知道代码太大,但请看一下。 尝试检查结果字符串的长度。 if(result.length() > 0) 【参考方案1】:11-09 16:26:43.289: W/System.err(19358): at com.MyDreamAuto.Login$AppTask.onPostExecute(Login.java:452)
那条线是什么? 您发布的代码有 225 行。
您可能正在使用View
(setText()
等),但找不到它。
【讨论】:
【参考方案2】:我认为问题可能是结果为空时的新 JSONArray(result)。
http://www.json.org/javadoc/org/json/JSONArray.html#JSONArray(java.lang.String)
你可能想要的是 !result.isEmpty() 而不是 equalsIgnoreCase("null")。
【讨论】:
【参考方案3】:我怀疑您是否已将以下语句放在注释行中
//String is = null
那么变量是如何存储值的???
【讨论】:
【参考方案4】:根据您发布的内容,您似乎认为
result != null
与
相同!result.contains(null)
不是。后者可以抛出 NPE。
编辑你正在处理'结果'是非空的,即使你知道它是空的,在'else'分支中。你的代码真的没有多大意义。
【讨论】:
以上是关于在检查空值时 onPostExecute 中的 AsyncTask 中的空指针异常的主要内容,如果未能解决你的问题,请参考以下文章