Reputation: 4595
Please I have this question which is a bit theoretical, but I would like to understand it.
Why if I pass a null argument to a 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;
}
}
Upvotes: 11
Views: 25425
Reputation: 719709
As other answers mention, the problem is this - (Integer) null
.
When the formal type of a method argument is int
and you try to pass (Integer) null
, the following is going to 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 code to unbox it!".
The code to unbox the value is a call to Integer.intValue()
, using the value-to-be-unboxed as the target object. Except that the value is null
... not a real object reference.
At runtime, you end up calling intValue()
on the null
, and that throws an NPE.
Some compilers will issue a warning for this, saying that the code will always throw an NPE. However, this is legal Java code, so a conformant Java compiler cannot call this a compilation error.
Possible solutions:
Don't use (Integer) null
as the parameter. Pass an int
value, or an actual (not null) Integer
object. In this case, zero looks like a reasonable substitute.
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, used, etcetera.
The first alternative is probably the correct one, unless you really need to model the possibility that an "attendee" has purchased an unknown number of tickets. (And if you do need to model that, then your application has to cope with this possibility ... somehow.)
Upvotes: 6
Reputation: 54811
You're assigning an Integer
: (Integer) null
to an int
parameter: int tickets_purchased
.
Integer
, as a reference type can be null
, but int
as a primitive type cannot be, so an error occurs during the attempt to convert.
This conversion is called an unboxing operation.
Upvotes: 6
Reputation: 5064
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 to by using 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 unbox the Integer
it tries to invoke Integer#intValue()
method on null
reference, and therefore it throws NullPointerException
.
Upvotes: 6
Reputation: 456
Yes, you get a null pointer exception because Java will try to autobox your null Integer to an int, and that wont work since null ints are not allowed :) (since you already tried it out i thought id give an explenation why you see this behaviour)
Upvotes: 4
Reputation: 3716
int tickets_purchased = (Integer)null;
this is not allowed in java.
You can use Integer tickets_purchased = (Integer)null;
Upvotes: 2
Reputation: 3691
an int
can't be null
.
If you really need to pass a null value, use the Integer
type
Upvotes: 11