0

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!

mynavy
  • 81
  • 2
  • 9
  • line 90 is at the end of the for loop " for(j=2;j

    – mynavy May 10 '14 at 06:42
  • Because the stack trace indicates you have a null pointer exception at line 90. Do you know how to debug an app? – Andrew Fielden May 10 '14 at 06:50
  • The problem is with the googlemap object as they said in the other answers ... i need to send the object from the mainactivity to the broadcast Receiver... Still trying to make it – mynavy May 10 '14 at 07:04

2 Answers2

2

Your googleMap object is null.

You have only declared it

GoogleMap googleMap;

but never instantiated. So then when you try to do,

googleMap.getMyLocation();

you run into Null Pointer Exception.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • Sorry buddy never used GoogleMaps yet. But this is what I found on Android Docs - You cannot instantiate a GoogleMap object directly, rather, you must obtain one from the getMap() method on a MapFragment or MapView that you have added to your application. – Atul O Holic May 09 '14 at 19:05
  • Yes it is declared in my MainActivity... it is working fine ther... im confused.. – mynavy May 09 '14 at 19:06
  • Oh ok, in that case please see this how to pass an object from Activity to your BroadcastReceiver - http://stackoverflow.com/questions/10032480/how-to-pass-data-to-broadcastreceiver – Atul O Holic May 09 '14 at 19:09
  • You will have to similarly pass your GoogleMap object here in order to access it. – Atul O Holic May 09 '14 at 19:10
  • I guess thats the problem .. but i dont understand what's in the registration the "com.myapp.myaction" is the name of the package or something? – mynavy May 09 '14 at 19:22
  • Thats a unique identifier basically a String which is used as an Action to identify your Broadcast Receiver. Kind of your own Intent-Filter. This is registered in the manifest file as – Atul O Holic May 09 '14 at 19:33
2

It's not an infinite loop. It's a NullPointerException, at line 90. I'm guessing here:

 Location lm = googleMap.getMyLocation();

Make sure you assign a value to googleMap before you try to call one of its methods.

ajpolt
  • 1,002
  • 6
  • 10
  • I understand but what value should i assign if im assuming that the device is outside getting coordinates from the gps... ? – mynavy May 09 '14 at 19:03
  • From the Google documentation (assuming in your layout file you have a MapFragment with id "map": GoogleMap map = ((MapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap(); – ajpolt May 09 '14 at 19:08