0

Here is where i got a problem: When i want ot fire button Marchewkaim getting error null pointer exception:

public class MainFrame extends JFrame {
public static  void Draw()
{
    mp.repaint();
}
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
static private MyPanel mp = null;

/**
 * This method initializes mp   
 *  
 * @return MyPanel  
 */
private MyPanel getMp() {
    if (mp == null) {
        mp = new MyPanel();
        mp.setLayout(null);
    }
    return mp;
}

/**
 * @param args
 */
public static void main(String[] args) {

    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            MainFrame thisClass = new MainFrame();
            thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            thisClass.setVisible(true); 
        }
    });



}

/**
 * This is the default constructor
 */
public MainFrame() {
    super();
    initialize();
}

/**
 * This method initializes this
 * 
 * @return void
 */
private JPanel getJContentPane() {
    if (jContentPane == null) {
        jContentPane = new JPanel();
        jContentPane.setLayout(new CardLayout(0, 0));

    }
    return jContentPane;
}
private void initialize() {

    this.setSize(668, 402);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
    JPanel panel = new JPanel();
    panel.setLayout(null);
    this.getContentPane().add(panel, "Main");
    panel.setVisible(true);
    JButton btnMarchewka = new JButton("Marchewka");
    btnMarchewka.setBounds(12, 25, 113, 49);
    panel.add(btnMarchewka);

    JButton btnArbuz = new JButton("Arbuz");
    btnArbuz.setBounds(12, 87, 113, 49);
    panel.add(btnArbuz);

    JButton btnUsun = new JButton("Usun ostatni");
    btnUsun.setBounds(12, 149, 113, 49);
    panel.add(btnUsun);
    btnMarchewka.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) 
        {
            Draw();
        }
    });

}

/**
 * This method initializes jContentPane
 * 
 * @return javax.swing.JPanel
 */


}  //  @jve:decl-index=0:visual-constraint="10,10"

Another class

public class MyPanel extends JPanel {

public GObject c; 
public GObject w;
//  @jve:decl-index=0:
private static final long serialVersionUID = 1L;
public List<GObject> list= new ArrayList<GObject>();
/**
 * This is the default constructor
 */
public MyPanel() {
    super();
    initialize();
}
public void addCarrot()
{
    GObject[] c=new GObject[list.size()+1];
    c[list.size()]=new Carrot();
    list.add(c[list.size()]);
    MainFrame.Draw();
    }
public void addWatermelon()
{
    GObject[] w=new Watermelon[list.size()+1];
    w[list.size()]=new Watermelon();
    list.add(w[list.size()]);

    MainFrame.Draw();
    }
   public void remove()
   {
   list.remove(list.size()-1);

    MainFrame.Draw();
  }
  /**
 * This method initializes this
 * 
 * @return void
 */
private void initialize() {
    this.setSize(400, 400);
    this.setLayout(null);
}
@Override
protected void paintComponent(Graphics arg0) {
    // TODO Auto-generated method stub
    super.paintComponent(arg0);
    if
            (list!=null){
    for(int i=0;i<list.size();i++)
    {
        list.get(i).drawMe(arg0);
    }
    }
}

 }

I tried to resolve this error on my own but i cant find where im pointing at nothing. Doing this for 6 hrs and im sick and tired.Im looking for some tips .

B.M
  • 39
  • 1
  • 5
  • Do you mean `btnMarchewka`? Also you should not name your method `Draw()` but instead `draw()`, due to the Java convention. I think your `Draw()`-method is missing a `validate()` before the `repaint()`. – hamena314 Dec 09 '16 at 17:13

1 Answers1

0

In MainFrame class you have getMp() method to initialise the mp but it never called which causing NPE from Draw() method when u call mp.repaint(). Some unused variables are also there in MyPanel class.

Ashok Prajapati
  • 374
  • 2
  • 7