java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference : <p>Everything working fine i got this error at the end please give solution. I have tried each and every solution, I don't know what to do.. please give the exact solution trying from past 2 days but i can't solve the problem..</p>

<p>Thanks in advance</p>

<pre><code>public double calculatedDistance(DataSnapshot dataSnapshot ) {


        HashMap&lt;String, Object&gt; values = (HashMap&lt;String, Object&gt;) dataSnapshot.getValue();

        double lati = Double.parseDouble(values.get("latitude").toString());
        double longi = Double.parseDouble(values.get("longitude").toString());

        double xSquare = Math.pow(lat-lati, 2);
        double ySquare = Math.pow(lng-longi, 2);
        double distance = Math.sqrt(xSquare + ySquare);

        //double dist = distance;

        textViewdistance.setText(String.valueOf(""+distance+""+"km"));
        return distance;

    }
</code></pre>

<pre><code>Error in Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ambulance, PID: 17399
    java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
        at com.example.ambulance.MapsActivity.calculatedDistance(MapsActivity.java:451)
        at com.example.ambulance.MapsActivity$2$1.onChildAdded(MapsActivity.java:178)
        at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.0:79)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.0:55)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
</code></pre>
