There is the Search Class where I made a method to do amazon search, and the Main Class calls the Method searchFor() But I keep getting the error
Exception in thread "main" java.lang.NullPointerException
package Project1;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class Search {
@FindBy(id = "twotabsearchtextbox")
WebElement search_box;
public void searchFor(String content) {
search_box.sendKeys(content);
search_box.submit();
}
}
And this is the Main Class
package Project1;
public class Main {
public static void main(String[] args) {
Search s1 = new Search();
s1.searchFor("gaming laptop");
}
}
Please refer below solution:
Main class
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("Your url ");
Search s1 = new Search(driver);
s1.searchFor("gaming laptop");
}
}
Search class
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Search {
@FindBy(id = "twotabsearchtextbox")
WebElement search_box;
WebDriver driver;
public Search(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void searchFor(String content) {
search_box.sendKeys(content);
search_box.submit();
}
}