0

I got the following exception when i tried to do frame.setvisible(true);

java.lang.reflect.InvocationTargetException at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
com.myapp.GeneralEventProc.cmdProc(GeneralEventProc.java:43)
at tcl.lang.Parser.evalObjv(Parser.java:810) at
tcl.lang.Parser.eval2(Parser.java:1209) at
tcl.lang.Interp.eval(Interp.java:2042) at
tcl.lang.Interp.eval(Interp.java:2071) at
javaapps.JScriptProcessor.processCurrentScript(JScriptProcessor.java:389)
at
javaapps.JScriptProcessor.processQueuedScripts(JScriptProcessor.java:632)
at javaapps.JSPThread.run(JSPThread.java:43) Caused by:
java.lang.NullPointerException at
java.awt.FlowLayout.layoutContainer(Unknown Source) at
java.awt.Container.layout(Unknown Source) at
java.awt.Container.doLayout(Unknown Source) at
java.awt.Container.validateTree(Unknown Source) at
java.awt.Container.validateTree(Unknown Source) at
java.awt.Container.validateTree(Unknown Source) at
java.awt.Container.validateTree(Unknown Source) at
java.awt.Container.validateTree(Unknown Source) at
java.awt.Container.validateTree(Unknown Source) at
java.awt.Container.validateTree(Unknown Source) at
java.awt.Container.validate(Unknown Source) at
java.awt.Window.show(Unknown Source) at
java.awt.Component.show(Unknown Source) at
java.awt.Component.setVisible(Unknown Source) at
java.awt.Window.setVisible(Unknown Source) at
com.myapp.mylist.makeList(Unknown Source) at
com.myapp.mylist.show(MyList.java:144) 

The source code is :

public void makeList() {

    synchronized (initLock) {

        System.out.println(" locking... makeList");

        if (_myFrame != null) {
            initiateList();
            _myFrame .setVisible(true);
        } else {
            System.out.println("myframe is null");
        }

    }

}

private void initiateList() {

    _myFrame.getContentPane().setLayout(null);
    _myFrame.getContentPane().removeAll();
    _myapp.setBounds(0, 0, getWidth(), (int) (getHeight() * 0.90));
    _myFrame.getContentPane().add(_myapp);
    _myFrame.getContentPane().add(_myPanel);
    _myPanel.setBounds(0, (int) (getHeight() * 0.90), getWidth(), (int) (getHeight() * 0.10));
    _myFrame.validate();
    _myFrame.repaint();

}

In the above code two methods makeList(), initiateList().

From makeList() method we are calling initiateList() method after checking the _myFrame is not null within synchronized. Then after returning from the method when i do _myFrame.setVisible(true); the NPE occurs.

Note: This issue happened once and its not reproducible.

Is there any bug in java layer?

Please help me out of this.

Thanks in advance.

shan
  • 21
  • 5

1 Answers1

1

This _mainFrame.getContentPane().setLayout(null); would seem to be the start of your problems.

Calling validate instructs the layout manager that it should update the layout of the components under it's control, so I'm not sure why you're calling it or what you hope to achieve.

Is it a bug? That's debatable. Swing is designed around the use of layout managers, so there is an expectation that a layout manager is in use, especially when you call a method intended to update it...I would say, no, it's not a bug...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • In addition to that sage advice: Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Feb 26 '14 at 06:22