我正在创建一个方法,该方法应该找到文本文件的路径并将其作为用于配置文件的字符串返回。这是受影响的代码块:
public String getConfigLocation() {
String fileName = "locateconfig.txt";
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
locationFile = file.getAbsolutePath();
return locationFile;
}
导致抛出此异常的行是File file = new File(classLoader.getResource(fileName).getFile());.
这是堆栈跟踪的前几行 - 只是前几行,因为这里受影响的所有方法都与我遇到问题的方法交互。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at epicorexpert.EpicoreJFrame.getConfigLocation(EpicoreJFrame.java:1068)
at epicorexpert.EpicoreJFrame.checkConfig(EpicoreJFrame.java:1076)
at epicorexpert.EpicoreJFrame.<init>(EpicoreJFrame.java:94)
我忽略了哪些可能显而易见的问题?谢谢。
编辑:为什么这被标记为重复问题并建议重定向有关 NullPointerExceptions 是什么的线程?我了解它们是什么以及它们通常如何解决。我在这里问的不是这个;这是专门关于 ClassLoader 和 File 对象以及为什么我从它们那里获取 NPE 的。
繁星coding
相关分类