'How Exactly do we set/define the object in bearingTo method?

How exactly do we set/define the object in bearingTo method?

I followed the code below in order to convert accelerations from geometric axes to object's (vehicle's) axes using bearing and magnetic declination. but I still don't know how to set the object for its bearingTo method.

Because when this code is implemented, it can't be executed because the error says

Attempt to invoke virtual method 'float android.location.Location.bearingTo(android.location.Location)' on a null object reference.

Location previousLocation;
Location location;
float[] rotation = new float[9];
float[] inclination = new float[9];
float[] gravity = {(float) xValueA, (float) yValueA, (float) zValueA};
float[] geomagnetic = {xValueM, yValueM, zValueM};
SensorManager.getRotationMatrix(rotation, inclination, gravity,
        geomagnetic);
float geometryAx = rotation[0]*gravity[0] + rotation[1]*gravity[1] + rotation[2]*gravity[2];
float geometryAy = rotation[3]*gravity[0] + rotation[4]*gravity[1] + rotation[5]*gravity[2];
float geometryAz = rotation[6]*gravity[0] + rotation[7]*gravity[1] + rotation[8]*gravity[2];
float latitude = (float)MobileSensors.getLat();
float longitude = (float)MobileSensors.getLon();
float altitude = (float)MobileSensors.getAlt();
long timeMilis = System.currentTimeMillis();
GeomagneticField geomagneticField = new GeomagneticField(latitude, longitude, altitude,timeMilis);
float magneticDeclination = geomagneticField.getDeclination();
float bearing  = previousLocation.bearingTo(location);
float teta = bearing - magneticDeclination;
double ay = geometryAy * Math.cos(teta) - geometryAx * Math.sin(teta);
double ax = geometryAy * Math.sin(teta) + geometryAx * Math.cos(teta);
double az = geometryAz;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source