Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday 23 May 2021

What is NullPointerException in Java with Examples

 Java "NullPointerException" Program

Here we are going to learn what NullPointerException in java with an example. So let's start the simple java null pointer exception program.

When we call any method by using any reference which is assigned null value then nullpointerexception occurs in java. Let's take a simple example of Java NullPointerExcepton which is given below.

NullPointerException Example in Java

class NullPointerExceptionExample
{
public void show()
{
System.out.println("not running");
}
public static void main(String args[])
{
NullPointerExceptionExample npl = null;//assigning null value to this reference
npl.show();
}
}

Now above java program will throw NullPointerException.

So, here we learned what is null pointer exception in java and when it occurs in java programs.


Share:

2 comments:

  1. Happy Morning team, this was Great article! Your example code provided helps to understand NullPointerException in Java in a simple and easy-to-follow manner. The clear explanation of when and why this exception occurs will help developers to avoid it in their code. Thanks for sharing this article. Refer Infycle Technologies.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate