Maps.getPhoto () 'on a null object reference

0

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.

    
asked by anonymous 14.04.2016 / 22:57

1 answer

0

Logcat already says what the error is

NullPointerException: Attempt to invoke virtual method 'int Maps.getPhoto ()' on a null object reference

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

Note that you create objects of class Map within the for loop of the public List<Maps> getSetCarList(int qtd, int category) method, in addition you have the following

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

 listAux.add(c);

In this case, if the if expression 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.

    
15.04.2016 / 20:09