This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
5年前关闭。
仅当我单击“下一步”按钮时才会发生NullPointerException。
我正在尝试使用cardlayout使框架过渡到另一个面板,但是我没有这样做。
第二个问题:如何使当前面板过渡到另一个类中的下一个面板(LoginPanel)?我已经创建了一个实例
这是我的LoginPanel代码。不好意思,我很抱歉
您应该创建所需的布局。
也可以看看
Cannot refer to a non-final variable inside an inner class defined in a different method
另请参阅Erwin的评论。
(12个答案)
5年前关闭。
仅当我单击“下一步”按钮时才会发生NullPointerException。
我正在尝试使用cardlayout使框架过渡到另一个面板,但是我没有这样做。
第二个问题:如何使当前面板过渡到另一个类中的下一个面板(LoginPanel)?我已经创建了一个实例
public class InsuranceMain extends JFrame {
private CardLayout cardPanel;
private JPanel buttonPanel;
private JButton next;
private JLabel label;
public InsuranceMain() {
buttonPanel = new JPanel();
JPanel card1 = new JPanel();
JPanel cards = new JPanel(cardPanel);
LoginPanel loginPanelA = new LoginPanel(this);
CardLayout cardLayout = new CardLayout();
cards.setLayout(cardLayout);
next = new JButton("Next"); // Button
label = new JLabel("Test");
// Main frame settings
setTitle("Travel Insurance Application");
setSize(300, 300);
setLayout(new FlowLayout());
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel.add(next);
add(buttonPanel);
card1.add(label);
cards.add(card1, "card1");
cards.add(loginPanelA, "loginPanelA");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == next) {
cardLayout.show(cards, "loginPanelA");
}}
});
}
public static void main(String[] args) {
InsuranceMain test = new InsuranceMain();
}
}
这是我的LoginPanel代码。不好意思,我很抱歉
public class LoginPanel extends JPanel {
private JButton loginB, registerB, clearB;
private JTextField nricTextField, passwordTextField;
private JLabel nric, password, loginTitle;
private JPanel centerPanel, southPanel;
private InsuranceMain main;
public LoginPanel(InsuranceMain main){
this.main = main;
// JLabels
loginTitle = new JLabel("Insurance Login", JLabel.CENTER);
nric = new JLabel("NRIC: ");
password = new JLabel("Password: ");
// JTextField
nricTextField = new JTextField("");
passwordTextField = new JTextField("");
// JButton
loginB = new JButton("Login");
loginB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == loginB) {
//cardLayout.show(cards, "card1");
}}
});
registerB = new JButton("Register");
clearB = new JButton("Clear");
// North Panel
setLayout(new BorderLayout()); // main panel's layout
add(loginTitle, BorderLayout.NORTH);
// Center Panel
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(2,2));
centerPanel.add(nric);
centerPanel.add(nricTextField);
centerPanel.add(password);
centerPanel.add(passwordTextField);
add(centerPanel, BorderLayout.CENTER);
// South Panel
southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
southPanel.add(loginB);
southPanel.add(registerB);
southPanel.add(clearB);
add(southPanel, BorderLayout.SOUTH);
}
}
最佳答案
actionPerformed方法中的NPE只能由cardLayout为null引起。所以代替
CardLayout cardLayout = (CardLayout) cards.getLayout(); // yields null
您应该创建所需的布局。
CardLayout cardLayout = new CardLayout();
也可以看看
Cannot refer to a non-final variable inside an inner class defined in a different method
另请参阅Erwin的评论。
关于java - NullPointerException错误和cardlayout ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24970120/
10-10 16:09