问题
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.
回答1:
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 callingremoveAll()
on it's pannel. This has no effect at all (as this panel is not shown anywhere). - The changed then code will call
removeAll()
onTestPanel1
'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 thepanel2
(in the "Next" button's action).
- Currently you change them when you create the
来源:https://stackoverflow.com/questions/15986208/nullpointerexception-continues