0
votes

I am using google Maps in my application. There is a button on page on clicking first time the google Maps is loaded properly but again going to that page and clicking that button crashes my app.

This is my code:

public class GoogleMaps extends Activity {

    MapFragment googleMapFragment = null;
    private static GoogleMap googleMap = null;
    private String provider;
    MarkerOptions marker;
    double latitude, longitude;
    private LocationManager locationManager;
    ParseGeoPoint myLatLng[];
    int count_place, i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.googlemap_view);
        try {
            initilizeMap();
            initParse();
        } catch (Exception e) {
            // TODO: handle exception
            Toast.makeText(getApplicationContext(), e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void initParse() {
        // TODO Auto-generated method stub
        Parse.initialize(this, "key","key");
        ParseObject plcobj = new ParseObject("Details");
        getLatLong();
        ParseGeoPoint myGeoPiont = new ParseGeoPoint(latitude, longitude);
        ParseGeoPoint userLocation = (ParseGeoPoint) plcobj.get("location");
        final ParseQuery query = ParseQuery.getQuery("Details");
        query.whereNear("location", myGeoPiont);
        query.setLimit(10);
        query.whereWithinKilometers("location", myGeoPiont, 10);
        query.findInBackground(new FindCallback() {

            @Override
            public void done(List objects, ParseException e) {
                // TODO Auto-generated method stub

                if (e == null) {
                    Iterator Itr = objects.iterator();
                    while (Itr.hasNext()) {
                        ParseObject plcObjNext = (ParseObject) Itr.next();
                        myLatLng[i] = plcObjNext.getParseGeoPoint("location");
                        i++;  // Line 76

                    }
                }
            }
        });
    }

    private void initilizeMap() {
        if (googleMap == null) {
            googleMapFragment = ((MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map));
            googleMap = googleMapFragment.getMap();
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
            googleMap.setMyLocationEnabled(true);
            Log.e("Maps", "------EOC-------");
            // Get the location manager

        }
    }

    public void getLatLong() {
        GPSTracker gps = new GPSTracker(GoogleMaps.this);
        if (gps.canGetLocation()) {
            longitude = gps.getLongitude();
            latitude = gps.getLatitude();
            Toast.makeText(getApplicationContext(), longitude + " " + latitude,
                    Toast.LENGTH_SHORT).show();
        } // return boolean true/false
        else {
            gps.showSettingsAlert();
        }
    }
}

This is my log:

10-18 13:37:24.219 E/AndroidRuntime(21624): FATAL EXCEPTION: main
10-18 13:37:24.219 E/AndroidRuntime(21624): java.lang.NullPointerException
10-18 13:37:24.219 E/AndroidRuntime(21624): at com.example.restaurantfinder.GoogleMaps$1.done(GoogleMaps.java:76)
10-18 13:37:24.219 E/AndroidRuntime(21624): at com.parse.FindCallback.internalDone(FindCallback.java:45)
10-18 13:37:24.219 E/AndroidRuntime(21624): at com.parse.FindCallback.internalDone(FindCallback.java:1)
10-18 13:37:24.219 E/AndroidRuntime(21624): at com.parse.Parse$6$1.run(Parse.java:888)
10-18 13:37:24.219 E/AndroidRuntime(21624): at android.os.Handler.handleCallback(Handler.java:605)
10-18 13:37:24.219 E/AndroidRuntime(21624): at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 13:37:24.219 E/AndroidRuntime(21624): at android.os.Looper.loop(Looper.java:137)
10-18 13:37:24.219 E/AndroidRuntime(21624): at android.app.ActivityThread.main(ActivityThread.java:4517)
10-18 13:37:24.219 E/AndroidRuntime(21624): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 13:37:24.219 E/AndroidRuntime(21624): at java.lang.reflect.Method.invoke(Method.java:511)
10-18 13:37:24.219 E/AndroidRuntime(21624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-18 13:37:24.219 E/AndroidRuntime(21624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-18 13:37:24.219 E/AndroidRuntime(21624): at dalvik.system.NativeStart.main(Native Method)
2
Where is your stacktrace ?GrIsHu
provide crash logs and filter your code to mention minimum problem causing area after doing some homework. That will be helpful to all of us. we can understand problem easily and you can get answer easily.Rohit Sharma
have you try to run application in emulator..?please provide exception detail(logcat).if need to try to extends FragmentActivity.Imtiyaz Khalani
Evidently the problem is in the line 76 of GoogleMaps.java18446744073709551615
The object followed by dot is null. I do not know your line numbers, but you should be able to find the line.18446744073709551615

2 Answers

0
votes

So you are calling initilizeMap(); then initParse(); inside the initParse we have a list of objects. So that objects are becoming null when you go from one activity to another. And when you come back try to fetch the objects by saving it somewhere.

0
votes

Your problem is an empty ParseGeoPoint array.

When the compiler sees ParseGeoPoint myLatLng[];, it creates an pointer to an empty array with no memory, so when you try to access the memory via myLatLng[i], you get a null pointer back because the memory does not exist, and writing to a null pointer throws a NullPointerException because you need to be aware of such a mistake. You wouldn;t want to write to nothing and not have the data at a later point. In C, if you wrote to an empty array, it would overwrite another part of memory, instead of throwing an exception, causing potentially hard to trace bugs.

You need to initialize the array with an appropriate size before trying to write to it. In Java, arrays are initialized to a fixed length with the following syntax, which uses a length of 3:

ParseGeoPoint myLatLng[] = new ParseGeoPoint[3];

or sometime after ParseGeoPoint myLatLng[]; you type myLatLng[] = new ParseGeoPoint[3];

In your case:

myLatLng[] = new ParseGeoPoint[objects.size()]; before you iterate through the objects and try to write to the array. You can not write to a null pointer. You need to tell the compiler where to point to and how much space to allocate.

You may find ArrayList and other List Objects to be helpful when you are working with a list of items. They provide a lot of convenient methods that can save you time if you need them and they do not need a fixed length. They provide increasing storage as you add, until you run out of memory.

It will help you greatly and save you a lot of time to learn how to debug null pointers. They are a very common exception and debugging null pointers is straightforward most of the time.