1. Preface
Hello Hello everyone, I am teacher long . Today I bring a real case to let everyone have a deeper understanding of the null pointer exception.
company just joined an intermediate Java developer. After a week of adaptive learning, it performed well in all aspects, so it allocated a small iteration for newcomers to do.
requirements are very simple. After matching the data pulled from a third party to the channels set by the company's backend, they are aggregated into a list and batched into the database.
However, in the matching logic, an NPE was reported after it was launched. This is a simple mistake that an intermediate developer should not make. I was scolded hard by the newcomer and remembered a production accident once.
2. Accident recurrence
1. Pseudocode
Description: pseudocode is not real online code, but is written for more convenient and vivid reproducing the accident scene. Real business scenarios are often more complex, and NPE vulnerabilities are hidden deeper, not easy to code view, and not easy to test; NPE in production environment is a more common exception. I hope everyone will not worry about why the test has not been tested. The key is to understand the causes and solutions of NPE through such a case.
// Channel set in the background
String channelNo = channelDao.getOne.getChannelNo;
// Data pulled by third parties
ListThirdData thirdDataList = httpClientUtils.getThirdDatas(DateUtils.today);
// Match filter
thirdDataList.stream.filter(o -channelNo.equals(o.getChannelNo)).collect(Collectors.toList);
// Batch library
thirdDataDao.saveAll(thirdDataList);
2. Analysis and solution
Students with experience and solid skills should be able to find problems more or less when they see this. In fact, these four codes were carefully designed by the author, and they can be said to be .
has just four lines of code and has gathered 3 NPEs. I'm hanged up~~
We analyze line by line:
3, first line analysis
channelDao.getOne If the return is, then calling getChannelNo will report NPE.
4, solution
1. Use defensive programming and return in advance (it depends on the specific business scenario)
// If channelNo is a necessary element for method logic execution, it is recommended to use this method
Channel channel = channelDao.getOne;
if (channel == ) {
return;
}
2. Use three-item operation to return an empty string ("")
// Return the bottom-line empty string
String channelNo = channelDao.getOne == ? "" : channelDao.getOne.getChannelNo;
3. Use the Optional function to return an empty string ("")
String channelNo = Optional.ofable(channelDao.getOne).orElse("");
5. Third line analysis (1) If
thirdDataList is, then calling stream will report NPE.
You can know the reason through the following source screenshot:


6, solution
1, use defensive programming, return in advance (recommended)
// It is recommended to use the collection tool class to judge empty
if (CollectionUtils.isEmpty(thirdDataList)) {
return;
}
2, use if conditional statement to wrap (not recommended)
if (CollectionUtils.isNotEmpty(thirdDataList)) {
// Execute the following logic
}
7, analyze the third line (2)
channelNo If it is returned, then executing channelNo.equals(o.getChannelNo) will report NPE.
We know that according to Java's specifications, the call to String's equals method requires that the left side be determined, in order to avoid the caller's condition.However, the caller and equals entry parameters are both variables. What should I do in this case?
1. Add another sentence to judge:
channelNo != channelNo.equals(o.getChannelNo)
2. In fact, you can use the equals method of the Objects class under java.uti package
Objects.equals(channelNo, o.getChannelNo)
See the source code at a glance. This method makes non-empty judgments on the object on the left

3, use other open source tool library or implement
by yourself, such as:
org.apache.commons.lang3.StringUtils
cn.hutool.core.util.StrUtil;
Original link: juejin.cn/post/7031445206152577061
Original author: l Labulami