Gareth D.
—You run your Java code and see an error similar to the following.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "myString" is null
How do you fix it and prevent similar errors from happening in future?
A NullPointerException
in Java is one of the most common errors. It means that you are trying to access a part of something that doesn’t exist.
For example, in the code below we call .length()
on myString
, which would usually return the length of the string. In this case, the string doesn’t exist (we set it to null
), and so this throws a NullPointerException
.
public class Main { public static void main(String[] args) { String myString = null; int stringLength = myString.length(); } }
You can fix this by ensuring that your object is not null
before calling the method. For example:
public class Main { public static void main(String[] args) { String myString = null; if (myString != null) { int stringLength = myString.length(); } else { System.out.println("myString is null"); } } }
Here we first ensure that myString
is not null
before calling length()
.
If it is, we print “myString is null” instead.
Of course, in real-world code it’s not normally as simple to identify which variables might be null
, as they might be passed in from other methods or classes, or be dependent on user input.
You can use some defensive programming techniques to avoid NullPointerExceptions such as always validating user input, and always checking if objects are null before calling their methods.
Sometimes it is also helpful to think about which variables might be null. If you are checking a user input variable against a constant, it is better to call the method from the constant instead of from the variable.
public class Main { public static void main(String[] args) { String userInput = null; System.out.println(userInput.equals("MY_VERY_SECRET_PASSWORD")); } }
This code throws a NullPointerException
, and if we’re not sure ahead of time what userInput
will be, we need to handle the case where it is null. We could add a null check as in the previous example, but a simpler way is to restructure the code as follows.
public class Main { public static void main(String[] args) { String userInput = null; System.out.println("MY_VERY_SECRET_PASSWORD".equals(userInput)); } }
Output:
false
In this case, we know that “MY_VERY_SECRET_PASSWORD” is not going to be null, as it is hardcoded, but we are not sure about userInput
. Because a.equals(b)
is the same as b.equals(a)
, we can simply swap the variables and operate on the one we know exists.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.