3

I am getting an error on this code below:

  HashMap map = new HashMap();
  map.put("driver", userId);
  map.put("customer", customerId);
  map.put("rating", 0);
  map.put("timestamp", getCurrentTimestamp());
  map.put("destination", destination);
  map.put("location/from/lat", pickupLatLng.latitude);
  map.put("location/from/lng", pickupLatLng.longitude);
  map.put("location/to/lat", destinationLatLng.latitude);
  map.put("location/to/lng", destinationLatLng.longitude);
  map.put("distance", rideDistance);
  historyRef.child(requestId).updateChildren(map);

They said to add:

do if( pickupLatLng.latitude != null && pickupLatLng.longitude != null && destinationLatLng.latitude != null && destinationLatLng.longitude != null) before creating the hashMap, or for each latitude and longitude.

But when i try this i am getting the following error: operator != cannot be applied to double, null

Can someone help me please ?

Hotcaribbean
  • 57
  • 1
  • 1
  • 7
  • 2
    A `double` is a primitive type, so you can't check it for `null`. Instead check it whether it's `0` or below for instance. – Darwind Jun 11 '18 at 10:34
  • 1
    Since double is a primitive and not an Object you cannot compare it with null. Compare it with number (zero). See [here](https://stackoverflow.com/questions/50711708/error-operator-cannot-be-applied-to-double-null-how-to-fix?noredirect=1&lq=1) – Jon Goodwin Jun 11 '18 at 10:35
  • This might be useful https://stackoverflow.com/questions/9090077/how-to-check-if-a-double-is-null – Rohit5k2 Jun 11 '18 at 10:35
  • pls share NPE log ! i guess latitude or longitude is fine – Radesh Jun 11 '18 at 10:42

3 Answers3

5

double is a primitive type, it cannot be null, instead you can use Double and then compare it to null

Suleyman
  • 2,765
  • 2
  • 18
  • 31
1

I guess latitude or longitude is fine and your pickupLatLng or destinationLatLng is null and give you NPE so i think you must use this code instead

if (pickupLatLng != null && destinationLatLng != null) {}
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Radesh
  • 13,084
  • 4
  • 51
  • 64
-1

Firstly, a Java double cannot be null, and cannot be compared with a Java null. (The double type is a primitive (non-reference) type and primitive types cannot be null.)

Try this,

if( pickupLatLng.latitude != 0 && pickupLatLng.longitude != 0 && destinationLatLng.latitude != 0 && destinationLatLng.longitude != 0) 
Raj
  • 2,997
  • 2
  • 12
  • 30
  • This doesn't add any explanation as to why OP get's this error and so the answer isn't helpful at all. Rephrase your answer or delete it. – Darwind Jun 11 '18 at 10:36