Why Did My New Java Developer Trigger NPE? Lessons & Fixes
A new Java developer caused a production NullPointerException while matching third‑party data, and the article walks through reproducing the bug, analyzing each faulty line, and applying defensive programming techniques such as early returns, Optional, Objects.equals, and collection checks to prevent similar crashes.
Introduction
A newly hired mid‑level Java developer was assigned a simple task: fetch third‑party data, match it against internal channel settings, aggregate the results, and batch insert them. After deployment, a NullPointerException (NPE) occurred, highlighting a basic mistake that should not happen at this level.
Bug Reproduction
Pseudo Code
The following pseudo code illustrates the core logic (note: it is not the exact production code, but it helps reproduce the issue):
// Backend channel configuration
String channelNo = channelDao.getOne().getChannelNo();
// Third‑party data fetch
List<ThirdData> thirdDataList = httpClientUtils.getThirdDatas(DateUtils.today());
// Matching filter
thirdDataList.stream().filter(o -> channelNo.equals(o.getChannelNo())).collect(Collectors.toList());
// Batch insert
thirdDataDao.saveAll(thirdDataList);Analysis and Solutions
Experienced developers can spot the problems quickly. The four lines contain three potential NPE sources.
1. First Line
If channelDao.getOne() returns null, calling getChannelNo() throws an NPE.
Solutions:
Defensive programming with early return:
Channel channel = channelDao.getOne();
if (channel == null) {
return;
}Use a ternary operator to fallback to an empty string:
String channelNo = channelDao.getOne() == null ? "" : channelDao.getOne().getChannelNo();Leverage Optional:
String channelNo = Optional.ofNullable(channelDao.getOne()).orElse("");2. Second Line
If thirdDataList is null, invoking stream() causes an NPE.
Solutions:
Defensive check using collection utilities (recommended):
if (CollectionUtils.isEmpty(thirdDataList)) {
return;
}Wrap with an if condition (not recommended):
if (CollectionUtils.isNotEmpty(thirdDataList)) {
// proceed with logic
}3. Third Line
If channelNo is null, the expression channelNo.equals(o.getChannelNo()) throws an NPE.
Solutions:
Explicit null check:
channelNo != null && channelNo.equals(o.getChannelNo())Use Objects.equals which handles nulls gracefully:
Objects.equals(channelNo, o.getChannelNo())Adopt third‑party utilities such as StringUtils or StrUtil.
// Example using Apache Commons StringUtils
StringUtils.equals(channelNo, o.getChannelNo());Conclusion
To catch such issues early, the article recommends the IDEA plugin SonarLint , which provides real‑time code‑quality analysis and flags risky patterns like potential NPEs. Its server counterpart, SonarQube, offers deeper inspection capabilities.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
