Search code examples
javaandroidnullgetter-setter

Getters returning null after calling in another class


i want to get the marker latitude to another class using getters and setters. but getters always return null value. it totally make no sense to me. i am struggling to find out what's wrong here.

Here is my Location1 class Where i use Setters

public class Location1 extends AppCompatActivity implements OnMapReadyCallback {

//do changes inside loaded map
@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(Location1.this, "Map Is Ready", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onMapReady: map is ready");
    mMap = googleMap;

    if (permission_Granted) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);
        mMap.getUiSettings().setCompassEnabled(true);



        //Afer Clicking google map marker info window
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {


                //Here using setters 
                marker1 marker1 = new marker1();
                marker1.setMarkerLat(marker.getPosition().latitude);

                Toast.makeText(Location1.this,String.valueOf(marker.getPosition().latitude),Toast.LENGTH_SHORT).show();

                startActivity(new Intent(Location1.this, pop.class));
            }
        });
}

}

and my marker1 class

public class marker1 {

Double markerLat;

public Double getMarkerLat() {
    return markerLat;
}

public void setMarkerLat(Double markerLat) {
    this.markerLat = markerLat;
}
}

and pop class where i use Getters which return null value

public class pop extends AppCompatActivity   {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.garagepopup_1);


    GarageNameTextView = (TextView) findViewById(R.id.nameView);
    GarageAddrTextView = (TextView) findViewById(R.id.addresView);
    GarageContactTextView = (TextView) findViewById(R.id.phoneView);

    dbGarages= FirebaseDatabase.getInstance().getReference("Garages");

    //Using getters
    marker1 marker1 = new marker1();
    String lat=String.valueOf(marker1.getMarkerLat());

    Toast.makeText(pop.this,lat, Toast.LENGTH_SHORT).show();

}
}

i am stuck here. cannot proceed forward..any help would be appreciated.. Thanks.


Solution

  • @Kasun_Chamara for get location in second activity you have some ways:

    first: you must set markerLat as static field.

    like this :

    public class marker1 {
    
    static Double markerLat;
    
    public Double getMarkerLat() {
        return markerLat;
    }
    
    public void setMarkerLat(Double markerLat) {
        this.markerLat = markerLat;
    }
    }
    

    second: use putExtra to send locationparameter :

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
    
    
                    //Here using setters 
                    marker1 marker1 = new marker1();
                    marker1.setMarkerLat(marker.getPosition().latitude);
    
                    Toast.makeText(Location1.this,String.valueOf(marker.getPosition().latitude),Toast.LENGTH_SHORT).show();
    
                    Intent myintent = new Intent(Location1.this, pop.class);
                    myintent.putExtra("Location",marker.getPosition().latitude);
                    startActivity(myintent);
                }
            });
    

    in second Activity:

    getIntent().getIntExtra("Location",0);
    

    Third:

    According to @skandigraun you can send its object:

    How to pass an object from one activity to another on Android