Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么添加这3行可以防止NullPointerException?_Java_Selenium Webdriver_Nullpointerexception_Selenium Chromedriver - Fatal编程技术网

Java 为什么添加这3行可以防止NullPointerException?

Java 为什么添加这3行可以防止NullPointerException?,java,selenium-webdriver,nullpointerexception,selenium-chromedriver,Java,Selenium Webdriver,Nullpointerexception,Selenium Chromedriver,如果我使用Test\u将\u给出\u Null\u Pointer\u Error方法,我将得到NullPointerException Stack trace FAILED: openURL java.lang.NullPointerException at SeleniumPracticePackage.CallUrl.**openURL**(CallUrl.java:63) 这是linedriver.get(prop.getProperty(“URL”) 调试显示prop为null 如果

如果我使用
Test\u将\u给出\u Null\u Pointer\u Error
方法,我将得到
NullPointerException

Stack trace
FAILED: openURL
java.lang.NullPointerException
at SeleniumPracticePackage.CallUrl.**openURL**(CallUrl.java:63)
这是line
driver.get(prop.getProperty(“URL”)
调试显示
prop
null

如果我在
openURL()
中添加以下行,代码就可以正常工作

Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties");
prop.load(fis);  
错误代码

 public class Test_Will_Give_Null_Pointer_Error {       
    WebDriver driver;
    Properties prop ;
    FileInputStream fis;        
    @BeforeTest
    public void openBrowser() throws IOException
    {               
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties");
        prop.load(fis);
        String browserType = prop.getProperty("Browser"); 

        //ignored Chromedriver code  below                                            
    }
    @Test
    public void openURL() throws IOException
    {       
       driver.get(prop.getProperty("URL"));
            //ignored rest of code 
    }
}
下面的代码工作正常

public class TestRunsFine {     
    WebDriver driver;
    Properties prop ;
    FileInputStream fis;        
    @BeforeTest
    public void openBrowser() throws IOException
    {               
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties");          prop.load(fis);System.setProperty("webdriver.chrome.driver","C:\\xxxxx\\chromedriver.exe");         
      WebDriver driver = new ChromeDriver();            

    }
    @Test
    public void openURL() throws IOException
    {
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("C:\\Users\\xxxx\\URL.properties");
      prop.load(fis);
      driver.get(prop.getProperty("URL"));           
    }
} 
问题:

  • 我在类级别声明了对象driver、prop、fls,因此它们不是本地的,一旦我在
    openBrowser
    方法中实例化它们,它们的值就应该转移到
    openURL()
    方法中。使用此逻辑,我处理驱动程序对象,没有看到任何
    NullPointerException
  • 如果上述概念是错误的,那么为什么驱动程序对象不抛出
    NullPointerException

  • 覆盖“道具全局”字段:

    public void openBrowser() throws IOException
    {               
        Properties prop = new Properties(); // HERE, this is a local field
    }
    
    要将新属性指定给全局属性字段,需要执行以下操作:

    public void openBrowser() throws IOException
    {               
        prop = new Properties(); // Assign to global field                             
    }
    
    请注意,这仅在您首先调用
    openBrowser()
    时有效,因为prop字段将不会初始化

    通常不应创建与全局字段同名的本地字段,因为这样很容易产生此类错误

    为确保只初始化字段一次(并且在需要使用字段时初始化字段),请将字段设置为最终字段并在构造函数中分配它们:

    private final Properties prob; // Global field
    private final WebDriver driver; // Global field
    
    public Constructor_for_your_class()
    {
        prop = new Properties(); // Sets the global field
        FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\src\\URL.properties");
        prop.load(fis);
        System.setProperty("webdriver.chrome.driver",‌​"C:\\xxxxx\\chromedr‌​iver.exe");
        driver = new ChromeDriver(); // Sets the global field
    }
    
    public void openURL()
    {
        driver // Accesses the global field
            .get(prop // Accesses the global field
                .getProperty("URL"));
        // ...
    }
    

    在每种方法中,你都将变量重新定义为局部变量。我不能将它们定义为全局变量并在任何地方使用吗?我想写一次下面的代码并在整个程序中使用,我应该怎么做。我明白你说的,但我仍然得到NullPointerexception属性prop=new Properties();FileInputStream fis=新的FileInputStream(“C:\\Users\\XXXX\\src\\URL.properties”);道具荷载(fis);System.setProperty(“webdriver.chrome.driver”,“C:\\xxxxx\\chromedriver.exe”);WebDriver驱动程序=新的ChromeDriver()@Tokci更新了我的答案。如果这不是你想要的,用你的新问题问一个新问题。