Null pointer access: The variable "tipoEstablecimiento" can only be null at this location
I've been searching in the Internet about this error with no result. I'm lo开发者_JAVA百科st with it. Anyone can help with it?
.
.
.
TipoEstablecimientoHotel tipoEstablecimiento = null;
.
.
.
.
try{
tipoEstablecimiento.setCodigo(""); <--- Line with the error.
}catch (Exception e){
System.out.println(e.getMessage());
}
.
.
.
Of course I have the imports that I need (I mean for TipoEstablecimiento), and it marks that line with the corresponding warning.
Thanks in advance.
You didn't show us enough code, but I'd go with the error you got. You don't initialize the variable between assigning null to it and calling its setCodigo
method so you'll surely get a NullPointerException (you cannot dereference a null object). Make sure you instantiate it before using it by calling its constructor, something like:
tipoEstablecimiento = new TipoEstablecimientoHotel();
Well, you're trying to call a method via tipoEstablecimiento
- and whatever is coming up with the warning has proven that the variable can't possibly have a non-null value - so it will always throw a NullPointerException
.
You need to assign a non-null value to the variable somewhere...
You need to instantiate the class, before calling its method.
TipoEstablecimientoHotel tipoEstablecimiento = new TipoEstablecimientoHotel();
The IDE or compiler is telling you that you are never setting tipoEstablecimiento
. In otherwords, that line that you are pointing at is absolutely guaranteed to throw a NullPointerException
.
Set the variable to some instance of TipoEstablecimientoHotel
to avoid the error.
There is nothing to do a setCodigo. TipoEstablecimiento is NULL, not an object this class. First you must instantiate an object
精彩评论