我收到了这样的错误:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at databasesearch.Frame.CreatingGUI(Frame.java:65)
at databasesearch.DatabaseSearch.main(DatabaseSearch.java:23)
at javaqcfp.JFDatabase$1.actionPerformed(JFDatabase.java:59)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.NullPointerException
at databasesearch.Constants.<clinit>(Constants.java:23)
... 39 more
我设法深入挖掘并发现Constant类和FileHandle类中的静态引用存在问题。
常数类:
public class Constants {
FileHandle h = new FileHandle();
public static String[] LIST_DATA = FileHandle.getA().split(Pattern.quote("."));
public static String[] LIST_DATA2 = FileHandle.getB().split(Pattern.quote("."));
public static int NEW_ELEMENT_ID =0;
}
FileHandle类:
public class FileHandle {
private static String a;
private static String b;
private static String c;
private static String d;
public void openFile() throws FileNotFoundException {
File dir = new File("DB");
if (!dir.exists()){
dir.mkdirs();
}
System.out.println(dir.getAbsolutePath());
if (dir.isDirectory()) {
for (File file : dir.listFiles()) {
try (Scanner s = new Scanner(file)) {
while (s.hasNext()) {
a = (a == null) ? s.next() :a + "."+s.next();
b = (b == null) ? s.next() :b + "." + s.next();
c = (c == null) ? s.next() : c + "." + s.next();
d = (d == null) ? s.next() : d + "." + s.next();
System.out.printf("%s\n %s\n %s\n %s\n", a,b,c,d);
}
}
}
}
}
public static String getA(){
System.out.println(a);
return a;
}
public static String getB() {
System.out.println(b);
return b;
}
}
在方法之间调试这个项目后我发现FileHandle类字符串a,b,c,d总是为null,这导致异常错误(或者我错了,这就是为什么我在这里)。也许somoenoe更多地了解这个错误,并希望帮助我解决问题?
答案 0 :(得分:2)
你是对的 - 在你试图设置常量LIST_DATA和LIST_DATA2时,a..d的值总是为NULL。那是因为你在静态上下文中加载这些变量,即当加载类时,你只是在实例具有openFile()方法时设置变量调用。
当你知道文件是什么以及如何打开文件时,你应该考虑一下,并确保在你需要常数之前这样做。
您可以通过延迟加载常量来实现此目的,但我怀疑在解决方案可行之前您可能需要解决更深层次的设计问题。