Java Nullpointerexception
Tags: codenewbie, java, null
Published: 2020-03-16 17:19:38

This blog will be a quick look at the Java language and its Nullpointerexception.
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/3/30/Java_programming_language_logo.svg/1200px-Java_programming_language_logo.svg.png" width="100" height="200" />

# What is Java?
Developed by James Gosling at Sun Microsystems (since been acquired by Oracle) and released in 1995; Java is a "general-purpose, concurrent, strongly typed, class-based object-oriented language" that is intended to let application developers write a program once, and run it anywhere on all platforms that support Java. (3)

That sounds great right? Run anywhere that supports Java. This means that it can be created on a Windows computer and run on a Mac or Linux computer, so it is a very versatile language.









# Where do you see Java?
Because Java is a "Write Once, Run Everyone" language, it is used all over. To name a few... 
It is commonly used as the programming language for Android applications and can be seen in major projects that were written using Java, including processing frameworks such as Yarn and Hadoop, as well as Microservices development platforms and integration platforms. It can be used in the cloud where developers can build, deploy, debug, and monitor Java applications on Google Cloud at a scalable level. And, frameworks such as Struts and JavaServer Faces all use a Java servlet to implement the front controller design pattern for centralizing requests.

![](https://mindmajix.com/blogs/images/Hadoop%20Yarn.JPG)








# So what is a Java NullPointerException?
NullPointerException is a Runtime Exception that is expected to crash or break down the program/application when it occurs. 
![](https://cdn.journaldev.com/wp-content/uploads/2017/07/java-nullpointerexception-hierarchy.png)
With Java, a special null value can be assigned to an object reference (a way to keep memory address information of an object which is stored in memory). The NullPointerException is thrown by the Java Virtual Machine when a program performs some operations on a certain object considered as null or is calling for some method on the null object.

![NullPointerException from JournalDev](https://dev-to-uploads.s3.amazonaws.com/i/m3pgxxi93krztwgktpxm.png)(5)

__Some of the common reasons for NullPointerException are:__(5)
 - Invoking a method on an object instance, but at runtime the object is null.
 - Accessing variables of an object instance that is null at runtime.
 - Throwing null in the program as if it were a Throwable value.
 - Accessing index or modifying value of an index of an array that is null.
 - Checking length of an array that is null at runtime.
 - Accessing or modifying the field of a null object.
 - When you try to synchronize over a null object.











# How to prevent the NullPointerExcetion?
Because a NullPointerException is an unchecked exception, we don’t have to catch it. Instead, usually the NullPointerExceptions can be prevented using null checks and preventive coding techniques.

This includes:
 - Ensuring that all the objects are initialized properly, before use.
 - The use of `String.valueOf()` rather than `toString()` method.
 - Use of ternary operators.
 - Add a null check for an argument and throw a "IllegalArgumentException" if required.
 - Write methods returning empty objects rather than null wherever possible.






<hr>

Happy Hacking
![](https://media.giphy.com/media/l3vRmVv5P01I5NDAA/giphy.gif)

#### References 
1. https://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html
2. https://www.geeksforgeeks.org/null-pointer-exception-in-java/
3. https://docs.oracle.com/javase/8/docs/technotes/guides/language/index.html
4. https://www.tutorialspoint.com/how-to-handle-the-runtime-exception-in-java
5. https://www.journaldev.com/14544/java-lang-nullpointerexception
6. https://www.theserverside.com/definition/Java



###### Please Note that I am still learning. If something that I have stated is incorrect please let me know. I would love to learn more about what I may not understand fully.