When I call a method like this:
int value1;
Integer value2 = null;
setValue(value2);
private void setValue(int value) {
value1 = value;
}
Why is there a NullPointerException
?
When I call a method like this:
int value1;
Integer value2 = null;
setValue(value2);
private void setValue(int value) {
value1 = value;
}
Why is there a NullPointerException
?
The case which hasn't been mentioned in the current answers is if the formal parameter type is primitive:
void setValue(int a) {
// ...
}
This can be called with a parameter null
, because the compiler will attempt to auto-unbox the value. Effectively:
setValue(null);
Would be invoked as:
setValue(((Integer) null).intValue());
However, this will always fail with NPE, because you can't dereference a null value.
But note that the NPE doesn't occur because null
is passed to the method, but rather because of how the value which would be passed is evaluated. The difference is subtle, but important.
Any IDE worth using should flag this as a problem.
Yes, if setValue()
does access the object like this:
void setValue(Long lung) {
this.val = lung.intValue();
}
because it tries to dereference null
. and no, if setValue()
does treat the object like that:
void setValue(Long lung) {
this.val = Optional.ofNullable(lung)
.map(Long::intValue)
.orElse(0L) ;
}
because it is aware of how to handle null
.
The point is: if the method which receives the null
argument does dereference it inside the method body, or pass it to some method which does, a NullPointerException
will be thrown. Otherwise not.
Null Pointer Exception
happens when you try to call a method on null
reference. So when you are calling that method and any where you are using that argument to call some method you will get a Null Pointer Exception
, otherwise if you are passing it into an API of JAVA or someone else and if the same thing happens inside then also you will jump into NPE
. So debug
your code properly to find the possible places where you might get Exception
and give null checks.
void setValue(Object value) {
// case : 1
int i = (Integer) value;
// case : 2
int i = value.toString().length();
// case : 3
value.getSomethingElse();
}
These are the possible cases where you will be getting NPE
. But if you are just calling this method to simply set some derived attribute, its fine because null
can be assigned to any derived attribute. But if you call any method on that attribute after setting it to null then the result will be the same.
class Foo {
private String entity;
public void setEntity(String entity) {
this.entity = entity;
}
public String getEntity() {
return entity;
}
}
class FooManager {
public static void main(String[] arg) {
Foo foo = new Foo();
foo.setEntity(null);
System.out.println(foo.getEntity().length()); //NPE
}
}
A NullPointerException
is generally thrown when the JVM expected a valid object reference but got null
instead. A very common specific case of this is when you set a reference to null
and then try to call a method or access a field on that reference.
Given the information you have given, it is impossible tell if your code will throw an exception.
If the setter just assigns a reference, it will not throw anything immediately, because you can pretty much assign null
to anything that is not a primitive. This is not to say that you will not get the exception later when you try to access the object you just set.
If the setter performs any type of actions directly on the reference you passed in, without checking if it is null
first, you are likely to get an exception. Most likely, however, if the setter is well written, it will perform some checks and take appropriate action before blindly using the passed-in reference.
A NullPointerException is thrown whenever you try to access something on a null-Reference.
Let's say we have this method:
public static String test(Object obj)
{
return obj.toString();
}
And we invoke it like this:
test(new Object());
Everything will work fine.
But if we invoke the method with null:
test(null);
a NullPointerException will be thrown.
Looking at the StackTrace you will see, that the NullPointerException was thrown in this line:
return obj.toString();
That's because we tried to invoke the Method "toString" on a null-Reference.
So we see, that not giving the Method a null-Parameter, but invoking a Method on a null-Reference causes a NullPointerException. Some Methods check if the Parameter is null and have a special action for that. Often it's even very useful. But most methods will require the parameters not to be null.