我已经使用IntelliJ Idea Java Swing表单GUI构建器设置了一个简单的UI,并将其设置到我的主类标签CodeChallenge
中。
在调试器中运行时,它抛出并抛出异常错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.desktop/java.awt.Container.addImpl(Container.java:1117)
at java.desktop/java.awt.Container.add(Container.java:1029)
at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:553)
at java.desktop/java.awt.Container.add(Container.java:436)
//these two are my files
at com.github.kingultron99.gui.login.LoginGui.<init>(LoginGui.java:14)
at com.github.kingultron99.CodeChallenge.lambda$main$0(CodeChallenge.java:14)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
这是我的代码:
package com.github.kingultron99;
import com.github.kingultron99.gui.login.LoginGui;
import javax.swing.*;
public final class CodeChallenge {
public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(() -> {
//line 16:
LoginGui loginGui = new LoginGui();
loginGui.setVisible(true);
});
}
}
package com.github.kingultron99.gui.login;
import javax.swing.*;
public final class LoginGui extends JFrame {
private JPanel rootPanel;
private JTextField usernameField1;
private JPasswordField passwordField1;
private JButton signinButton;
public LoginGui() {
//line 16:
add(rootPanel);
setTitle("CodeChallenge | Login");
setSize(900,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
仅当我将add(rootPanel)
放入GUI类LoginGui