0

I got an error like this:

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

I managed to dig in and found that there is problem with my static references in Constant class and FileHandle class.

Constant class:

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 class:

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

After debugging this project among methods i found out that FileHandle class Strings a,b,c,d is always null and this is causing exception error ( Or maybe i am wrong, that is why i am here). Maybe somoenoe know more about this error and would like to help me solving problem ?

1
  • They are null because you have not called openFile yet when the Constants class is loaded... Commented Mar 8, 2016 at 12:35

1 Answer 1

2

You are correct - the values of a..d will always be NULL at the moment you are trying to set the constants LIST_DATA and LIST_DATA2. That's because you are loading those variables in a static context, i.e. when the class is being loaded, but you are only setting the variables when the instance has the openFile() method called.

You should think about when you know what the file is and how to open it, and make sure this occurs before you need your constants.

It might be possible for you to do this via lazy loading of your constants, but I suspect there's a deeper design issue that you may need to solve before your solution can work.

Sign up to request clarification or add additional context in comments.

1 Comment

But in my main class i am firstly calling : FileHandle r = new FileHandle(); r.openFile(); and later : Frame f = new Frame(); f.CreatingGUI(); And in Frame i need this constants. Why that not working ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.