1

New to automation. Help is greatly needed.

Why am I getting a NullPointerException?

Error: Null pointer access: The variable driver can only be null at this 
     location

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import common.controls.PageVerified;


public class Google extends PageVerified{
        PageVerified PgVerif = new PageVerified();
        WebDriver driver = new ChromeDriver();
    
        
        @Test
            public void Login() throws Exception{
            driver.manage().window().maximize();
            driver.get("https:Google.com");
            driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Test");
            Thread.sleep(2000);
            driver.findElement(By.className("lsb")).click();
            PgVerif.Verify();
            String a = driver.getTitle();
            System.out.println(a);
            driver.findElement(By.xpath("//*[@id='lst-ib']")).clear();
            driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Test");
            Thread.sleep(2000);
            driver.findElement(By.className("sbico-c")).click();
            String b = driver.getTitle();
            System.out.println(b);
            
            driver.close();
        }
            
        

Following is another class that I am calling where I get the error and it is in separate package than the above.

package common.controls;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class PageVerified 
{
    public String Verify()
        {
            WebDriver driver = null ;
            String pagetitle = driver.findElement(By.xpath("//*[@id='gb_70']")).getText();
                if(pagetitle != null)
                {
                    System.out.println(pagetitle + "Verified Successfully"  );
                    return pagetitle;
                }
                else
                {
                    System.out.println("Error::Not Verified");
                }
                return null;
        }   
}
8
  • 1
    This post was asked and answered on StackOverflow. In your Verify method, you define the driver as null, then you immediately try to use it on the next line as if it was initialized, and it's not, it's null. This was explained in the other post, or is this some example from a web site somewhere that someone else is also using (not your own code?) Commented Feb 26, 2018 at 15:43
  • @BillHileman: Yes i understand that you define the driver as null, then you immediately try to use it on the next line as if it was initialized, and it's not, it's null but i am not able to resolve it, what would i do to resolve it?
    – Kkat
    Commented Feb 26, 2018 at 15:51
  • @BillHileman - can you link to the answer, so we can close it here if in fact it is cross-posted? Commented Feb 28, 2018 at 15:46
  • @Kkat - If you did not understand SO answer, better is asking additional questions there. This looks like programming question. Seems that you just need to upgrade your coding skills - follow some tutorials, if you are not ready to debug your code. Bill explained it right. Commented Feb 28, 2018 at 15:51
  • 1
    @BillHileman - IMHO by providing code snippets which OP can copy-paste without understanding, poster provide OP dis-service: reinforce OP's cargo-cult approach without adding the understanding which OP lacks. Your answer is the only one trying to explain, not to provide a snippet to copy-paste. Commented Feb 28, 2018 at 16:22

2 Answers 2

1

You were getting null pointer exception because, though the driver is initialized in Google class.In this line PgVerif.Verify(); Control shifts to verify() method and in that method you are declaring another variable for driver (like WebDriver driver=null;). So a new variable driver is created (and this variable is not the previously initialized one from Google class).

So there are two things that you can do.

  1. While creating the PageVerified instance you can send the initialized driver instance into its constructor and use it in the PageVerified class.

    public class Google
    {
      WebDriver driver = new ChromeDriver();
      PageVerified PgVerif = new PageVerified(driver);
    ...
    }
    
    public class PageVerified 
    {
     public WebDriver driver;
    
     public PageVerified(WebDriver driver)
     {
             this.driver = driver;
     }
    
     public String verify()
     {
        String pagetitle = driver.findElement(By.xpath("// [@id='gb_70']")).getText();
     ...
     }
    }
    
  2. Send the initialized driver directly into the verify() method after initialization.

    public class Google
    {
      WebDriver driver = new ChromeDriver();
      PageVerified PgVerif = new PageVerified();
      ...
      PgVerif.verify(driver);
    }
    
    public class PageVerified 
    {
     public String verify(WebDriver driver)
     {
        String pagetitle = driver.findElement(By.xpath("// [@id='gb_70']")).getText();
           ...
     }
    }
    
1

You can try as per below: public WebDriver driver;

public BasePage(WebDriver driver) {

    PageFactory.initElements(driver, this);
    this.driver = driver;

}

public void verifyTitle(String etitle) {

    WebDriverWait wait = new WebDriverWait(driver, 10);
    try {
        wait.until(ExpectedConditions.titleIs(etitle));
        Reporter.log("Title is Matching: " + etitle, true);
    }

    catch (Exception e) {
        Reporter.log("Title is not Matching: " + etitle, true);
        Assert.fail();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.