CSS font stacks & developer FAQs with Web standards
Site navigation below

One of hundreds of answers available with our premium content service.

Part answer

A: The best way to handle a NullPointerException is to prevent one being thrown to begin with. A NullPointerException happens when a null value is assigned to an object type variable, then a method is called on that variable. The variable is expected to hold a “pointer” or reference to an object, but actually holds a null value, hence “null pointer”.

This type of exception can also happen when you try to get or set a field on a null object reference, operate on the reference as if it was an array, or throw it as if it was an exception.

A NullPointerException is a runtime exception that you should not try to catch. Avoid the exception by checking the value before a call, as in the example below with the System.getProperty(String) method. The getProperty() method is expected to return a String value for a given system property, or null if the property does not exist.

… full answer hidden

Click for full answers

Premium members click below for full answer
What's the best way to handle a NullPointerException?

Home · Web fonts · Font stacks · FAQs · Java · CSS · Javascript · HTML · Site manager