Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

java - NullPointerException continues

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;
}
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you try changing this line:

new TestPanel2().panel2.removeAll();

To:

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

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

A better solution is:

Change TestPanel2 to:

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;
    }
}

And then modify TestPanel1 to:

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;
    }
}

Finally, on PanelEventTest.originalFrame(), change:

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

to:

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.

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

  • 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).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...