无法从数据库中获取数据用户

Can't get data User from databese

我用Ormlite 来使用database.I 写函数search User by firebaseId。 此函数获取有关用户的信息:全名、电子邮件、照片。但是当去执行配置文件 activity 我的 apliaction 停止

ProfileActivity.class

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ButterKnife.bind(this);

        dbHelper = new DbHelper(this);

        Application application = (Application) getApplicationContext();
        userFirebaseId = application.getFirebaseId();
//show line error 
        userMy = dbHelper.getByUser(userFirebaseId);

        nameEditText.setText(userMy.getFullName());
        emailEditText.setText(userMy.getEmail());


        auth = FirebaseAuth.getInstance();
        user = FirebaseAuth.getInstance().getCurrentUser();
        authListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (user == null) {
                    startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
                    finish();
                }
            }
        };
    }

DbHepler.class

public class DbHelper 扩展 OrmLiteSqliteOpenHelper { PreparedQuery pq;

    Dao<User, ?> userDao=null;
    Dao<MyDream, ?> myDreamDao = null;

    public static final String DB_NAME = "dream.db";
    public static final int DB_VERSION = 1;

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {

        try {
            TableUtils.createTable(connectionSource, Dream.class);
            TableUtils.createTable(connectionSource, User.class);
            TableUtils.createTable(connectionSource, MyDream.class);
            Dao<Dream, Integer> dreamDao = getDao(Dream.class);
            userDao = getDao(User.class);
            myDreamDao = getDao(MyDream.class);
             } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, Dream.class, true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public User getByUser(String firebaseId) {
// this error line 
        QueryBuilder<User, ?> qb = userDao.queryBuilder();
        try {
            qb.where().eq(User.Columns.FIREBASE_ID, firebaseId);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        try {
            pq = qb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        try {
            return userDao.queryForFirst(pq);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    }

我的日志

FATAL EXCEPTION: main
Process: magdalena.pl.dreamguide, PID: 11813
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{magdalena.pl.dreamguide/magdalena.pl.dreamguide.activity.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'com.j256.ormlite.stmt.QueryBuilder com.j256.ormlite.dao.Dao.queryBuilder()' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'com.j256.ormlite.stmt.QueryBuilder com.j256.ormlite.dao.Dao.queryBuilder()' on a null object reference
                                                                         at magdalena.pl.dreamguide.db.DbHelper.getByUser(DbHelper.java:460)
                                                                         at magdalena.pl.dreamguide.activity.ProfileActivity.onCreate(ProfileActivity.java:99)
                                                                         at android.app.Activity.performCreate(Activity.java:6251)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

您在 onCreate 方法中初始化 userDao。仅当按照说明 here 创建数据库时才调用此方法。因此,初始化 userDao 的代码应该移到更接近它的使用位置,或者在它使用之前执行它的地方。