Backend Development 9 min read

Common Java Development Pitfalls: NPE, Collections, and Best Practices

This article discusses why hiring top engineers matters, reviews the Alibaba Java Development Manual, and details common Java pitfalls such as NullPointerExceptions, improper use of equals, map null handling, and collection operations, offering practical solutions and best‑practice recommendations for backend developers.

Java Captain
Java Captain
Java Captain
Common Java Development Pitfalls: NPE, Collections, and Best Practices

We should always hire the best engineers because a top engineer can accomplish the work of many ordinary ones; they need autonomy, motivation, and the freedom to work on what they love.

The author revisits the Alibaba Java Development Manual (now version 1.2), emphasizing its goal of reducing repetitive pitfalls and encouraging developers to document common mistakes.

1. Automatic unboxing NPE A single‑line method that creates a new User and immediately calls getId() can throw a NullPointerException if getId() returns null . int method() { return new User().getId(); }

2. Chained calls causing NPE Even after checking that a collection is not empty, individual elements may still be null , leading to NPE when chaining calls. static void method1() { List list = new ArrayList (); list.add(new User()); if (!CollectionUtils.isEmpty(list)) { for (User user : list) { System.out.println("userid:" + user.getId().toString()); } } }

3. Equals pitfalls Calling user.getName().equals("mafly") can throw NPE if getName() returns null . The safe pattern is "mafly".equals(user.getName()) .

4. Map NPE Both HashMap and ConcurrentHashMap behave differently with null keys/values. ConcurrentHashMap rejects null for both, while HashMap allows them, which can lead to NPE if not checked.

5. foreach removal Removing elements from a list inside a foreach loop causes ConcurrentModificationException . Use an Iterator and its remove() method instead. public static void main(String[] args) { List a = new ArrayList<>(); a.add("1"); a.add("2"); a.add("3"); for (String temp : a) { if ("2".equals(temp)) { a.remove(temp); } } Iterator it = a.iterator(); while (it.hasNext()) { String temp = it.next(); if ("2".equals(temp)) { it.remove(); } } }

6. Arrays.asList() limitations The list returned by Arrays.asList() is fixed‑size; calling add , remove or clear throws UnsupportedOperationException . Modifications to the original array are reflected in the list.

7. toArray() misuse Calling the no‑arg toArray() returns an Object[] . Casting it to a specific array type causes ClassCastException . Use the generic version <T> T[] toArray(T[] a) instead. String[] array = new String[list.size()]; array = list.toArray(array);

8. subList pitfalls A subList is a view of the original list; modifications to either list affect the other. Changing the size of the original list while iterating the sub‑list can trigger ConcurrentModificationException . Also, requesting a sub‑list larger than the original size throws an exception.

In summary, the article highlights a series of common Java exceptions and collection‑related mistakes, providing concrete code examples and clear remediation steps to help backend developers write safer, more reliable code.

Javabackend developmentBest PracticesCollectionsNPE
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.