0

My app is currently trying to connect a user and customer and here I am trying to do this via Firebase and GeoFire. However for some reason this switch is throwing me

2020-03-20 01:13:13.614 14508-14508/swift.fix.application I/Choreographer: Skipped 62 frames!  The application may be doing too much work on its main thread.
2020-03-20 01:13:13.907 14508-14508/swift.fix.application D/AndroidRuntime: Shutting down VM
2020-03-20 01:13:13.935 14508-14508/swift.fix.application E/AndroidRuntime: FATAL EXCEPTION: main
    Process: swift.fix.application, PID: 14508
    java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
        at swift.fix.application.TradesmanMapActivity$4.onLocationResult(TradesmanMapActivity.java:180)
        at com.google.android.gms.internal.location.zzau.notifyListener(Unknown Source:4)
        at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal(com.google.android.gms:play-services-base@@17.1.0:17)
        at com.google.android.gms.common.api.internal.ListenerHolder$zaa.handleMessage(com.google.android.gms:play-services-base@@17.1.0:6)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at com.google.android.gms.internal.base.zar.dispatchMessage(com.google.android.gms:play-services-base@@17.1.0:8)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        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:858)

The app crashes when I select to be a user, and crashes the app. I have looked through to see if I am missing a CustomerID somewhere but can't see anywhere. The stand out is the NullPointerException error.

switch (CustomerID){
                    case "":

                        geoFireWorking.removeLocation(UserID, new GeoFire.CompletionListener() {
                            @Override
                            public void onComplete(String key, DatabaseError error) {

                            }
                        });
                        geoFireAvailable.setLocation(UserID, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                            @Override
                            public void onComplete(String key, DatabaseError error) {

                            }
                        });

                        break;
                    default:
                        geoFireAvailable.removeLocation(UserID, new GeoFire.CompletionListener() {
                            @Override
                            public void onComplete(String key, DatabaseError error) {

                            }
                        });
                        geoFireWorking.setLocation(UserID, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                            @Override
                            public void onComplete(String key, DatabaseError error) {

                            }
                        });

                        break;
                }

I am not sure what I am missing and it is really bugging me as I assumed it was correct :(

rostacko1
  • 35
  • 6
  • What is line 180 of `TradesmanMapActivity.java`? – Andreas Mar 20 '20 at 02:13
  • I'm confused by this question, it looks like both branches of the switch are the same. You could combine them by putting `case "":` and `default:` on successive lines with the same block after. Where does the `NullPointerException` come in? – DCTID Mar 20 '20 at 02:13
  • @Andreas line 180 is ```switch (CustomerID){``` – rostacko1 Mar 20 '20 at 10:57
  • @DCTID the first removes working tradesman and sets location for available tradesman, the default removes location of available and sets location for working. Says on line 180 ```switch (CustomerID) {``` is where the null pointer comes in – rostacko1 Mar 20 '20 at 11:00
  • 1
    Solved, thanks. Required ```CustomerID = "";``` earlier on in an if statement – rostacko1 Mar 20 '20 at 12:06

1 Answers1

1

It sounds like, to me, that CustomerID is null. Check it before using switch on it.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thats what I assumed but I am following a tutorial and can't see where I am missing something as it is only ever declared null before the switch and never changes – rostacko1 Mar 20 '20 at 10:58
  • Found the error, needed to add this into an if statement earlier in the code ```CustomerID = "";``` – rostacko1 Mar 20 '20 at 12:04