我从数据库服务器接收数据并将它们放在相关的TextView中。但有时我的应用程序崩溃了!并且错误是java.lang.NullPointerException。 有趣的是这是正确的..例外并不总是出现,但很少。所以我认为这可能与互联网连接的下降有关,但我并不完全确定。有什么建议吗?
public class fetchContactDataAsyncTask extends AsyncTask<Void, Void, Contact> {
Contact contact;
GetContactCallback contactCallBack;
public fetchContactDataAsyncTask(Contact contact, GetContactCallback callBack) {
this.contact = contact;
this.contactCallBack = callBack;
}
@Override
protected Contact doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", contact.username));
dataToSend.add(new BasicNameValuePair("password", contact.password));
dataToSend.add(new BasicNameValuePair("name", contact.name));
dataToSend.add(new BasicNameValuePair("surname", contact.surname));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");
Contact returnedContact = null;
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);;
if (jObject.length() == 0){
returnedContact = null;
}else {
String phone = jObject.getString("phone");
String email = jObject.getString("email");
String address = jObject.getString("address");
String notes = jObject.getString("notes");
returnedContact = new Contact(contact.username, contact.password, contact.name, contact.surname, phone, email, address, notes);
}
}catch (Exception e){
e.printStackTrace();
}
return returnedContact;
}
@Override
protected void onPostExecute(Contact returnedContact) {
progressDialog.dismiss();
contactCallBack.done(returnedContact);
super.onPostExecute(returnedContact);
}
}
那是我的错误日志
12-02 18:41:04.849 14145-14145/ci.giacomo.net.mycontactpro4 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: cirillo.giacomo.net.mycontactpro4, PID: 14145
java.lang.NullPointerException: Attempt to read from field 'java.lang.String cirillo.giacomo.net.mycontactpro4.Contact.name' on a null object reference
at cirillo.giacomo.net.mycontactpro4.ContactDetTextView$1.done(ContactDetTextView.java:115)
at cirillo.giacomo.net.mycontactpro4.ServerRequests$fetchContactDataAsyncTask.onPostExecute(ServerRequests.java:372)
at cirillo.giacomo.net.mycontactpro4.ServerRequests$fetchContactDataAsyncTask.onPostExecute(ServerRequests.java:312)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)