简体   繁体   中英

Crash while trying to get firebase realtime database value

I have crash while trying to get Data from firebase real time database. I'm trying to make something like force update. Once the application is starting, I'm checking if the current installed app version is equals to value in firebase (see image below), and if is not equal , I'm sent the user to play market to update. But the app crashes with log (See below) I have value in firebase real time database that looks like this :

在此处输入图片说明

And here is the code:

public class AppUpdater {

        private Activity mActivity;
        private String userCurrentVersion = null;
        private String currentVersion = null;

        private FirebaseDatabase mDB = FirebaseDatabase.getInstance();
        private DatabaseReference appData = mDB.getReference("AppData");

        public AppUpdater(Activity mActivity) {
            this.mActivity = mActivity;
        }

        public boolean hasNewVersion() {
            getCurrentVersion();
            if (currentVersion.equals(userCurrentVersion())) {
                return true;
            } else {
                return false;
            }
        }

        private String userCurrentVersion() {
            try {
                PackageInfo pInfo = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0);
                userCurrentVersion = pInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return userCurrentVersion;
        }

        private void getCurrentVersion() {
            getVersionFromFireBase(appData, new OnDataRetrieveListener() {
                @Override
                public void onSuccess(DataSnapshot dataSnapshot) {
                    Log.d("getCurrentVersion", "onSuccess: ");
                    currentVersion = dataSnapshot.child("AppVersion").getValue(String.class);
                }

                @Override
                public void onStart() {
                    Log.d("getCurrentVersion", "onStart: ");

                }
                @Override
                public void onFailure() {
                    Log.d("getCurrentVersion", "onFailure: ");
                }
            });
        }
        private void getVersionFromFireBase(DatabaseReference appData, final OnDataRetrieveListener listener) {
            listener.onStart();
            appData.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    dataSnapshot.child("AppVersion").getValue();
                    listener.onSuccess(dataSnapshot);
                }
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    listener.onFailure();
                }
            });
        }
    }

in my onCreate method in activity I called method:

private void checkForUpdate() {
        mUpdater = new AppUpdater(this);
        isUpdateRequired = mUpdater.hasNewVersion();
        Log.d("isUpdateRequired", "checkForUpdate: "+isUpdateRequired);
        if (isUpdateRequired) {
            //TODO open google play market to update
        }
    }

Application crash with log:

Process: com.enjoyapps.easywedd, PID: 17268
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.enjoyapps.easywedd/com.enjoyapps.easywedd.ui.activities.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6981)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.enjoyapps.easywedd.AppUtils.AppUpdater.hasNewVersion(AppUpdater.java:32)
        at com.enjoyapps.easywedd.ui.activities.LoginActivity.checkForUpdate(LoginActivity.java:28)
        at com.enjoyapps.easywedd.ui.activities.LoginActivity.onCreate(LoginActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:7326)
        at android.app.Activity.performCreate(Activity.java:7317)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)

You are getting a NullPointerException in this method:

 public boolean hasNewVersion() {
            getCurrentVersion();
            if (currentVersion.equals(userCurrentVersion())) {
                return true;
            } else {
                return false;
            }
        }

In this code, you are using currentVersion.equals, here you are gettign nullpointerexception because you are using the object currentVersion which has a null value initialized in your code like:

private String currentVersion = null;

Get the currentVersion in the variable and then use the .equals() to perform a check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM