How to hide my JPanel when my JFrame loads?

Reinaldo :

I have a problem in hiding my JPanel (Login Panel) when my JFrame starts to show/load from my Main method. Doing the code below gives me a NullPointerException.

// Main Class
public class Main(){
  public static void main(String[] args){
     try {
         new LoginPage().setVisible(true);
     } catch (Exception e){e.printStackTrace()}
  }
}

// LoginPage Class {
public class LoginPage extends javax.swing.JFrame {
   private javax.swing.JPanel LoginPanel;
   public LoginPage(){
     super("Login Form");
     LoginPanel.setVisible(false); // ERROR - Throws NullPointerException
     setSize(1280, 720);
     setLocationRelativeTo(null);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     initComponents();
   }
  }
}
fwerther :

You should instantiate your "LoginPanel".

You just declared it with the "private javax.swing.JPanel LoginPanel", but since it's not instantiated you're getting the NullPointerException.

Just instantiate it with an loginPanel = new LoginPanel(); and it should work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=296213&siteId=1