Maps.getPhoto()' on a null Object Reference

Asked

Viewed 29 times

0

The program I am developing compiles, does not show any error, emulo on mobile, however when opening certain screen the application shows error message and closes. In the AS terminal shows the message:

Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'int.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 is my function Maps:

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 error screen function 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 help me.

1 answer

0


logcat already tells what the error is

Nullpointerexception: Attempt to invoke virtual method 'int Maps.getPhoto()' on a null Object Reference

That is, you are calling the method getPhoto() of an object Map whose instance is null, in which case you must instantiate a Map before calling the method getPhoto().

Notice that you create class objects Map inside the loop for of the method public List<Maps> getSetCarList(int qtd, int category), further you have the following block

 if(category != 0 && c.getCategory() != category){
        continue;
 }

 listAux.add(c);

In that case, if the expression of if is valid, listAux.add(c) is not executed and a new iteration of for is called, therefore, I believe that somehow this expression is always giving true and for this reason, no Map is added to the list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.