“线程中的异常”;“主要”;newproject.PG1.main处的java.lang.NullPointerException(PG1.java:30)

“线程中的异常”;“主要”;newproject.PG1.main处的java.lang.NullPointerException(PG1.java:30),java,Java,如何修复线程“main”java.lang.NullPointerException中的此错误异常 在newproject.PG1.main(PG1.java:30)中,我相信它是关于getTitle方法的,但我不知道如何更正它 My code is below package newproject; import org.openqa.selenium.firefox.FirefoxDriver; //comment the above line and uncomment below li

如何修复线程“main”java.lang.NullPointerException中的此错误异常 在newproject.PG1.main(PG1.java:30)中,我相信它是关于getTitle方法的,但我不知道如何更正它

My code is below

package newproject;
import org.openqa.selenium.firefox.FirefoxDriver;
//comment the above line and uncomment below line to use Chrome
//import org.openqa.selenium.chrome.ChromeDriver;
public class PG1 {


    public static void main(String[] args) {
        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
        FirefoxDriver driver = new FirefoxDriver();
        //comment the above 2 lines and uncomment below 2 lines to use Chrome
        //System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
        //WebDriver driver = new ChromeDriver();

        String baseUrl = "http://demo.guru99.com/selenium/newtours/";
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = "";

        // launch Fire fox and direct it to the Base URL
        driver.get(baseUrl);

        // get the actual value of the title
        actualTitle = driver.getTitle();

        /*
         * compare the actual title of the page with the expected one and print
         * the result as "Passed" or "Failed"
         */
        if (actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed");
        }

        //close Fire fox
        driver.close();

    }

}

actualTitle
为空-尝试与if语句部分相反,而不是
actualTitle.contentEquals(expectedTitle)
do
expectedTitle.contentEquals(actualTitle)
,这将不会生成NPE。

哪一行是第30行?如果是由driver.getTitle()生成的,你可能想查看这个链接。你能写完整的stackTrace吗?我很乐意听到其他解释。为什么投票被否决?这几乎肯定是对所问问题的正确答案。