Java Null Lang Pointer Exception
Last Updated : Mar 11, 2024
IN - Java | Written & Updated By - Anjali
In this article we will show you the solution of java null lang pointer exception, when a programme tries to access or modify a null object reference—one that doesn't point to any objects in memory—which means it doesn't point to any object at all—Java NullPointerException usually happens as a runtime exception.
If not handled appropriately, null pointer exceptions can be annoying and result in unanticipated programme crashes.
Java uses reference variables to access objects that are created dynamically. A reference variable is said to not refer to any objects when it is given the value null.
A NullPointerException is raised if code tries to execute operations on such a null reference. We'll talk about the java null lang pointer exception now.
Step By Step Guide On Java Null Lang Pointer Exception :-
The NullPointerException serves as a warning that the program's reasoning was flawed.
Programming mistakes such neglecting to initialise object references, not verifying for null before accessing an object, or invoking methods on null references are frequently the cause of it.
It is crucial to make sure that object references are correctly initialised before accessing them as well as include null checks in code where necessary to prevent NullPointerExceptions.
Writing strong and dependable Java programmes requires a thorough understanding of the reasons behind null pointer exceptions and effective management techniques.
import java.lang.NullPointerException; public class NullPointerExceptionExample { public static void main(String[] args) { String str = null; try { int length = str.length(); // This line will throw a NullPointerException System.out.println("Length: " + length); } catch (NullPointerException e) { System.out.println("NullPointerException caught!"); System.out.println("Error message: " + e.getMessage()); e.printStackTrace(); } finally { System.out.println("The 'finally' block is executed regardless of an exception"); } System.out.println("Program continues after the exception"); invokeAnotherMethod(); } public static void invokeAnotherMethod() { String anotherStr = null; try { printString(anotherStr); } catch (NullPointerException e) { System.out.println("NullPointerException caught in another method!"); System.out.println("Error message: " + e.getMessage()); } } public static void printString(String text) { System.out.println("Text: " + text); } }
- You can see that we wrote Java code to display the null lang pointer exception in this example.
- The first thing we do is import the Java.lang package's NullPointerException class.
- We may utilise the NullPointerException class directly in our code thanks to this import.
- It is declared and defined that the class NullPointerExceptionExample exists.
- It includes a main method, which acts as the program's entry point.
- We declare and initialize the String variable str with the value null inside the main function.
- This indicates that the str variable does not refer to any memory-based objects.
- The next step is to handle any potential NullPointerExceptions using a try-catch block.
- We use the str variable, which is null, to call the length() method within the try block. Intentionally throwing a NullPointerException is this line of code.
- We catch NullPointerExceptions in the catch block if they are thrown.
- The catch block prints the stack trace of the exception, notifies the user that a NullPointerException has been detected, and shows any error messages related to the exception.
- We add a finally block after the catch block.
- Whether or not an exception was thrown, the code included in the finally block is still carried out.
- In this case, it prints a message informing the user that the finally block always runs, regardless of errors.
- We display a message after the finally block to show that the programme continues after the exception.
- The invokeAnotherMethod() function is then used to show how to handle exceptions in a different method.
- We declare and initialise a String variable named anotherStr with null inside the invokeAnotherMethod() method.
- We execute the printString() method inside of a try block while supplying the anotherStr variable as an argument.
- The printString() method raises NullPointerException since anotherStr is null.
- If a NullPointerException occurs in the catch block, we show a message stating that a NullPointerException was actually caught in another method & display the error message connected with the exception (if any).
- The specified text is simply printed by the printString() function. Since the text is null in this instance, it will print Text: null.
Conclusion :-
As a result, we were able to understand the java null lang pointer exception.
We also discovered that null pointer exception-related unexpected programme crashes can be avoided by properly initialising object references and including null checks.
I hope this article on java null lang pointer exception helps you and the steps and method mentioned above are easy to follow and implement.