How to open a Jframe passed as parameter?

Asked

Viewed 234 times

0

My idea is to open this window passed as parameter without it 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 they might notice the commentary, in certain part of the class the eclipse accuses NullPointerException on the line I leave the janela visible, which makes no sense, since I already instated it at the beginning of the class. Due to this error the eclipse does not even open the frame. What am I missing?

  • Take a good look at what you are doing. If you receive a Frame as parameter, why new Frame? Remove it JFrame janela = new JFrame;, since you are getting a window in the builder.

  • I’ve tried to do without that new, but the IDE continues to accuse the same error, on the same line ...

  • How are you starting this class? Jframe is being passed as null.

  • Good ! This is exactly what was missing ! There was no instantiation of the window passed as parameter !

1 answer

1


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

JFrame janela = new JFrame();

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

On the call of this class, remember to pass one JFrame valid as a parameter Teste t = new Teste(algumFrame); otherwise the error will continue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.