Trying to build my first calculator from scratch and everything was going smooth but I am now getting hit with a handler coming up as a null pointer exception.
import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JButton; import javax.swing.ImageIcon; import javax.swing.Icon; import javax.swing.JTextArea; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; public class tuna extends JFrame { private JTextArea Screen; private JButton Num0; private JButton Num1; private JButton Num2; private JButton Num3; private JButton Num4; private JButton Num5; private JButton Num6; private JButton Num7; private JButton Num8; private JButton Num9; private ImageIcon INum0; private ImageIcon INum1; private ImageIcon INum2; private ImageIcon INum3; private ImageIcon INum4; private ImageIcon INum5; private ImageIcon INum6; private ImageIcon INum7; private ImageIcon INum8; private ImageIcon INum9; private int count =0; double Fnum; double Snum; boolean[] op = {false,false,false,false}; // not in use yet double Ans; // this is initializing all of the variables public tuna(){ super("RaycasterJr's Calculator"); setLayout(new FlowLayout()); Fnum = 0; Snum = 0; Screen = new JTextArea(10,10); Screen.setEditable(false); add(Screen); JButton Nums[] = {Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9}; Icon[] Numicons = new Icon[10]; for(int count=0; count<=9;count++){ Numicons[count] = new ImageIcon(getClass().getResource(count +"Num.png")); /// Setup the names of the images to co-ordinate with the count+"Num" system. This will allow for limited code. } for (int count = 0; count>=9; count++){ Nums[count] = new JButton(Numicons[count]); add(Nums[count]); /// This will create all of the buttons with their own images } [COLOR="#FF0000"][COLOR="#FFFF00"]handlerNum H = new handlerNum(); for (int count = 0; count<=9;count++){ Nums[count].addActionListener(H); //This is where the error occurs //this is the error code below: //Exception in thread "main" java.lang.NullPointerException //at tuna.<init>(tuna.java:66) //at apples.main(apples.java:5)[/COLOR] [/COLOR] } } public class handlerNum implements ActionListener{ public void actionPerformed(ActionEvent Event){ if (Event.getSource() == Num0){ JOptionPane.showMessageDialog(null,"Testing"+Event.getSource()); /// if a operator is click set that operators boolean to true and set the rest to false - this solves the multi-op problem } } } }
Any help would be appreciated as I'm loving java and trying the gui from scratch but... hit a wall.
Again, thank you for any help.