Null Pointer Exception When Reading File Java

Table of Contents

  • Reasons for NullPointerException
    • 1. Invoking the case method of a null object
    • 2. Accessing or modifying the field of a null object.
    • three. Taking the length of null every bit if it were an array.
    • 4. Accessing or modifying the slots of null as if information technology were an array.
    • 5. Throwing zero as if information technology were a Throwable value.
    • 6. When yous add aught to Collections which do not allow nulls such as ConcurrentHashMap
    • vii. Trying to synchronize using nix object.
  • Detection of NullPointerException
  • Prepare NullPointerException
    • one. Null bank check
    • 2. Do null bank check as well before putting key or value in Collection which do non let null
  • Best practices for avoiding NullPointerException
    • 1. Cord comparing
    • two. Employ Optional
    • 3. Use ternary opertor
    • 4. Go along check on arguments of method
    • 5. Use StringUtils from Apache Common

In this tutorial, we will see the most frequent java exception i.e. Exception in thread main java.lang.NullPointerException. It is one of the Runtime Exception.

Heighten your manus if yous ever got NullPointerException while working on java lawmaking. I am certain that this is the exception you might accept encountered most oft. You might likewise agree that NullPointerException is hurting for nigh of coffee developers(novice or expert), but there is not much nosotros can practice almost them, because this is the price we need to pay for convenient or unavoidable cypher references.

NullPointerException

Reasons for NullPointerException

Reasons for NullPointerException equally defined by Java docs.

  • Invoking the instance method of a null object.
  • Accessing or modifying the field of a nix object.
  • Taking the length of null as if it were an assortment.
  • Accessing or modifying the slots of zilch as if it were an array.
  • Throwing nix as if it were a Throwable value.
  • When yous add null to Collections which do not allow nulls such every bit [ConcurrentHashMap](https://java2blog.com/concurrenthashmap-in-java/ "ConcurrentHashMap")
  • Trying to synchronize using goose egg object.

Let's sympathize each of these in item. I am creating a Employee class which nosotros are going to use in our examples.

1. Invoking the example method of a nada object

This is most obvious one.

When you lot run above program, y'all will get below output.

Exception in thread "chief" java.lang.NullPointerException
at org.arpit.java2blog.InvokingMethodOnNullMain.main(InvokingMethodOnNullMain.java:7)

As you tin can come across, e1 is null at line 7 and nosotros are calling getMethod on it, thats why we are getting Exception in thread " main java.lang.NullPointerException here.

two. Accessing or modifying the field of a nada object.

It is very much similar to calling instance method on cypher.

When you lot run to a higher place programme, you will get below output.

Exception in thread "master" java.lang.NullPointerException
at org.arpit.java2blog.NullFieldExampleMain.master(InvokingMethodOnNullMain.java:8)

As you can see, e1 is null at line eight and we are accessing name field of e1, thats why we are getting NullPointerException hither.

three. Taking the length of null as if it were an array.

When you try to get length of null array, you will get NullPointerException.

When you run higher up programme, yous will get below output.

Exception in thread "main" java.lang.NullPointerException
at org.arpit.java2blog.NullArrayLengthMain.chief(InvokingMethodOnNullMain.java:9)

4. Accessing or modifying the slots of null as if it were an assortment.

When y'all try to access or modify null slot in array.Allow's understand with the aid of example.

When you lot run above program, you will get beneath output.

Exception in thread "main" java.lang.NullPointerException
at org.arpit.java2blog.InvokingMethodOnNullMain.chief(InvokingMethodOnNullMain.java:8)

v. Throwing null as if it were a Throwable value.

When you lot throw zip as Throwable.

When you run in a higher place program, you lot volition go below output.

Exception in thread "main" java.lang.NullPointerException
at org.arpit.java2blog.InvokingMethodOnNullMain.main(InvokingMethodOnNullMain.java:7)

six. When yous add goose egg to Collections which do not allow nulls such equally ConcurrentHashMap

When you run above programme, you will get below output.

Exception in thread "chief" java.lang.NullPointerException
at coffee.base/coffee.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1022)
at java.base/java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.coffee:1017)
at org.arpit.java2blog.InvokingMethodOnNullMain.primary(InvokingMethodOnNullMain.java:10)

7. Trying to synchronize using null object.

When y'all synchronize on zippo object, you will go NullPointerException

When you run higher up program, you volition get below output.

Exception in thread "main" java.lang.NullPointerException
at org.arpit.java2blog.InvokingMethodOnNullMain.primary(InvokingMethodOnNullMain.java:10)


Detection of NullPointerException

It is easier to observe NullPointerException. Bank check exception trace, it volition tell y'all class name, method name and line number.Become to that line number and bank check what can be the reason for NullPointerException.


Fix NullPointerException

Permit's take above scenrios and fix NullPointerException.

1. Null check

bank check null before calling method or field.

When yous run above program, you won't become NullPointerExcpetion.

ii. Do null check every bit well before putting primal or value in Drove which exercise not let nothing


Best practices for avoiding NullPointerException

one. Cord comparison

This is almost frequent reason for Exception in thread main coffee.lang.NullPointerException, let's sympathise with the help of example.

Equally nosotros did not gear up name of Employee e1, we will get NullPointerException here.
When yous run above program, yous will get beneath output.

Exception in thread "chief" java.lang.NullPointerException
at org.arpit.java2blog.StringComparisonMain.primary(StringComparisonMain.java:viii)

You can change logic as below.

This volition avoid Exception in thread main java.lang.NullPointerException.
Please note that it may cause unexpected beliefs due to null. If proper noun cannot be zippo at all for Employee, and then don't use in a higher place method as it will ignore null names in this case.

ii. Apply Optional

Java 8 has introduced a new form chosen Optional.In general, we do not observe any value in method, we return null from it and it becomes hurting for caller to check for null to employ information technology.
For case:

Every bit y'all can encounter, if we did not find employee in employeeList, nosotros are returning nothing from findEmployee() method. The caller will become  employee object from findEmployee() method and may call getName() method which volition in plough raise NullPointerException.You tin apply Optional to avoid such situations.

There is no employee with name John

Information technology gives indication to caller than returned value can be null.

3. Use ternary opertor

You can employ ternary operation to check for null.

As you can see, we won't become NullPointerException hither.

4. Keep check on arguments of method

5. Utilize StringUtils from Apache Common

You can use StringUtils class to take care of lots of String nix and empty Cord check. Sometimes you need to cheque if String is null or empty, you can utilise isEmpty method from StringUtils to take care of information technology.

That's all about Exception in thread "main" java.lang.NullPointerException in coffee.

rastmaked1938.blogspot.com

Source: https://java2blog.com/exception-thread-main-java-lang-nullpointerexception/

0 Response to "Null Pointer Exception When Reading File Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel