帮助!
我正在尝试将我在Firebase中拥有的产品的地址放入地图中,但遇到下一个错误
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String e.arturo.appmoviles.Model.Product.getAddress()'
我的Firebase结构
将我带到地图的按钮,其中模型是我的产品模型
btnMapa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Common.currentproduct = model;
Intent map = new Intent(ProductDetail.this,Maps.class);
startActivity(map);
}
});
这就是我初始化Common.currentproduct = model;
Product model;
这是我的模型产品
public class Product {
private String Name,Address,Image,Description,Price,Link,LatLng;
public Product() {
}
public Product(String name, String address, String image, String description, String price, String link, String latLng) {
Name = name;
Address = address;
Image = image;
Description = description;
Price = price;
Link = link;
LatLng = latLng;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getLink() {
return Link;
}
public void setLink(String link) {
Link = link;
}
public String getLatLng() {
return LatLng;
}
public void setLatLng(String latLng) {
LatLng = latLng;
}
}
我有一个名为Common的类,在其中放置了变量,方法,在程序的每个部分都使用了该类,并在其中放置了一个称为currentproduct的产品类型对象
public static Product currentproduct;
这是我创建的在地图上显示产品地址的方法
drawRoute(youLocation,Common.currentproduct.getAddress());
这是方法drawRoute
private void drawRoute(LatLng youLocation, String address) {
mService.getGeoCode(address).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject = new JSONObject(response.body().toString());
String lat = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lat").toString();
String lng = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lng").toString();
LatLng productLocation = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.product);
bitmap=Common.scaleBitmap(bitmap,70,70);
MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.title(Common.currentproduct.getName())
.position(productLocation);
mMap.addMarker(marker);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
感谢您的帮助!