我正在使用数据驱动的JSON为登录页面编写以下代码,但显示“由于a1.JsonConfig.configProperties为空,因此无法调用org.json.JSONObject.getJSONObject(String)”
package a1;
import java.nio.file.Paths;
import java.nio.file.Files;
import org.json.JSONObject;
public class JsonConfig {
static JSONObject configProperties;
public static void loadConfig() {
try {
configProperties = new JSONObject(
new String(Files.readAllBytes(Paths.get("src\\main\\java\\Config.json"))));
} catch (Exception e) {
System.out.println("unable to load the json file path");
}
}
public static String getUserName(String userName) {
return configProperties.getJSONObject("users").getJSONObject(userName).getString("username");
}
public static String getPassword(String userName) {
return configProperties.getJSONObject("users").getJSONObject(userName).getString("password");
}
}