java - 调用方法

时间:2017-01-20 14:03:35

标签: java netbeans methods nullpointerexception

由于甚至我的com sci老师都无法弄清楚的原因,我试图在我的主程序中调用一个方法但是当我运行该程序时,java会抛出以下错误:

线程中的异常" main"显示java.lang.NullPointerException         在GUIcreatorr.AddComponents(GUImaker5.java:390)         在GUImaker5.main(GUImaker5.java:425) Java结果:1 建立成功(总时间:5秒)

这是我的代码:

`class GUIcreatorr implements ActionListener
{
    private JFrame frame;
    private CardLayout cardLayout = new CardLayout();
    private JPanel contentPanel;

    //more constructors here for the various panels i am adding to contentPanel

    public void AddComponents()
    {
        //ADDS ALL FIVE PANELS TO CONTENT PANE
        contentPanel.setLayout(cardLayout);
        contentPanel.add(panelMenu,"panelMenu");
        contentPanel.add(panelMenu,"infoPanel");
        contentPanel.add(panel1,"panel1");
        contentPanel.add(panel2,"panel2");
        contentPanel.add(panel3,"panel3");
        contentPanel.add(panel3,"winPanel");
        contentPanel.add(panel3,"losePanel");

        frame = new JFrame("Maple Gang-Gang: Shooting Cups game");
        frame.setContentPane(contentPanel);
        cardLayout.show(contentPanel, "panelMenu");

        frame.setSize(1000, 800); 
        frame.setLocationRelativeTo(null); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
    }

    @Override
        public void actionPerformed(ActionEvent e)
        {
        throw new UnsupportedOperationException("Not supported yet.");
        }
}

public class GUImaker5
{
    public static void main(String[] args)
    {
        GUIcreatorr letsPlay = new GUIcreatorr();

        letsPlay.AddComponents();

    } 
}`

如您所见,我的方法AddComponents获取我在同一类的先前方法中创建的所有面板,并将它们添加到要显示的内容窗格中。我做错了什么,或者有没有办法简单地让java忽略NullPointerException错误?我是初学程序员,但我听说过使用try / catch异常。我应该使用其中一个来解决我的问题,如果是这样的话?

提前感谢急需的帮助!!!

0 个答案:

没有答案