Junior — Middle
What causes a NullPointerException error in a program?
sobes.tech AI
Answer from AI
A NullPointerException in Java occurs when the program tries to invoke a method or access a field on an object that is currently null. For example, if you have a reference variable that does not point to a real object, calling a method on it will throw this error.
Example:
String s = null;
int length = s.length(); // NullPointerException here
To avoid this error, check objects for null before using them or initialize them properly.