Null Pointers are a basic exception that we Java developers often encounter, an extremely common but seemingly incurable problem.
In this article, stack length will show you what null Pointers are and how to avoid them effectively.
What is a null pointer?
When a variable has a value of null, in Java it represents an empty object that does not exist, has no actual content, and has no memory allocated to it. Null is also the default value of an object’s member variables.
So, if you call a method or variable of an object that has not been initialized, you will get a null pointer exception.
A null pointer exception occurs in the following example:
Object object = null;
String string = object.toString();
Copy the code
A null pointer is a subclass of RuntimeException. It is not captured, but can be reported only when the program is running, and can cause program interruption.
See this article: A map to figure out the Java exception mechanism.
How do I avoid null Pointers?
Here are some of the most common cases of null Pointers and how to resolve them.
1, string comparison, constant first
if(status.equals(SUCCESS)){
}
Copy the code
In this case, status may be null, causing null-pointer exceptions. The constant should be placed first to avoid null-pointer exceptions.
if(SUCCESS.equals(status)){
}
Copy the code
This should be mentioned in various development specifications, and is the most basic.
2. Initialize the default values
Give an object a default value or a default constructor implementation when it is initialized, such as:
User user = new User();
String name = StringUtils.EMPTY;
Copy the code
Return the empty collection
When a collection is returned, the default is null, and the uniform specification returns an empty collection.
Take a List of examples.
public List getUserList(){
List list = userMapper.gerUserList();
return list == null ? new ArrayList() : list;
}
Copy the code
In this way, the recipient does not have to worry about null-pointer exceptions and does not affect the business.
4, assertions
Assertions are used to check the security of a program. Conditions are checked before use. If conditions are not met, an exception is reported.
The assert keyword comes with Java, for example:
assert name == null : "Name cannot be empty";
Copy the code
Output:
Exception in thread "main"Java. Lang. AssertionError: name is not correctCopy the code
However, assertion checking is not enabled by default and requires the JVM parameter -enableassertions to be effective.
This is rarely used in Java. It is recommended to use Spring, which is more powerful and convenient to use.
Use in Spring:
Assert.notNull(name,"Name cannot be empty");
Copy the code
5, Optional
Optional is a new feature in JDK 8. = null; this is very useful when multiple subobjects within an object are checked continuously.
We won’t go into details here, but look at this article: Optional new JDK8 feature.
There are probably five of them, but there are many more. How to avoid null Pointers is to pay attention to code specifications and improve code literacy. Reply keyword: Java in the background of wechat public account of Java technology stack, you can get more dry products of Java series technology with stack length sorted.
Welcome to share your comments!
Follow the wechat public account of Java technology stack, stack leader will continue to share Java dry goods tutorial, the public account will be the first time to push, continue to pay attention to. In the public account background reply: Java, get stack length arrangement of more Java tutorial, are actual combat dry goods, the following is only part of the preview.
- Do you really understand the transient keyword?
- 1. Synchronized?
- With Java 11 released, Strings can still play like this!
- Are Strings in Java really immutable?
- Five differences between sleep() and wait()
- …