有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java(应用程序崩溃)返回空指针异常。。。当从可序列化类调用公共类的方法时

所以,当我从控制器类调用一个方法到模型类(应该返回一个字符串)时,它会给出空指针,因为模型类中的方法 返回空变量,即使已填充

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 安卓.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: L安卓/database/Cursor;.getNotificationUri ()L安卓/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 安卓.content.res.TypedArray.getChangingConfigurations, referenced from method 安卓.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: L安卓/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 安卓.content.res.TypedArray.getType, referenced from method 安卓.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: L安卓/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 安卓.content.res.Resources.getDrawable, referenced from method 安卓.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: L安卓/content/res/Resources;.getDrawable (IL安卓/content/res/Resources$Theme;)L安卓/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 安卓.content.res.Resources.getDrawableForDensity, referenced from method 安卓.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: L安卓/content/res/Resources;.getDrawableForDensity (IIL安卓/content/res/Resources$Theme;)L安卓/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 安卓.view.View.performClick(View.java:4240)
                at 安卓.view.View$PerformClick.run(View.java:17721)
                at 安卓.os.Handler.handleCallback(Handler.java:730)
                at 安卓.os.Handler.dispatchMessage(Handler.java:92)
                at 安卓.os.Looper.loop(Looper.java:137)
                at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                at com.安卓.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

共 (1) 个答案

  1. # 1 楼答案

    你试过用query.getFirst(...)替换query.getFirstInBackground(...)吗?我可能误解了这个问题,但看起来可能是后台线程停止运行,然后返回null(unset变量),然后后台请求完成,但为时已晚,出现了null指针异常