Java: NULL in the constructor

Please, I have this question, which is a little theoretical, but I would like to understand it.

Why, if I pass an empty argument to the constructor, do I get a NullPointerException ?

This is my example.

 new AttendeeDetail("Gus Goose","1151","15-01-2012",(Integer) null,null) 

This is the class:

 public class AttendeeDetail { private String ticketholder_name; private String user_id; private String date_of_birth; private int tickets_purchased; private ArrayList<Ticket> tickets; public AttendeeDetail(String ticketholder_name, String user_id, String date_of_birth, int tickets_purchased, ArrayList<Ticket> tickets) { this.ticketholder_name=ticketholder_name; this.user_id=user_id; this.date_of_birth=date_of_birth; this.tickets_purchased=tickets_purchased; this.tickets=tickets; } } 
+6
source share
6 answers

a int cannot be null . If you really need to pass a null value, use the Integer type

+4
source

You assign Integer : (Integer) null the int : int tickets_purchased .

Integer , since the reference type may be null , but int as a primitive type cannot be, so an error occurs when trying to convert.

This conversion is called an unpack operation .

+6
source

Consider the following code:

 public class Main { public static void main(String[] args) { Integer wrapped = null; int primitive = wrapped; } } 

Now let's see what this code compiles with javap :

 public static void main(java.lang.String[]); Code: 0: aconst_null 1: astore_1 2: aload_1 3: invokevirtual #2 // Method java/lang/Integer.intValue:()I 6: istore_2 7: return 

As you can see, in order to unpack Integer , it tries to call the Integer#intValue() method on the null reference, and therefore it throws a NullPointerException .

+6
source

As the other answers mention, the problem with this is (Integer) null .

When the formal argument type is int and you try to pass (Integer) null , the following will happen.

  • The Java compiler is going to say "Ah! I have an Integer value and I need to convert it to an int value. I will output the code to unbox this!".

  • The code to delete the value is a call to Integer.intValue() , using the value-to-be-unboxed as the target. Except that null ... is not a real reference to an object.

  • At runtime, you call intValue() on null and throw an NPE.

Some compilers will issue a warning for this, stating that the code will always throw NPE. However, this is legal Java code, so the corresponding Java compiler cannot call this a compilation error.


Possible solutions:

  • Do not use (Integer) null as a parameter. Pass an int value or an actual (non-null) Integer object. In this case, zero looks like a reasonable replacement.

  • Change the type of the formal parameter from int to Integer ... and propagate the change to the places where the corresponding field value is returned, etc.

The first option is probably correct if you really do not need to simulate the possibility that the β€œvisitor” bought an unknown number of tickets. (And if you need to simulate this, then your application should handle this opportunity ... somehow.)

+5
source

Yes, you get a null pointer exception, because Java will try to automatically specify a null integer in an int, and this will not work, since null ints are not allowed :) (since you already tried this, I thought id gives an explanation, why do you see this behavior)

+4
source

int tickets_purchased = (Integer)null; it is forbidden in java.

You can use Integer tickets_purchased = (Integer)null;

+2
source

Source: https://habr.com/ru/post/976287/


All Articles