When NullPointerException Decides to Take a Day Off
Introduction
If you’ve been programming in Java for a while, you’ve likely encountered the dreaded NullPointerException. It’s one of those errors that can ruin your day—trying to call a method on null almost always ends in disaster.
But what if I told you that sometimes, calling a method on null doesn’t throw an exception? Sounds bizarre, right?
Let’s walk through a few cases and uncover a behavior that might just surprise you.
A Simple Example: Calling Methods on Null
Consider this class, TestUtil, which has two simple methods
package basics.learnings;
public class TestUtil {
public static void runStaticMethod() {
System.out.println("This is a static method");
}
public void runNonStaticMethod() {
System.out.println("This is a non-static method");
}
}
Now, let’s create an Example class and try calling these methods.
Case 1: The Expected Behavior
First, we do the usual—create an object and call both methods.
package basics.learnings;
public class Example1 {
public static void main(String[] args) {
TestUtil testUtil = new TestUtil();
testUtil.runStaticMethod(); // Works ✅
testUtil.runNonStaticMethod(); // Works ✅
}
}
✅ Expected Output:
This is a static method
This is a non-static method
Nothing unusual so far.
Case 2: The Disaster Strikes
Now, what happens if we set testUtil to null and try calling a method?
package basics.learnings;
public class Example2 {
public static void main(String[] args) {
TestUtil testUtil = null;
testUtil.runNonStaticMethod(); // 💥 Throws NullPointerException
}
}
❌ Output:
Exception in thread "main" java.lang.NullPointerException
🚨 As expected, Java crashes! We tried calling a method on null, and it threw a NullPointerException.
Case 3: The Shocking Twist
Now, let’s try something else.
package basics.learnings;
public class Example3 {
public static void main(String[] args) {
TestUtil testUtil = null;
testUtil.runStaticMethod(); // Wait... what?
}
}
What did you expect? A NullPointerException?
Recommended by LinkedIn
Surprise! Here’s the actual output:
This is a static method
😲 No exception! No crash! It just works!
The Big Reveal: Why This Happens
You might be wondering—why did calling runNonStaticMethod() on null crash, but calling runStaticMethod() worked fine?
Here’s the key:
In simpler terms, when you write:
testUtil.runStaticMethod();
Java actually sees it as:
TestUtil.runStaticMethod();
You can also verify this from your build folder where compiled class resides where testUtil.runStaticMethod(); would be replaced with TestUtil.runStaticMethod(); during compilation itself.
The null reference (or any object reference for that matter) is completely ignored when calling static method.
Having said that, it is not recommended to use it in this fashion.
So let’s see how one should be writing code around this.
Best Practices and Takeaways
1️⃣ Never call static methods on an instance reference, even if it works. Always use the class name:
TestUtil.runStaticMethod(); // The correct way ✅
2️⃣ Non-static methods require an actual instance. Calling them on null will throw a NullPointerException.
3️⃣ Understanding the difference between static and instance methods is crucial to avoid tricky bugs.
Conclusion
Java is full of interesting behaviors, and this is one of those moments that makes you pause and go, "Wait, what just happened?" 😆
Next time you see a NullPointerException, remember—sometimes it decides to take a day off! 🚀
Have you encountered this behavior before? Share your thoughts in the comments!