NullPointerException继续 [英] NullPointerException continues

查看:240
本文介绍了NullPointerException继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图从Panel1类的Next JButton触发动作事件时调用Panel2类的JPanel panel2时,我得到了NullPointerException.如何解决呢?请帮助.

When I am trying to call JPanel panel2 of Panel2 class on triggering of action event from Next JButton of Panel1 class, I am getting NullPointerException. How to resolve this? plzz help.

public class PanelEventTest
{

/**
 * @param args
 */
JFrame frame;

void originalFrame()
{
    frame = new JFrame();
    frame.setSize(500, 300);
    frame.setVisible(true);
    frame.setLayout(new FlowLayout());
    frame.add(new TestPanel1().panel1());
    frame.add(new TestPanel2().panel2());
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new PanelEventTest().originalFrame();
}
}

public class TestPanel1
{
JPanel panel1;
JButton next;

JPanel panel1()
{
    panel1 = new JPanel();
    next = new JButton("Next");
    next.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e) 
        {
            // TODO Auto-generated method stub
            new TestPanel2().panel2.removeAll();
        }
    });
    panel1.add(next);
    return panel1;
}
}
public class TestPanel2
{
JPanel panel2;
JList jlist;
String[] list = {"Sachin","Tarun","Vipin"};
JPanel panel2()
{
    panel2 = new JPanel();
    jlist = new JList(list);
    panel2.add(jlist);
    panel2.add(new JLabel("Test"));
    return panel2;
}
}

我的最后一个问题使用JPanel的Nullpointerexception 已成功解决.请帮助.这个例外正在吞噬我的头脑.

My last question Nullpointerexception with JPanel was successfully resolved by you guys. Plz help in this. This exception is eating my head.

推荐答案

如果您尝试更改此行:

new TestPanel2().panel2.removeAll();

收件人:

new TestPanel2().panel2().removeAll();

将解决问题,但当前的逻辑存在缺陷.

Will solve the problem, but the current logic is flawed.

TestPanel2更改为:

public class TestPanel2 {
    JPanel panel2;
    JList jlist;
    String[] list = { "Sachin", "Tarun", "Vipin" };

    public TestPanel2() { // was: JPanel panel2() {
        panel2 = new JPanel();
        jlist = new JList(list);
        panel2.add(jlist);
        panel2.add(new JLabel("Test"));
                      // was: return panel2;
    }
}

然后将TestPanel1修改为:

public class TestPanel1 {
    JPanel panel1;
    JButton next;

    public TestPanel1(final JFrame frame, TestPanel2 tp2) { // was: JPanel panel1() {
        panel1 = new JPanel();
        next = new JButton("Next");
        final JPanel panel2 = tp2.panel2; // line created
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                panel2.removeAll(); // was: new TestPanel2().panel2.removeAll();
                frame.validate(); // line created
                frame.paint(); // line created
            }
        });
        panel1.add(next);
                         // was: return panel1;
    }
}

最后,在PanelEventTest.originalFrame()上更改:

frame.add(new TestPanel1().panel1());
frame.add(new TestPanel2().panel2());

收件人:

TestPanel2 tp2 = new TestPanel2();
frame.add(new TestPanel1(frame, tp2).panel1);
frame.add(tp2.panel2);
frame.validate();
frame.repaint();

说明

您在需要构造函数时正在创建方法.您必须阅读以下内容:了解构造函数.

Explanation

You are creating methods, when you needed constructors. You must read this: Understanding constructors.

此外,您需要将TestPanel2frame传递给TestPanel1:

Also, you need to pass TestPanel2 and the frame to TestPanel1:

  • 您的代码正在创建一个新的TestPanel2(没有附加),然后在其面板上调用removeAll().这根本没有任何效果(因为此面板未在任何地方显示).
  • 更改后的代码将在TestPanel1的面板上调用removeAll().
  • 此外,每次对组件进行更改时,都需要重新验证/重新绘制组件.
    • 当前,您在创建frame(添加面板)和删除panel2(在下一步"按钮的操作中)时进行更改.
    • Your code was creating a new TestPanel2 (attached to no one) and then calling removeAll() on it's pannel. This has no effect at all (as this panel is not shown anywhere).
    • The changed then code will call removeAll() on TestPanel1's panel.
    • Also, you need to revalidate/repaint the components everytime you make a change on them.
      • Currently you change them when you create the frame (adding the panels) and when you remove the panel2 (in the "Next" button's action).

      这篇关于NullPointerException继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆