I have 3 classes. After I run it it gives me java.lang.NullPointerException
.
I believe something wrong with interaction between my mf
objects.
I included some parts of my problem code, please take a look
public class PanelOne extends JPanel{
/*other code*/
private myFrame mf;
public Timer timer = new Timer(delay, new TimerHandler());
public PanelOne(){
super();
timer.start();
//setBackground(Color.CYAN);
}
public PanelOne (myFrame mf){
super();
this.mf = mf;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
mf.P2.spot.setBounds(getWidth(), getHeight());
mf.P2.spot.move();
mf.P2.spot.draw(g);//if change is true the ball is not red
}
/*other code*/
}
public class PanelTwo extends JPanel{
/*other code*/
private myFrame mf;
public Spot spot = new Spot(100,100,20);
public PanelTwo(){
super();
/*other code*/
}
public PanelTwo(myFrame mf){
super();
this.mf = mf;
}
public class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent ac){
if(ac.getSource()==quit){
System.exit(0);
}
if(ac.getSource()==stop){
mf.P1.timer.stop();
}
if(ac.getSource()==start){
mf.P1.timer.start();
}
}
}
}
public class myFrame extends JFrame{
public PanelOne P1 = new PanelOne();
public PanelTwo P2 = new PanelTwo();
public myFrame(){
super("MyFrame");
/*other code*/
}
public static void main(String args[]){
myFrame mf = new myFrame();
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}