How to open a JFrame passed as a parameter?

0

My idea is to open this window as a parameter without losing any attribute, for example, I have the class:

public class Teste {

    JFrame janela = new JFrame;    

    public ControllerJPreTransacao(JFrame janela) {

            this.janela = janela;
            initEvents();
    }

    private void initEvents(){

            janela.setVisible(true); //NullPointerException
    }

}
As you might note, in a certain part of the class eclipse accuses NullPointerException on the line that leaves janela visible, which does not make sense since I instantiated it at the beginning of the class. Due to this error the eclipse does not even open the frame. What am I missing?

    
asked by anonymous 13.06.2016 / 18:59

1 answer

1

The error occurs because no parameter is probably being passed to the constructor, in addition to the instantiation is incorrect, the correct way is:

JFrame janela = new JFrame();

As you want to display the window received in the constructor, remove the instantiation of the attribute, just leave JFrame janela; , and treat the method if it is null or not.

When calling this class, remember to pass a JFrame valid as a parameter so Teste t = new Teste(algumFrame); otherwise the error will continue.

    
13.06.2016 / 19:28