Once the customer name is clicked the place of his/her needs to be marked with a marker on google map. As I am displaying the customer details on a popup window, the customer's latitude and longitude are being passed from popup window class to the map activity class as an object through intents. While receiving it in map activity.java it becomes null and throwing a null pointer exception.
my code for popup window class (CustomerPupUp.java):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_pop_up);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width),(int)(height*.7));
listView = (ListView)findViewById(R.id.custListVw);
database = FirebaseDatabase.getInstance();
reference = database.getReference("Customers");
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, R.layout.activity_cust_layout,R.id.cus, list);
customer = new AddCustomer();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren() )
{
customer = ds.getValue(AddCustomer.class);
Log.d("CustomerPopUp", "onDataChange: " + customer.getLat());
list.add(customer.getFname().toString() + " " + customer.getLname().toString() );
customerList.add(customer);
}
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(CustomerPopUp.this, MapsActivity.class);
intent.putExtra("CustomerData", customerList.get(position));
startActivity(intent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
following code segment shows how I am getting the object through intent in MapsActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
try {
database = FirebaseDatabase.getInstance();
reference = database.getReference("Customers");
customer= (AddCustomer) getIntent().getSerializableExtra("CustomerData");
mMap.addMarker(new MarkerOptions().position(new LatLng(customer.getLat(), customer.getLng())).title(customer.getFname() + customer.getLname()));
}catch (NullPointerException ex)
{
Log.d(TAG, "onCreate: " + ex.getMessage());
}
search = (EditText)findViewById(R.id.SearchText);
gps =(ImageView) findViewById(R.id.GpsBtn);
cust = (ImageView)findViewById(R.id.CusBtn);
isServiceFine();
getLocationPermission();
cust.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MapsActivity.this, CustomerPopUp.class);
startActivity(intent);
}
});
}
following is the AddCustomer class
public class AddCustomer implements Serializable {
private String fname;
private String lname;
private String email;
private String pass;
private String confPass;
private int num;
Double lat;
Double lng;
AddCustomer()
{
}
public AddCustomer(String FName, String LName, String EMail, String PWD, String CONFPWD, int NUM, Double latitude, Double longtitude)
{
this.fname = FName;
this.lname = LName;
this.email = EMail;
this.pass = PWD;
this.confPass = CONFPWD;
this.num = NUM;
this.lat = latitude;
this.lng = longtitude;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getConfPass() {
return confPass;
}
public void setConfPass(String confPass) {
this.confPass = confPass;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
}
Please help me solve this issue.