0

I am trying to call a method in the main function using selenium. However, I am not able to do so. Getting null pointer error. The subclass drinks should be called in the main class coop_main. Could someone please help me.

Superclass:

import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;

public class Coop_main {
    WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();

        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("credentials_enable_service", false);
        prefs.put("password_manager_enabled", false); 
        options.setExperimentalOption("prefs", prefs);
        options.addArguments("start-maximized");
        options.addArguments("disable-infobars");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.coopathome.ch/de/");
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='nav']/ul/li[1]/a/span")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='subnav--item-m_0001']/li[1]/ul[1]/li[1]/a")).click();
        Thread.sleep(2000);
        JavascriptExecutor jse8= (JavascriptExecutor)driver;
        jse8.executeScript("window.scrollBy(0,400)", "");
        driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[2]/div/div/div[3]/button")).click();
        Thread.sleep(2000);
        driver.findElement(By.id("j_username")).sendKeys("[email protected]");
        driver.findElement(By.id("j_password")).sendKeys("mypassword");
        driver.findElement(By.id("loginOverlaySubmitLoginFormButton")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();
        Drinks obj=new Drinks();
        obj.drinks();

    }
}

Subcalss:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

public class Drinks {
    static WebDriver driver;
    public static void drinks() throws InterruptedException
    {

        driver.findElement(By.xpath(".//*[@id='nav']/ul/li[7]/a/span")).click();
           Thread.sleep(2000);
           driver.findElement(By.xpath(".//*[@id='subnav--item-m_0222']/li[1]/ul[1]/li[1]/a")).click();
           Thread.sleep(2000);
           driver.findElement(By.xpath(".//*[@id='sortOptions']")).click();
           Select dropdown1=new Select(driver.findElement(By.name("sort")));
           dropdown1.selectByVisibleText("Preis (absteigend)");
//       JavascriptExecutor jse6= (JavascriptExecutor)driver;
//       jse6.executeScript("window.scrollBy(0,5000)", "");
           JavascriptExecutor jse7= (JavascriptExecutor)driver;
           jse7.executeScript("window.scrollBy(0,400)", "");
           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[4]/button")).click();
           Thread.sleep(2000);
           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[4]/div/div/button[2]")).click();

    }

}

Error:

Exception in thread "main" java.lang.NullPointerException
    at Test_package.Drinks.drinks(Drinks.java:13)
    at Test_package.Coop_main.main(Coop_main.java:42)

1 Answer 1

2

It looks like the 'driver'-field in the 'Drinks'-class is never initialized. Hence the null pointer when you try to use that object on line 13.

One way to fix that is to pass the driver object from Coop_main to the Drinks class as a constructor argument.

Create a constructor in Drinks

    public Drinks(WebDriver driver) {
      this.driver=driver;
    }

Adjust Coop_main

    Drinks obj=new Drinks(driver);

Additionally:

  • Not sure if you intended to include credentials in your code sample. You might want to remove those
  • I think you should look into the Page Object Model, which would result in much more readable and maintainable tests.
  • check here.
0

Your Answer

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

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