11

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;
    }
}
6
  • 2
    why do not you try it? Commented Oct 6, 2014 at 7:40
  • @KickButtowski I did and got a NullPointerException indeed Commented Oct 6, 2014 at 7:41
  • I feel it is good question and +1 already Commented Oct 6, 2014 at 7:45
  • 1
    Why do you parse null to integer? That is the place where there is a NPE Commented Oct 6, 2014 at 7:47
  • @Raghav The op asked valid question and we are trying to learn Commented Oct 6, 2014 at 7:48

6 Answers 6

11

an int can't be null. If you really need to pass a null value, use the Integer type

Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

6

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.

2 Comments

so as I can understand null does not apply to objects ?
@KickButtowski Sorry what do you mean?
6

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.

  1. 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!".

  2. 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.

  3. 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:

  1. 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.

  2. 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.)

Comments

4

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)

Comments

2

int tickets_purchased = (Integer)null; this is not allowed in java.

You can use Integer tickets_purchased = (Integer)null;

1 Comment

either this cannot be an answer or does not answer the op fully

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.