My app keeps crashing onReceive Basically im sending gps coordinates to another android device and Im trying to build an equation of line from those sets of gps coordinates on a while(true) loop untill he will get to this line, Im using the while loop because I need to wait untill he will get coordinates to solve the equation of line.
But it keeps crashing .
thats my OnReceive code :
package com.example.yeah;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.android.gms.maps.GoogleMap;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsListener extends BroadcastReceiver {
GoogleMap googleMap;
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
//show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
String s=smsMessage[0].getMessageBody();
Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(s);
double[] d = new double[1000] ;
int p=0;
while (m.find())
{
double k = Double.parseDouble(m.group(1));
d[p]=k;
p++;
}
int j,i=1,t,success=0;
double line;
double[] ship = new double[1000] ;
double[] b = new double[1000] ;
double lat;
double lng;
ship[0]=SlopeCalc(d[2],d[0],d[3],d[1]);
b[0]=d[0]-ship[0]*d[1];
if (p>3)
{
for(j=2;j<p;j++)
{
if(j+2<p){
ship[i]=SlopeCalc(d[j+2],d[j-2+2],d[j+1+2],d[j-1+2]);
b[i]=d[j]-(ship[i]*d[j+1]);
j++;
i++;
}
else{
break;
}
}
}
while(true)
{
Location lm = googleMap.getMyLocation();
lat=lm.getLatitude();
lng=lm.getLongitude();
for (t=0;t<i;t++){
line=ship[t]*lng+b[t]-lat;
if (line==0){
success=1;
break;
}
}
if (success==1){
break;
}
}
Toast poast = Toast.makeText(context, "I FOUND U", Toast.LENGTH_LONG);
poast.show();
abortBroadcast();
}
public static double SlopeCalc(double y2,double y1, double x2,double x1){
double sou;
sou=(y2-y1)/(x2-x1);
return sou;
}
}
and thats my logcat :
05-09 21:44:24.165: E/Gsm/SmsMessage(2483): hasUserDataHeader : false
05-09 21:44:24.190: E/AndroidRuntime(2483): FATAL EXCEPTION: main
05-09 21:44:24.190: E/AndroidRuntime(2483): java.lang.RuntimeException: Unable to start receiver com.example.yeah.SmsListener: java.lang.NullPointerException
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.os.Looper.loop(Looper.java:130)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-09 21:44:24.190: E/AndroidRuntime(2483): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 21:44:24.190: E/AndroidRuntime(2483): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 21:44:24.190: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-09 21:44:24.190: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-09 21:44:24.190: E/AndroidRuntime(2483): at dalvik.system.NativeStart.main(Native Method)
05-09 21:44:24.190: E/AndroidRuntime(2483): Caused by: java.lang.NullPointerException
05-09 21:44:24.190: E/AndroidRuntime(2483): at com.example.yeah.SmsListener.onReceive(SmsListener.java:90)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
05-09 21:44:24.190: E/AndroidRuntime(2483): ... 10 more
I guess its crashing because the while loop ... Need your help!
Your googleMap object is null.
You have only declared it
but never instantiated. So then when you try to do,
you run into Null Pointer Exception.
It's not an infinite loop. It's a NullPointerException, at line 90. I'm guessing here:
Make sure you assign a value to googleMap before you try to call one of its methods.