How to handle NullPointerException in Java
Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are-
- Exception
- Error
Exception: If you are doing any wrong things in a program, then the exception occurs. If there are no wrong things, then there is no exception is appears. The exception can handle by exception handling tools.
Types of exception: The exception is mainly five types. These are –
- RuntimeException
- Input outputException
- SQLException
- InterruptedException
- ClassNotFoundException
RuntimeException: RuntimeException is also divided into many parts. These are:
- ArithmeticException
- NullPointerException
- NumberFormatException
- IndexOutOfBoundsException
- ArrayIndexOutOfBoundsException
- StringIndexOutOfBoundsException
NullPointerException: Here, we mainly discuss the NullPointerException and know how we can handle the NullPointerException in Java. The compiler prevents this exception in a few cases with the compile-time mistake "The variable won't be initialized". In contrast, a null reference variable is exceeded controversially:
String s;
foo(s);
NullPointerException is thrown whilst software tries to apply an item reference that has the null value. Those may be:
- Invoking a way from a null item.
- Getting access to or editing a null object’s discipline.
- Taking the length of null, as though it have been an array.
- Getting access to or modifying the slots of null object, as if it have been an array.
- Throwing null, as if it have been a Throwable fee.
- When you try to synchronize over a null item.
What is the need of null value?
The null value is a unique price used in java. It's explicitly used to signify that no fee is assigned to a reference variable. One utility of null is enforcing facts structures like related listing and tree. Different applications consist of a Null object sample (See this for details) and a Singleton sample. The Singleton sample guarantees that the best example of a category is created and pursued to supply a global factor of getting admission to the object. A sample manner to create at maximum one example of a class is to claim all its constructors as non-public and then create a public technique that returns the precise instance of the elegance.
Avoid the NullPointerException: Here, we discuss how the NullPointerException is avoided in java. To prevent the NullPointerException, we should ensure that every object is initialized nicely, earlier than you use them. When we claim a reference variable, we must verify that item isn't null before we request a technique or an area from the gadgets.
Example 1: Here we discuss the example of NullPointerException in Java.
class NullPointerException
{
public static void main (String[] args)
{
String str = null;
try
{
System.out.println (str.toUpperCase());
}
catch (NullPointerException n)
{
System.out.println (“null can not be printed”);
}
}
}
Output: We compile the program and also run this program. After compilation the result is:
null can not be printed
Example 2: Here we discuss the example of NullPointerException in Java. The ternary operator can be used to keep away from NullPointerException. First, the Boolean expression is evaluated. If the expression is authentic then, the value1 is lower back, in any other case, the value2 is back. we will use the ternary operator for dealing with null suggestions:
import java.io.*;
class NullPointerException
{
public static void main (String[] args)
{
String str = null;
String message = (str == null)? “” ;
str.substring(0,8);
System.out.println (message);
str = “NullPointerException”
message = (str == null) ? “” : str.substring(0,8);
System.out.println (message);
}
}
Output: We compile the program and also run this program. After compilation the result is:
Null
So, here we discuss how we can handle the NullPointerValue in java. We are talking about what exception is and how many types of exceptions exist. We write two programs in java on the topic NullPointerException and share the output of the given programs.