Making a calculator in GUI using stacks. I am getting this error
can someone please help me. I have trying to finish this code all night and still aren't getting anywhere. Please help.
Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at Calculator.<init>(Calculator.java:42) at Calculator.main(Calculator.java:384)
can someone please help me. I have trying to finish this code all night and still aren't getting anywhere. Please help.
import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JFrame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.util.*; public class Calculator extends JFrame implements ActionListener { // 10 11 12 13 14 15 16 17 18 String numbers[]={"0","1","2","3","4","5","6","7","8","9","+","-","*","/",".","CE","C","=","+/-"}; JButton button[]=new JButton[19]; Panel p[]=new Panel[6]; TextField text; Box box; FlowLayout fv; Calculator(String title) { super(title); this.setDefaultCloseOperation(EXIT_ON_CLOSE); box=Box.createVerticalBox(); fv=new FlowLayout(); this.setLocationRelativeTo(null); for(int i=0;i<numbers.length;i++) { button[i]=new JButton(numbers[i]); button[i].addActionListener(this); if(i!=15&&i!=16) { button[i].setPreferredSize(new Dimension(55,35)); } else { button[i].setPreferredSize(new Dimension(85,35)); } if(i<6) { p[2]=new Panel(); box.add(p[i]); } if(i==0) { text=new TextField("0",30); p[0].add(text); } } p[1].add(button[7]);p[1].add(button[8]);p[1].add(button[9]);p[1].add(button[10]); p[2].add(button[4]);p[2].add(button[5]);p[2].add(button[6]);p[2].add(button[11]); p[3].add(button[1]);p[3].add(button[2]);p[3].add(button[3]);p[3].add(button[12]); p[4].add(button[0]);p[4].add(button[14]);p[4].add(button[18]);p[4].add(button[13]); p[5].add(button[15]);p[5].add(button[16]);p[5].add(button[17]); this.add(box); this.setBounds(520,200,280,320); this.setResizable(false); this.setVisible(true); this.validate(); } int press=0; int press1=0; int press2=0; int strlen=0; String[] tokens; String stra; double result; Stack<Double> operands = new Stack(); Stack<String> operators = new Stack(); StringBuffer str=new StringBuffer("0"); StringBuffer str2=new StringBuffer("0"); double sum1=0; public void actionPerformed(ActionEvent e) { if(e.getSource()==button[10]) { this.pressOperator(10); } if(e.getSource()==button[11]) { this.pressOperator(11); } if(e.getSource()==button[12]) { this.pressOperator(12); } if(e.getSource()==button[13]) { this.pressOperator(13); } if(e.getSource()==button[14]) { if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&press!=1) { str.append(button[14].getText()); str2.append(button[14].getText()); text.setText(str.toString()); press=1; } } if(e.getSource()==button[18]) { if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/') { if(press2==0) { str.insert(strlen, "-"); str2.insert(strlen, "@"); text.setText(str.toString()); press2=1; } else { str.deleteCharAt(strlen); str2.deleteCharAt(strlen); text.setText(str.toString()); press2=0; } } } if(e.getSource()==button[15]) { if(strlen!=0) { str.delete(strlen, str.length()); str2.delete(strlen, str2.length()); text.setText(str.toString()); } else { str.delete(0,str.length()); str2.delete(0,str2.length()); str.append("0"); str2.append("0"); text.setText(str.toString()); press=0; press2=0; } } if(e.getSource()==button[16]) { str.delete(0, str.length()); str2.delete(0, str2.length()); str.append("0"); str2.append("0"); text.setText(str.toString()); press=0; press1=0; press2=0; strlen=0; } if(e.getSource()==button[17]) { if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&press!=1) { String txt=str2.toString(); tokens = stra.split("\\+|\\-|\\*|\\/"); fillStacks(operands,operators,tokens); result = calculate(operands,operators); String res = Double.toString(result); text.setText(res.toString()); } } if(e.getSource()==button[0]) { this.displayNumber(0); } if(e.getSource()==button[1]) { this.displayNumber(1); } if(e.getSource()==button[2]) { this.displayNumber(2); } if(e.getSource()==button[3]) { this.displayNumber(3); } if(e.getSource()==button[4]) { this.displayNumber(4); } if(e.getSource()==button[5]) { this.displayNumber(5); } if(e.getSource()==button[6]) { this.displayNumber(6); } if(e.getSource()==button[7]) { this.displayNumber(7); } if(e.getSource()==button[8]) { this.displayNumber(8); } if(e.getSource()==button[9]) { this.displayNumber(9); } } public static boolean isOperator(String str) { boolean tf; if(str.equals("+") || str.equals("-") || str.equals("/") || str.equals("*")) tf = true; else if(str.equals("q") || str.equals("Q")) tf = false; else { tf = false; System.out.println("Invalid input. Please try again."); } return tf; }//end isOperator /**Methods for filling stacks and evaluating expressions.*/ private static void fillStacks(Stack<Double> stkd, Stack<String> stks,String[] str) { boolean isOp; for(int i = 0; i < str.length; i++) { try { stkd.push(Double.parseDouble(str[i])); }//end try catch (NumberFormatException e) { isOp = isOperator(str[i]); if(isOp == true) stks.push(str[i]); }//end catch }//end for }//end fillStacks public void pressOperator(int i) { if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&str.charAt(str.length()-1)!='.') { str.append(button[i].getText()); str2.append(button[i].getText()); text.setText(str.toString()); press=0; press1=1; press2=0; strlen=str.length(); } } public void displayNumber(int i) { if(press1!=1&&press!=1&&str.charAt(0)=='0') { str.replace(0, 1,button[i].getText()); str2.replace(0, 1,button[i].getText()); text.setText(str.toString()); } else { str.append(button[i].getText()); str2.append(button[i].getText()); text.setText(str.toString()); } } /** Methods for Addition, Subtraction, Multiplication and Division */ public double add(Stack<Double> stk) { double op2 = stk.pop(); double op1 = stk.pop(); double result = op1 + op2; stk.push(result); return result; }// end add public double subtract(Stack<Double> stk) { double op2 = stk.pop(); double op1 = stk.pop(); double result = op1 - op2; stk.push(result); return result; }// end subtract public double divide(Stack<Double> stk) { double op2 = stk.pop(); double op1 = stk.pop(); double result = op1 / op2; stk.push(result); return result; }//end divide public double multiply(Stack<Double> stk) { double op2 = stk.pop(); double op1 = stk.pop(); double result = op1 * op2; stk.push(result); return result; }//end multiply public double calculate(Stack<Double> stkd, Stack<String> stks) { double result = 0; String str = ""; while(!stks.empty()) { str = stks.pop(); if(str.equals("+")) result = add(stkd); else if(str.equals("-")) result = subtract(stkd); else if(str.equals("/")) result = divide(stkd); else if(str.equals("*")) result = multiply(stkd); }//end while return result; } public static void main(String args[]) { Calculator cal=new Calculator("1"); } }