我的Android应用程序从servlet接收数据作为JSON字符串。该字符串是有效的JSON字符串。我在servlet端使用完全相同的ProfileBean类,并运行gson.fromJson()来反序列化字符串并将bean存储在db中,这很好。由于某些原因在android方面,调用gson.fromJson()会导致空指针异常。
protected ProfileBean doInBackground(ProfileBean... params) {
...
InputStream is = httpConn.getInputStream();
DataConversion dc = new DataConversion();
String jsonString = dc.convertStreamToString2(is);
is.close();
Log.d(jsString, jsonString.substring(0, 200));
Log.d(jsString, jsonString.substring(jsonString.length() - 200, jsonString.length()));
Log.d(jsString, String.valueOf(jsonString.length()) );
pfb = gson.fromJson(jsonString, ProfileBean.class);
...
}
这是pub的ProfileBean类:
public class ProfileBean implements Serializable{
String name;
String email;
String homeCity;
String password;
String img;
/**
* Takes 5 String parameters. Representing the data of a new user.
* @param name
* @param email
* @param homeCity
* @param password
* @param img
*/
public ProfileBean(String name, String email, String homeCity, String password, String img){
this.name = name;
this.email=email;
this.homeCity=homeCity;
this.password=password;
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHomeCity() {
return homeCity;
}
public void setHomeCity(String homeCity) {
this.homeCity = homeCity;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
}
我整个上午一直在努力解决这个问题,检查了Async任务,Gson和其他类似堆栈溢出问题的文档。我认为问题出在ProfileBean类中,但如上所述,它适用于Servlet端。我被困了,任何帮助都会非常感激。
这是日志消息:
04-02 14:09:42.490 25449-25449/com.danielsapps.packbuddy W/System.err: java.util.concurrent.ExecutionException: java.lang.NullPointerException
04-02 14:09:42.525 25449-26309/com.danielsapps.packbuddy W/dalvikvm: threadid=13: thread exiting with uncaught exception (group=0x411932a0)
04-02 14:09:42.560 25449-25449/com.danielsapps.packbuddy W/System.err: at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:223)
04-02 14:09:42.560 25449-25449/com.danielsapps.packbuddy W/System.err: at java.util.concurrent.FutureTask.get(FutureTask.java:82)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at android.os.AsyncTask.get(AsyncTask.java:482)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at com.danielsapps.packbuddy.HostelSearch.searchForHostelsAndSetUiData(HostelSearch.java:47)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at android.view.View.performClick(View.java:4232)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at android.view.View$PerformClick.run(View.java:17298)
04-02 14:09:42.565 25449-25449/com.danielsapps.packbuddy W/System.err: at android.os.Handler.handleCallback(Handler.java:615)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at android.os.Looper.loop(Looper.java:137)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4921)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: at dalvik.system.NativeStart.main(Native Method)
04-02 14:09:42.570 25449-25449/com.danielsapps.packbuddy W/System.err: Caused by: java.lang.NullPointerException
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at com.danielsapps.packbuddycontroller.HostelSearchController.doInBackground(HostelSearchController.java:81)
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at com.danielsapps.packbuddycontroller.HostelSearchController.doInBackground(HostelSearchController.java:32)
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-02 14:09:42.575 25449-25449/com.danielsapps.packbuddy W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-02 14:09:42.580 25449-25449/com.danielsapps.packbuddy W/System.err: at java.lang.Thread.run(Thread.java:856)
04-02 14:09:42.690 25449-26309/com.danielsapps.packbuddy E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.danielsapps.packbuddycontroller.HostelSearchController.doInBackground(HostelSearchController.java:81)
at com.danielsapps.packbuddycontroller.HostelSearchController.doInBackground(HostelSearchController.java:32)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)