-1

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);
    }
}
Manbran
  • 1
  • 5

1 Answers1

0

You did something mistake. So, put the arguments in a new PanelOne() and new PanelTwo() object as below.

public class myFrame extends JFrame{
    public PanelOne P1 = new PanelOne(this);
    public PanelTwo P2 = new PanelTwo(this);
public myFrame(){
    super("MyFrame");
    ..........
}
public static void main(String args[]){
    myFrame mf = new myFrame();
    mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Sameera Liaynage
  • 821
  • 2
  • 10
  • 12
  • When you face to exception you can put try/catch block and use System.out.println(); to find the wrong place in your code. – Sameera Liaynage Sep 25 '14 at 04:24
  • [Here](http://stackoverflow.com/questions/26025061/stop-start-bouncing-ball-on-a-frame-java?noredirect=1#comment40765296_26025061) is my question too about this issue – Manbran Sep 25 '14 at 04:43