Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Understanding NullPointerException And How To Avoid It In Java/Selenium

Posted on 04/23/2025 By admin

Hello Folks,

Very common question asked by people:

“I am getting NullPointerException in automation script. I am correctly using selenium concepts but unable to resolve it.”

So, in this post, we will see what is reason behind above exception and how to solve it.

What is NullPointerException?

When we declare a reference variable, internally a pointer is created to reference variable.

For example:

Suppose Demo is a class and we create an object of class Demo as below:

Demo d= new Demo();

See the below image:

Now consider below lines of code:

Demo d1;

Above, I just declared a reference variable/object of Class Demo. I am not initialing it. When a reference variable is not initialized, it points to NULL. It means reference variable is pointing to nothing.

And when we want to perform any action on such reference variable, we get  NullPointerException. 

We will see an example below:

Output:

We perfectly declared and initialized np1 and called print method on it. It printed output successfully. And we just declared np2 and did not initialized it. So np2 is pointing to null. When we call print method on it, we got NullPointerException.

As per JAVADOC:

NullPointerException is thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

I can list out some more reasons of NullPointerException from here:

1) Invoking methods on an object which is not initialized 2) Parameters passed in a method are null 3) Calling toString() method on object which is null 4) Comparing object properties in if block without checking null equality 5) Incorrect configuration for frameworks like spring which works on dependency injection 6) Using synchronized on an object which is null

7) Chained statements i.e. multiple method calls in a single statement

Uncategorized

Post navigation

Previous Post: Selenium: Interview Questions
Next Post: Git Tutorial 26 – How To Ignore Already Indexed/staged/Committed/Pushed File to Git Repo

Related Posts

TestNG Tutorials 41: Skipping a Test Conditionally in TestNG | Make Selenium Easy Uncategorized
TestNG Tutorials 61: Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnGroup Uncategorized
TestNG Tutorials 50: DataProvider in TestNG – Understand Basics of DataProvider & How It Works Uncategorized
September 19, 2017 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Selenium Topics – Page 37 Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark