1

I wrote some code that sets a marker on a current location when clicked. Unfortunately, the entire application crashes when no current location is given and the button is clicked. The error states: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

Is there a way implement a try catch exemption within the button?

Here is the on click instruction for the button:

    markerBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            LatLng myCurrentLocation = new LatLng(myCurrentLatitude, myCurrentLongitude);
            marker = mMap.addMarker(new MarkerOptions().position(myLocation).title("New Marker"));
            markerBtn.setTag("yo");

        }
    });
Zoe
  • 27,060
  • 21
  • 118
  • 148
Bread
  • 71
  • 1
  • 13
  • Sure there is a way. Have you tried? Which part of implementing it *exactly* are you stuck on? https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html – codeMagic Nov 25 '17 at 00:07
  • your myLocation is probably null. You need to put checks for nulls in the code, like if (myLocation!=null) { } –  Nov 25 '17 at 05:00

3 Answers3

1
markerBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        try{
            LatLng myCurrentLocation = new LatLng(myCurrentLatitude, myCurrentLongitude);
            marker = mMap.addMarker(new MarkerOptions().position(myLocation).title("New Marker"));
            markerBtn.setTag("yo");
        }catch(Exception e){
            System.out.println("No current location set");
        }
});
kujosHeist
  • 810
  • 1
  • 11
  • 25
1

Try this before accessing lat/lng values.

      markerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{ 
                     if((myCurrentLatitude!=null && myCurrentLongitude!=null) &&(myCurrentLatitude!=0.0 && myCurrentLongitude!=0.0))
                     {

                        LatLng myCurrentLocation = new LatLng(myCurrentLatitude, myCurrentLongitude);
                        marker = mMap.addMarker(new MarkerOptions().position(myLocation).title("New Marker"));
                        markerBtn.setTag("yo");
                   }
                }
              catch(Exception e)
              {
                 e.getMessage();
               }


            }
        });
MezzDroid
  • 560
  • 4
  • 11
1

Already two answers here. But i want to tell you the right way to do this. Just return the code with appropriate message if he did not selected location yet.

Bonus suggestion : Avoid using try catch block for these. they are used for unexpected errors. Like trying to connect a socket that does not exist.

   markerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

              // check if user has not set location yet.

                if (myCurrentLatitude == null | myCurrentLongitude == null){
                    Toast.makeText(EventViewActivity.this, "Please choose your location first", Toast.LENGTH_SHORT).show();
                    return;
                }
                LatLng myCurrentLocation = new LatLng(myCurrentLatitude, myCurrentLongitude);
                marker = mMap.addMarker(new MarkerOptions().position(myLocation).title("New Marker"));
                markerBtn.setTag("yo");

            });
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212