You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium读取properties文件时出现NullPointerException问题排查

NullPointerException When Loading app.properties via FileInputStream in Selenium BasePage

Got it, let's break down exactly why this error is happening and walk through the fixes step by step.

Why You're Seeing the NullPointerException

There are two key issues here:

  • System.getProperty("app.properties") returns null: The System.getProperty() method doesn't directly look for a file named app.properties—it searches for a JVM system property with that name. If you haven't explicitly set this property when launching your tests, it will return null, and passing null to the FileInputStream constructor triggers a NullPointerException.
  • Invalid app.properties format: Your properties file uses quotes around values (e.g., "https://www.google.com/"), which isn't valid for Java Properties files. The parser will treat those quotes as part of the actual value, causing problems later when you try to use the base URL or browser setting.

Step-by-Step Fixes

1. Fix the Properties File Format First

Update your app.properties to remove the quotes—Java Properties uses simple key-value pairs without them:

baseUrl=https://www.google.com/
browser=chrome

2. Load the Properties File Correctly

You have two reliable options here, depending on your project structure:

Option A: Load from the Classpath (Recommended)

If you've placed app.properties in your project's src/test/resources directory (standard for test configs), use the class loader to load it without worrying about absolute paths. This is the most portable approach:

public class BasePage {
    public static WebDriver driver;
    public static Properties prop;

    @Test
    public void BasePages() throws IOException {
        try {
            prop = new Properties();
            // Use class loader to access the file from resources
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream("app.properties");
            
            if (inputStream == null) {
                System.out.println("Error: app.properties not found in classpath");
                return;
            }
            
            prop.load(inputStream);
            System.out.println("Properties file loaded successfully");
        } catch (Exception e) {
            System.out.println("Exception encountered: " + e.getMessage());
            e.printStackTrace(); // This helps debug the exact issue
        }
    }
}

Pro tip: Use try-with-resources to auto-close the input stream (avoids resource leaks):

try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("app.properties")) {
    prop.load(inputStream);
} catch (IOException e) {
    e.printStackTrace();
}

Option B: Explicitly Set the System Property

If you want to keep using System.getProperty(), you need to pass the full path to your app.properties file as a VM argument when running your tests:

  • Maven Command:
    mvn test -Dapp.properties=/full/path/to/your/app.properties
    
  • IDE Run Configuration:
    Go to your test's run settings, add a VM argument like:
    -Dapp.properties=C:/your-project/src/test/resources/app.properties
    

3. Quick Best Practice Note

Avoid using static fields for WebDriver and Properties in test classes—this can cause test isolation issues (tests sharing state between runs). Consider initializing these per test method instead.

内容的提问来源于stack exchange,提问作者Madhu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:47:43