What is Java null

6.1 The "null" and "this" references

Java has a "zero"Constant with the value of which you can assign references to:

  • dereferencing an object because the application logic requires it. The dereferenced object will (eventually) be deleted after nobody points to it anymore
  • Initialize references.

The following example shows what happens when dare2 and dare 3 be assigned the zero reference:

package s1.block6.reference; public class Main {public static void main (String [] args) {Kraftwagen wagen1 = new Kraftwagen ("0-1"); Motor vehicle wagen2 = wagen1; Kraftwagen wagen3 = new Kraftwagen ("0-2"); dare2 = zero; dare3 = zero; }}

The car "0-2" becomes a candidate for deletion. The car "0-1" is still on the reference dare 1 referenced.

If a reference contains a null variable, it no longer points to an object and you can no longer call the methods and data fields of an instance. With static declared methods and attributes are excluded from this.

In this case, the Java runtime system becomes a NullPointerException throw and exit the program. The following example shows the case of an unhandled exception and the handling of an exception:

package s1.block6.reference; public class Main {public static void main (String [] args) {Kraftwagen wagen1 = new Kraftwagen ("O-1"); Motor vehicle wagen2 = wagen1; Kraftwagen wagen3 = new Kraftwagen ("O-2"); dare2 = zero; dare3 = zero; try {System.out.println (wagen2.getKennzeichen ()); // First exception } catch (NullPointerException e) {System.out.println ("Variable wagen2 is a null reference"); } System.out.println (wagen3.getKennzeichen ()); // Second exception // The program was terminated because of the unhandled exception // The following line can no longer be reached System.out.println ("This line of the program cannot be reached .."); }}

The program will produce the following output on the console when it is executed;

Variable wagen2 is a null reference exception in thread "main" java.lang.NullPointerException at s1.block6.reference.Main.main (Main.java:20)

The first exception, the call to wagen2.getKennzeichen () is caught with a try-catch block. After the handling in the catch block, the method continues.

The call of wagen3.getKennzeichen () is not caught and throws an exception on line 20 which terminates the program. Exceptions and exception handling are covered in a later section, in Course 2.

There are cases in which you want to pass a pointer to the current instance in whose methods you are currently working as a parameter of a method to be called from another class. The keyword is used for this this. It can be used to point to the current instance as long as you are not in a static Method is located. static Methods can also be used without an existing instance. An object instance therefore does not necessarily exist.

Example:

class Person {... public void seat assignment (reservation r) {... r.booking (this); ...}} ... // Any other class Person p = new Person (); Reservation booking; booking = new reservation ("LH454", "17B"); // Time 1 p.seatSeatAssign (booking); // time 2 ...

Anonymous (not verified)

Tue, 12/18/2018 - 19:11

Permalink

Spelling mistake?

Shouldn't it read "... if wagen2 and wagen3 are assigned the zero reference" at the top of the first gray box next to the zero reference?

Stefan Schneider

Tue, 11/10/2020 - 17:32

Permalink

In reply to spelling mistake? by Anonymous (not verified)

Oops

Thank you, that's right, it has been improved.