2016-03-08 49 views
0

我得到了這樣的錯誤:的ExceptionInInitializerError的Java Netbeans的

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 

我設法挖掘,發現有一位與我在恆類和文件句柄類的靜態引用問題。

恆類:

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; 
} 

文件句柄類:

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; 
    } 
} 

方法中調試這個項目後,我發現,文件句柄類字符串A,B,C,d始終爲空,這是導致異常錯誤(或者,也許我錯了,這就是爲什麼我在這裏)。也許somoenoe知道更多關於這個錯誤,並想幫助我解決問題?

+0

它們爲空,因爲在加載Constants類時尚未調用openFile ... – assylias

回答

2

你是對的 - 當你試圖設置常量LIST_DATA和LIST_DATA2時,a..d的值將始終爲NULL。這是因爲您在靜態上下文中加載這些變量,即當加載時,但只有在實例已調用openFile()方法時才設置變量。

您應該考慮何時知道文件是什麼以及如何打開它,並確保在需要常量之前發生這種情況。

您可能可以通過延遲加載常量來做到這一點,但我懷疑在解決方案工作之前可能需要解決更深層的設計問題。

+0

但在我的主類中,我首先調用:FileHandle r = new FileHandle(); r.openFile(); 及更高版本:Frame f = new Frame(); f.CreatingGUI(); 而在框架中,我需要這個常量。爲什麼這不起作用? – TheDude

相關問題