Debugging NPEs in Spring Boot: Real‑World Case Study and Fixes
A junior Java developer encounters a NullPointerException while integrating third‑party data into a Spring Boot backend, and the article walks through reproducing the bug, analyzing each faulty line, and presenting defensive‑programming, ternary, Optional, and utility‑class solutions plus a SonarLint recommendation.
Introduction
A new intermediate Java developer was assigned a simple task: pull data from a third‑party source, match it to internal channels, aggregate into a list, and batch insert into the database. After deployment, a NullPointerException (NPE) occurred.
Accident Reproduction
Pseudo Code
说明 :The pseudo code is not real production code but illustrates the NPE scenario. The real business logic is more complex, and NPEs often hide deep in the code.
String channelNo = channelDao.getOne().getChannelNo();
List<ThirdData> thirdDataList = httpClientUtils.getThirdDatas(DateUtils.today());
thirdDataList.stream().filter(o -> channelNo.equals(o.getChannelNo())).collect(Collectors.toList());
thirdDataDao.saveAll(thirdDataList);Analysis and Solutions
First Line Analysis
If channelDao.getOne() returns null, calling getChannelNo() triggers an NPE.
Solution 1 – Defensive Programming (recommended)
// Defensive check
Channel channel = channelDao.getOne();
if (channel == null) {
return;
}Solution 2 – Ternary Operator
String channelNo = channelDao.getOne() == null ? "" : channelDao.getOne().getChannelNo();Solution 3 – Optional
String channelNo = Optional.ofNullable(channelDao.getOne()).orElse("");Third Line Analysis (1)
If thirdDataList is null, invoking stream() causes an NPE.
Solution – Collection Null/Empty Check
// Recommended: use collection utility to check emptiness
if (CollectionUtils.isEmpty(thirdDataList)) {
return;
}Third Line Analysis (2)
If channelNo is null, the expression channelNo.equals(o.getChannelNo()) throws an NPE.
Solution 1 – Explicit Null Check
channelNo != null && channelNo.equals(o.getChannelNo())Solution 2 – Objects.equals
Objects.equals(channelNo, o.getChannelNo())Solution 3 – Utility Libraries
// Apache Commons Lang
StringUtils.equals(channelNo, o.getChannelNo());
// Hutool
StrUtil.equals(channelNo, o.getChannelNo());Final Recommendation
Install the SonarLint plugin for IntelliJ IDEA. It dynamically detects code risks such as NPEs and provides real‑time feedback. Its server counterpart is SonarQube.
Author: l拉不拉米 (Source: juejin.cn)
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
