Backend Development 5 min read

Understanding and Preventing NullPointerException in Java Backend Development

This article presents a real-world Java backend case where a newly hired developer caused a NullPointerException during data matching and batch insertion, analyzes each line of the problematic code, and offers defensive programming techniques such as null checks, Optional, and utility methods to avoid NPEs.

Java Captain
Java Captain
Java Captain
Understanding and Preventing NullPointerException in Java Backend Development

Introduction

Hello everyone, I'm the team leader and I will share a real case to deepen understanding of NullPointerException.

A newly hired intermediate Java developer was assigned a simple iteration: match third‑party data with internal channel settings and batch insert the results.

After deployment the matching logic threw an NPE, which is a basic mistake for a mid‑level developer.

Incident Reproduction

1. Pseudocode

The following pseudocode reproduces the issue (note: not the exact production code).

// backend configured channel
String channelNo = channelDao.getOne().getChannelNo();
// third‑party data
List<ThirdData> thirdDataList = httpClientUtils.getThirdDatas(DateUtils.today());
// filter matching
thirdDataList.stream().filter(o -> channelNo.equals(o.getChannelNo())).collect(Collectors.toList());
// batch insert
thirdDataDao.saveAll(thirdDataList);

2. Analysis and Solutions

The four lines hide three potential NPEs.

3. First line analysis

If channelDao.getOne() returns null , calling getChannelNo() triggers an NPE.

Solution: defensive programming – check for null before use.

// if channelNo is required, return early
Channel channel = channelDao.getOne();
if (channel == null) {
return;
}

Alternative: use ternary operator to supply an empty string.

String channelNo = channelDao.getOne() == null ? "" : channelDao.getOne().getChannelNo();

Or use Optional to provide a default.

String channelNo = Optional.ofNullable(channelDao.getOne()).orElse("");

4. Second line analysis

If thirdDataList is null , invoking stream() causes an NPE.

Solution: check collection emptiness before processing.

// recommended defensive check
if (CollectionUtils.isEmpty(thirdDataList)) {
return;
}

Another option (less recommended) is to wrap the logic in an if block.

if (CollectionUtils.isNotEmpty(thirdDataList)) {
// execute subsequent logic
}

5. Third line analysis

If channelNo is null , the expression channelNo.equals(o.getChannelNo()) throws an NPE.

Fixes include:

Explicit null check: channelNo != null && channelNo.equals(o.getChannelNo())

Use Objects.equals(channelNo, o.getChannelNo()) which handles null safely.

Leverage utility libraries such as StringUtils or StrUtil .

Conclusion

The article also recommends the SonarLint plugin (and its server counterpart SonarQube) for real‑time detection of issues like NPEs in IDEA.

JavaBackend DevelopmentOptionalnullpointerexceptiondefensive programming
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.