NullPointerException: is an exception of java
that indicates that an object is without an object , worth the redundancy.
Case 1: (Object not initialized) That is, an object has been created but not initialized, in java an object is initialized with the reserved word new
, followed by the name of the object that is you want to instantiate.
Example:
import java.util.ArrayList;
public class TestNullException {
public static void main (String [] Args) {
ArrayList<Integer> Numeros;
Integer suma = 0;
for (Integer numero : Numeros) {//error de java.lang.NullPointerException, por no iniciar Numeros con new ArrayList<Integer>(),
suma = suma + numero;
}
System.out.println ("El sumatorio actual es: " + suma);
}
}
case 2: (Object deleted) That is, at some point the object was initialized and instantiated, but it was deleted, that is, it was pointed to null.
Example:
import java.util.ArrayList;
public class TestNullException {
class EjemploNull{
String nombre;
EjemploNull(String nombre){
this.nombre=nombre;
}
public static String GetNombre(){
return this.nombre
}
}
public static void main (String [] Args) {
EjemploNull nom=new EjemploNull("Juan");
...
...//mucho codigo utilizando nom sin problemas
...
nom=null;//por algun motivo lo pusiste en null, tal vez sin querer
...// mas codigo sin darte cuenta del error
String nuevo_nombre=nom.GetNombre();//error de java.lang.NullPointerException
...//el resto del codigo
}
}
I think the second case can replicate your problem.
I hope some of this will help you.