The program I am developing compiles, does not display any error, emulo in the mobile, but when opening a certain screen the application shows error message and date. In the AS terminal it displays the message:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int br.com.diego.tecnologiadanet.domain.Maps.getPhoto ()' on a null object reference at br.com.diego.tecnologiadanet.StartActivity.onCreate (StartActivity.java:88)
and points to this line:
>>> ivCar.setImageResource(maps.getPhoto()); <<<
tvModel.setText(maps.getModel());
tvBrand.setText(maps.getBrand());
tvDescription.setText( maps.getDescription() );
Here's my minmap function:
public class Maps implements Parcelable {
private String model;
private String brand;
private String description;
private int category;
private String tel;
private int photo;
//public Maps(){}
public Maps(String m, String b, int p){
model = m;
brand = b;
photo = p;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getCategory() {
return category;
}
public void setCategory(int category) {
this.category = category;
}
// PARCELABLE
public Maps(Parcel parcel){
setModel(parcel.readString());
setBrand(parcel.readString());
setDescription(parcel.readString());
setCategory(parcel.readInt());
setTel(parcel.readString());
setPhoto(parcel.readInt());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString( getModel() );
dest.writeString( getBrand() );
dest.writeString( getDescription() );
dest.writeInt( getCategory() );
dest.writeString( getTel() );
dest.writeInt( getPhoto() );
}
public static final Parcelable.Creator<Maps> CREATOR = new Parcelable.Creator<Maps>(){
@Override
public Maps createFromParcel(Parcel source) {
return new Maps(source);
}
@Override
public Maps[] newArray(int size) {
return new Maps[size];
}
};
}
The function of the error screen when opening:
public List<Maps> getSetCarList(int qtd, int category){
String[] models = new String[]{"SETOR 1", "SETOR 2", "SETOR 3"};
String[] brands = new String[]{"STATUS - ABASTECENDO", "STATUS - AGUARDANDO", "STATUS - AGUARDANDO"};
int[] categories = new int[]{1, 2, 3};
int[] photos = new int[]{R.drawable.mapa, R.drawable.mapa, R.drawable.mapa};
String[] description = new String[]{"DIAS: 06,07,08 - 13,14,15 - 23,24,25", "DIAS: 06,07,08 - 13,14,15 - 23,24,30", "DIAS: 06,07,08 - 13,14,15 - 23,24,00"};
List<Maps> listAux = new ArrayList<>();
for(int i = 0; i < qtd; i++){
Maps c = new Maps( models[i % models.length], brands[ i % brands.length], photos[i % models.length] );
c.setDescription( description[i % description.length] );
c.setCategory( categories[ i % brands.length ] );
c.setTel("(XX) XXXXXXX");
if(category != 0 && c.getCategory() != category){
continue;
}
listAux.add(c);
}
return(listAux);
}
I hope you will help me.