Skip to main content

New to automation. Help is greatly needed.

Anybody could help me with following code

Why i am I getting NullPointerExceptiona NullPointerException?

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

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

Following is another class that iI am calling where iI get the error and it is in seperate Pacakageseparate 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;
                        }   
}

New to automation. Help is greatly needed

Anybody could help me with following code

Why i am getting NullPointerException?

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

Following is another class that i am calling where i get error and it is in seperate Pacakage 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;
                        }   
}

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:

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;
        }   
}
Bumped by Community user
edited tags
Link
Kkat
  • 11
  • 2
Source Link
Kkat
  • 11
  • 2

Selenium: Getting error with webElement or Webdriver "Drive" Variable

New to automation. Help is greatly needed

Anybody could help me with following code

Why i am getting NullPointerException?

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

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 error and it is in seperate Pacakage 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;
                        }   
}