English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Cosa succede se non si gestisce NullPointerException nel metodo principale di Java?

In Java, each type has a default value. When you do not initialize the instance variables of a class, the Java compiler will use these values to initialize them for you. Null is the default value for the object type, and you can also manually assign null to an object in a method.

Object obj = null;

However, you cannot use an object with a null value or (if you use a null value instead of an object) an object, and it will causeNullPointerException.

Here are some situations that may cause NullPointerException.

  • Use a null object to call method a (instance).

public class Demo {
   public void demoMethod() {
      System.out.println("Hello how are you");
   }
   public static void main(String args[]) {
      Demo obj = null;
      obj.demoMethod();
   }
}

Run-time exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:11)
  • Access, modify, and print the fields of a null value (object).

public class Demo {
   String name = "Krishna";
   int age = 25;
   public static void main(String args[]) {
      Demo obj = null;
      System.out.println(obj.age);
      System.out.println(obj.name);
   }
}

Run-time exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:11)
  • Attempt to access (print/use in a statement) the length of a null value.

public class Demo {
   String name = null;
   public static void main(String args[]) {
      Demo obj = new Demo();
      System.out.println(obj.name.length());
   }
}

Run-time exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:7)
  • Throw a null value.

public class Demo {
   public static void main(String args[]) {
      throw null;
   }
}

Run-time exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:5)
  • Access or modify an element/slot with a null value.

public class Demo {
   public static void main(String args[]) {
      int myArray[] = null;
      System.out.println(myArray[5]);
   }
}

Run-time exception

Exception in thread "main" java.lang.NullPointerException
   at july_set3.Demo.main(Demo.java:6)

Evitare il NullPointerException

Evitare il NullPointerException

  • Assicurarsi che tutti gli oggetti siano inizializzati prima di utilizzarli.

  • Assicurarsi che ogni variabile di riferimento (oggetto, array, ecc.) non sia null prima di accedervi ai campi e ai metodi (se presenti).

Gestione del NullPointerException

Sì, è possibile gestire il NullPointerException nel metodo main e visualizzare il proprio messaggio. Inoltre, se non è stato gestito, il programma terminerà in eccezione durante l'esecuzione.

Esempio

public class Demo {
   public static void main(String args[]) {
      int myArray[] = null;
      try {
         System.out.println(myArray[5]);
      }
         System.out.println("NPE occurred");
      }
   }
}

Risultato di output

NPE occurred

Ma poiché il NullPointerException è un'eccezione a controllo esplicito, non è necessario trattarla in tempo di esecuzione.

Inoltre, se ci sono errori da correggere nel programma, si verifica un NPE, si consiglia di correggere o evitare l'errore invece di cercare di catturare l'eccezione.