I’m trying to figure out how a method invocation that supplies unusable arguments throw a exception on the calling line of code – before it gets to the method line.
Below is an example
1. static Integer x;
2. public static void main(String args[]){
3. doStuff(x)} //null pointer exception thrown on this line
//lines 4-49
50. public static void doStuff(int z){}
Here I’m sending a Integer object reference to the method, and due to autoboxing, an Integer object is a valid reference to send.
If the invoked method is not loaded onto the stack until the line 50 is reached, why does the JVM throw the null pointer exception on line 3, when it hasn’t gotten to the method signature yet?
If the invoked method is not loaded onto the stack until the line 50 is reached– this is an incorrect assumption.Under the hood when you autobox and treat an Integer as an int or vice versa, the compiler inserts a call to intValue() or Integer.valueof().
It gets more obvious why if you use a decompiler on the class file:
So
intValue()is called on a Null – which results in null pointer exception.