I’m making an app which for now, parses an url and takes some info from it. I want to use this values to create objects, put them into an arraylist, and show them on screen with a ListView layout.
Problem comes when i want to assign drawables values from the object. I want, depending on its values, assign a drawable or another. Here is the code:
Retransmision retransmision = new Retransmision();
retransmision.setIdioma(idioma);
retransmision.setTipo(tipo);
retransmision.setCalidad(calidad);
retransmision.setLink(link);
retransmision.setImagenLogo(tipo);
retransmision.setImagenCalidad(calidad);
retransmisionesDescargadas.add(retransmision);
I just create the object and assign some values with its methods. And heres the method wich is throwing nullPointerException:
public void setImagenCalidad(int calidad) {
if (calidad == 0) {
imagenCalidad = context.getResources().getDrawable(R.drawable.desconocida);
} else if(calidad <= 250) {
imagenCalidad = context.getResources().getDrawable(R.drawable.baja);
} else if(calidad <= 500) {
imagenCalidad = context.getResources().getDrawable(R.drawable.media);
} else if(calidad <= 750) {
imagenCalidad = context.getResources().getDrawable(R.drawable.alta);
} else if(calidad <= 1000) {
imagenCalidad = context.getResources().getDrawable(R.drawable.muy_alta);
} else {
imagenCalidad = context.getResources().getDrawable(R.drawable.excelente);
}
}
Weird since i use a similar code for method setImagenLogo that seems to work well. And only difference is one gets a String parameter. And the other an int.
Ps: If it might help, this is the constructor of the retransmision object and its variables:
Context context;
private String idioma;
private String tipo;
private int calidad;
private String link;
private Drawable imagenLogo;
private Drawable imagenIdioma;
private Drawable imagenCalidad;
public Retransmision() {
idioma = "";
tipo = "";
calidad = 0;
link = "";
imagenLogo = null;
imagenIdioma = null;
imagenCalidad = null;
}
The only thing that could be null there is the
context… and I see you didn’t initialize it in your constructor. Maybe this is what you want: