0

The below activity gets data from my parse database and displays it. I have a function getLocationFromAddress() to convert a string address into LatLng format. The function prepareMap() then makes a marker and then points to that location on the map. The code for the activity(not showing functions which aren't imp) is as follows

public class SingleRestraunt extends ActionBarActivity {
    private GoogleMap map;
    TextView resteName, resteCuisine, resteLocation, resteAddress, restePrice,
            resteTimings, restePayment, resteRating, resteWebsite, next, prev;
    String restName, obj, restCuisine, restLocation, restAddress, restPrice,
            restTimings, restPayment, restRating, restWebsite;
    String[] menu;
    JSONArray restmenu;
    WebView web;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_restraunt);
        resteName = (TextView) findViewById(R.id.restrauntName);
        resteCuisine = (TextView) findViewById(R.id.restrauntCuisine);
        resteLocation = (TextView) findViewById(R.id.restrauntLocation);
        resteAddress = (TextView) findViewById(R.id.restrauntAddress);
        restePrice = (TextView) findViewById(R.id.restrauntPrice);
        resteTimings = (TextView) findViewById(R.id.restrauntTimings);
        restePayment = (TextView) findViewById(R.id.restrauntPayment);
        resteRating = (TextView) findViewById(R.id.restrauntRating);
        resteWebsite = (TextView) findViewById(R.id.restrauntWebsite);
        web = (WebView) findViewById(R.id.webView1);
        next = (TextView) findViewById(R.id.next);
        prev = (TextView) findViewById(R.id.prev);

        Intent i = getIntent();
        obj = i.getStringExtra("restId"); //gets the id of the restaurant whose 
        getDetails(obj);                  //details to be show   

    }

    private void getDetails(String obj) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("resdb");
        query.getInBackground(obj, new GetCallback<ParseObject>() {

            @Override
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    restName = object.getString("name");
                    restCuisine = object.getString("cuisine");
                    restLocation = object.getString("locality");
                    restAddress = object.getString("address");
                    restPrice = object.getString("price");
                    restTimings = object.getString("timings");
                    restPayment = object.getString("accepted");
                    restRating = object.getString("ratings");
                    restWebsite = object.getString("URL");
                    restmenu = object.getJSONArray("menu");
                    addData();
                    // prepareMap();
                } else {
                    e.printStackTrace();
                }
            }
        });

    }

    public void addData() {
        resteName.setText(restName);
        resteCuisine.setText(restCuisine);
        resteLocation.setText(restLocation);
        resteAddress.setText(restAddress);
        restePrice.setText(restPrice);
        resteTimings.setText(restTimings);
        restePayment.setText(restPayment);
        resteRating.setText(restRating);
        resteWebsite.setText(restWebsite);
        if (restmenu != null) {
            try {
                String menu = (String) restmenu.get(i);
                getMenu(menu);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

        }
    }

       public LatLng getLocationFromAddress(String strAddress) {
        Geocoder coder = new Geocoder(this);
        List<Address> address;
        LatLng p1 = null;
        try {
        address = coder.getFromLocationName(strAddress, 5);
        if (address != null && address.size() > 0) {
            Address location = address.get(0);
            p1 = new LatLng(location.getLatitude(), location.getLongitude());
        } else {
            p1 = new LatLng(19.111258, 72.908313);
        }
        } catch (IOException e) {
            e.printStackTrace();
            return p1;
        }
        return p1;
    }

    public void prepareMap() {
        final LatLng REST = getLocationFromAddress(restAddress + ","
                + restLocation);
        int zoomNiveau = 15;
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        map.addMarker(new MarkerOptions().position(REST).title(restName));//New error points here
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(REST, zoomNiveau));
}

The funniest thing is the above activity does not crash every time. It seems to crash for only certain restaurants. The error log shows the below

09-03 06:57:11.667: E/AndroidRuntime(2574): Process: com.example.gastronomaapp, PID: 2574
09-03 06:57:11.667: E/AndroidRuntime(2574): java.lang.NullPointerException
09-03 06:57:11.667: E/AndroidRuntime(2574):     at com.example.gastronomaapp.SingleRestraunt.prepareMap(SingleRestraunt.java:204)

Absolutely no idea whats the issue. I cant understand what the logic issue is either because it seems to work for certain restaurant.Any ideas? EDIT Made a quick change suggested in the comments. But problem persists but only the error has changed

Androider
  • 61
  • 1
  • 2
  • 9

1 Answers1

2

Your problem is probably in this code:

 address = coder.getFromLocationName(strAddress, 5);
        if (address != null) {
            Address location = address.get(0);
            p1 = new LatLng(location.getLatitude(), location.getLongitude());
        } else {
            p1 = new LatLng(19.111258, 72.908313);
        }

Make sure that the list of addresses is not empty before you try to get the first address, add something like this:

 address = coder.getFromLocationName(strAddress, 5);
        if (address != null && address.size() > 0) {
            Address location = address.get(0);
            p1 = new LatLng(location.getLatitude(), location.getLongitude());
        } else {
            p1 = new LatLng(19.111258, 72.908313);
        }

UPDATE:

why don't you try something like that:

 public LatLng getLocationFromAddress(String strAddress) {
    Geocoder coder = new Geocoder(this);
    List<Address> address;
    LatLng p1 = null;
    try {
    address = coder.getFromLocationName(strAddress, 5);
       if (address != null && address.size() > 0) {
           Address location = address.get(0);
           p1 = new LatLng(location.getLatitude(), location.getLongitude());
       } 
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (p1 == null) {
        p1 = new LatLng(19.111258, 72.908313);
    }
    return p1;
}
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • Ive Edited the question a bit.Incorporated the changes.Same issue,different error though – Androider Sep 03 '14 at 11:35
  • Have commented it,its the line with map.addMarker(new MarkerOptions().position(REST).title(restName)) – Androider Sep 03 '14 at 12:20
  • One of the parameters you parssing to this Marker custructor is null. make a quick debug run to find what is it. I'm suspecting that you don't get anything on "restName" parameter. – Emil Adz Sep 03 '14 at 12:27
  • I removed the restName part completely and it still returns the same error. Only other option seems to be REST but that shouldn't be possible – Androider Sep 03 '14 at 13:17
  • Could you test that the your REST Object is always initialized? – Emil Adz Sep 03 '14 at 13:31
  • I'm starting to doubt whether the function does the job it is meant to. either that or the addresses aren't sufficient to get a latitude longitude – Androider Sep 03 '14 at 16:29
  • Nope man still same error. Keeping my code aside do you know of any better way on what im trying to do. basically use a string address and try to mark it on a map – Androider Sep 03 '14 at 16:56
  • what you are doing is the way to achive this, on what line the error is now and is it still a NullPointerException? – Emil Adz Sep 03 '14 at 16:58
  • And would you mind to remove the final on LatLng REST object. – Emil Adz Sep 03 '14 at 16:59
  • removed final and copied your code. But now the NPE has now moved to map.moveCamera(CameraUpdateFactory.newLatLngZoom(REST, zoomNiveau)); – Androider Sep 03 '14 at 17:05
  • Why don't you try to place a break point at this location an run the application in debug mode that way the code will stop it's execution and you will be able to see what object is null at that time. – Emil Adz Sep 03 '14 at 17:07
  • On rerunning in debug, I noticed that the REST does have some values, but the app still crashes with NPE. I have absolutely no clue whats the issue now – Androider Sep 03 '14 at 18:40