[英](Application crashes) Null pointer exception in return …when a method of public class is called from a Serilizable class
因此,當我從Controller類到模型類調用一個方法(應該返回一個String)時,它會給出null指針,因為Model類中的方法即使填充了它也會返回null變量。
public class HumLogController implements Serializable {
private transient HumLogModel humLogModel;
public String logIn (String username, String password ){
this.humLogModel = new HumLogModel();
// The line below is line 52. checkUser(username) is giving null pointer because Model class method is returning a null variable (I think).
if (checkUser(username).equalsIgnoreCase("correct")){
this.username = username;
if(checkPassword(password , username).equalsIgnoreCase("correct")){
this.password = password;
return "success";
}
else{
return "Incorrect password";
}
}
else {
return "User do not exist";
}
}
private String checkUser(String username){
return humLogModel.checkUser(username);
}
}
/** So when the checkUser(string method) call the checkUser method, it returns null,
because the checkUser(username) method in the HumLogModel class is returning null. */
HumLogModel類是...
public class HumLogModel extends Application {
private ParseObject customer;
private ParseObject tradesman;
private ParseObject user;
public String userSuccess ;
public String passwordSuccess;
public String checkUser(final String username) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.whereEqualTo("username", username);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
// The value in line below is assigned in the variable.
// but it is not returned (I do not why :-( ) can anyone help :-( because I can not proceed without it.
HumLogModel.this.userSuccess = "incorrect";
} else {
Log.d("score", "The getFirst request is success");
HumLogModel.this.userSuccess = "correct";
}
}
});
return userSuccess;
// return "correct";
// if the method returns a hardcoded string "correct" directly everything works fine. but when it returns a variable userSuccess , it returns null...!!!! and I get null pointer.
}
}
日志貓如下
08-04 00:12:07.260 1992-1992/com.example.praduman.humlog D/dalvikvm﹕ Late-enabling CheckJNI
08-04 00:12:07.472 1992-2007/com.example.praduman.humlog D/dalvikvm﹕ GC_FOR_ALLOC freed 312K, 4% free 8997K/9336K, paused 8ms, total 11ms
08-04 00:12:07.476 1992-2007/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.database.Cursor.getNotificationUri, referenced from method com.parse.ParseSQLiteCursor.getNotificationUri
08-04 00:12:07.476 1992-2007/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve interface method 491: Landroid/database/Cursor;.getNotificationUri ()Landroid/net/Uri;
08-04 00:12:07.476 1992-2007/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
08-04 00:12:07.776 1992-1992/com.example.praduman.humlog D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
08-04 00:12:07.776 1992-1992/com.example.praduman.humlog D/﹕ HostConnection::get() New Host Connection established 0xb9827de0, tid 1992
08-04 00:12:07.792 1992-1992/com.example.praduman.humlog D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
08-04 00:12:07.792 1992-1992/com.example.praduman.humlog D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
08-04 00:12:07.848 1992-1992/com.example.praduman.humlog W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
08-04 00:12:07.848 1992-1992/com.example.praduman.humlog E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
08-04 00:12:07.848 1992-1992/com.example.praduman.humlog E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-04 00:12:07.848 1992-1992/com.example.praduman.humlog D/OpenGLRenderer﹕ Enabling debug mode 0
08-04 00:12:07.852 1992-1992/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-04 00:12:07.852 1992-1992/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 443: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-04 00:12:07.852 1992-1992/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 465: Landroid/content/res/TypedArray;.getType (I)I
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 406: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-04 00:12:07.856 1992-1992/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-04 00:12:07.896 1992-1992/com.example.praduman.humlog W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
08-04 00:12:22.788 1992-1992/com.example.praduman.humlog D/AndroidRuntime﹕ Shutting down VM
08-04 00:12:22.788 1992-1992/com.example.praduman.humlog W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c26648)
08-04 00:12:22.788 1992-1992/com.example.praduman.humlog E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.praduman.humlog.HumLogController.logIn(HumLogController.java:52)
at com.example.praduman.humlog.LogInActivity$1.onClick(LogInActivity.java:57)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
08-04 00:12:23.084 1992-2132/com.example.praduman.humlog D/dalvikvm﹕ GC_FOR_ALLOC freed 325K, 4% free 9184K/9536K, paused 14ms, total 14ms
/**below is the code and log when the method returns hardcoded string "correct" not the
variable */
public String checkUser(final String username) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.whereEqualTo("username", username);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
HumLogModel.this.userSuccess = "incorrect";
} else {
Log.d("score", "The getFirst request is success");
HumLogModel.this.userSuccess = "correct";
Log.d("After assigning value" , userSuccess);
}
}
});
// return userSuccess;
return "correct";
}
the log related to this method is given below
com.example.praduman.humlog D/score﹕ The getFirst request is success
08-04 01:05:19.240 24736-24736/com.example.praduman.humlog D/After assigning value﹕ correct
您是否嘗試過將query.getFirstInBackground(...)
替換為query.getFirst(...)
? 我可能會誤解這個問題,但是看起來它可能正在觸發后台線程進行處理,然后返回null(未設置變量),然后后台請求完成,但是為時已晚,並且發生null指針異常。
我在日志HumLogModel.checkUser()
的調試日志條目。 我的猜測是該方法在執行回調以設置userSuccess
的值之前userSuccess
。 在檢查logIn方法中的值之前,您需要等待回調返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.