Null Pointer Exception in Java

A NullPointerException is thrown when a Java program attempts to process an object which contains a null value.

public class Example {
public void doSomething() {
Integer number = null;
if (number > 0) {
System.out.println("Positive number");
}
}
}

In the example above, the number (Integer) object is null, so performing a simple evaluation will throw a NullPointerException.

--

--

--

Tech enthusiastic, life explorer, single, motivator, blogger, writer, software engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Understanding Date and Time In Java

Important Things about Apache Spark

Do’s and Don’ts for performance best practices in flutter/dart

Master the art of Dynamic programming

Testing FCM Notifications? Like a pro!

Bring Test Engineering into your DevOps practice — Honeycomb

Printer with a piece of paper on it that reads test in bold capital letters

April 2019: ARK GitHub Development Bounty Program

Model Driven App: How to dynamically create a HTML table from the sub grid in the Email Template

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TechGuy

TechGuy

Tech enthusiastic, life explorer, single, motivator, blogger, writer, software engineer

More from Medium

Java Unit Test All-in-One

Playing practically with Stream API

A Beginner’s Guide to How to Code in Java

Programming in Java - Annotations