1

I am learning to use java.swing library. I am trying to create a layout of very simple calculator. I have added addNumbers method. I am trying to show buttons in a calculator and I have used for loops.buttons are not appearing I am getting nullpointerexception.

import java.awt.*;
import javax.swing.*;

public class Calculator extends JFrame{

    /**
     * @param args
     */
    //dEFINE WIDTH AND HEIGHT
    private static final int WIDTH = 400;
    private static final int HEIGHT = 600;

    //Values for buttons having numbers
    private JButton[] numButton;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Calculator myCalculator = new Calculator();

    }

    public Calculator(){
        setTitle("Simple Calculator");
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container Pane = getContentPane();
        Pane.setLayout(new GridLayout(3,3));

        //Add numbers to screen now
        addNumbers(Pane);



    }

    //Function to add numbers on screen
    public void addNumbers(Container P){
        for(int i = 1; i <= 9; i++){
            numButton[i] = new JButton(String.valueOf(i));
            P.add(numButton[i]);
        }
    }

}
Maroun
  • 94,125
  • 30
  • 188
  • 241

3 Answers3

6

You need to initialize your array:

private JButton[] numButton = new JButton[10];

The 10 here allows for 10 spaces in your array.

Thomas Nairn
  • 1,186
  • 8
  • 34
  • I changed private JButton[] numButton to private JButton[] numButton = new JButton[10], still I am getting random buttons, sometimes 3, sometimes 4 – Ajay Birbal May 05 '15 at 13:39
  • What do you mean by random buttons? You should update your question. – Thomas Nairn May 05 '15 at 13:40
  • After making the changes you suggessted, I started getting sometimes 5 buttons, sometimes 4 buttons . i want 9 buttons all the time. This problem is specific to your answer only – Ajay Birbal May 05 '15 at 13:44
  • Sorry for my mistake, 9 buttons are indeed appearing but some buttons are delaying or sometimes i have to click on window to maximize and then return them to normal size, then button appears – Ajay Birbal May 05 '15 at 13:49
  • After your addNumbers method. call Pack(); and then call setVisible(); for simplicity's sake You should always add your buttons first. – Thomas Nairn May 05 '15 at 13:50
0

The above solution works and removes the NullPointer since memory is now allocated to the numButton array.... Here is a working code snip that also incorporates the (BODMAS) signs... only difference is that I have got all key labels into an ArrayList and the for loop now works on this ArrayList to get its labels for the calculator... ArrayList may not be a good collection..maybe enum might be better but this is to show how it could be made more dynamic and adding button labels or new buttons would be easier... Also note that you can make GridSize dynamic based on the size of the key label array..gridSize = this.buttons.size() / 4....

acearch
  • 343
  • 1
  • 8
0
import java.awt.Container;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Calculator extends JFrame{

    private static final int WIDTH = 400;
    private static final int HEIGHT = 600;


    List<String> buttons = new ArrayList<String>();




    JButton[] numButton = null;


    public static void main(String[] args) {
        Calculator myCalculator = new Calculator();
    }

    public Calculator(){
        setTitle("Simple Calculator");
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container Pane = getContentPane();


        this.buttons.add("1");
        this.buttons.add("2");
        this.buttons.add("3");
        this.buttons.add("4");
        this.buttons.add("5");
        this.buttons.add("6");
        this.buttons.add("7");
        this.buttons.add("8");
        this.buttons.add("9");
        this.buttons.add("0");
        this.buttons.add("+");
        this.buttons.add("=");
        this.buttons.add("%");
        this.buttons.add("#");
        this.buttons.add("*");
        this.buttons.add("/");

        int gridSize = this.buttons.size() / 4;

        Pane.setLayout(new GridLayout(gridSize,gridSize));


        this.numButton = new JButton[this.buttons.size()];

        //Add numbers to screen now
        addNumbers(Pane);
       }

    //Function to add numbers on screen
    public void addNumbers(Container P){
        for(int i = 0; i < this.buttons.size(); i++){
            numButton[i] = new JButton(this.buttons.get(i).toString());
            P.add(numButton[i]);
        }
    }

}
acearch
  • 343
  • 1
  • 8