hi folks, im having a problem here on my code can't figure what's wrong..
so here's my code:

import java.awt.*;
import java.awt.Font.*;
import java.awt.Color.*;
import java.awt.Button.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
import javax.swing.JOptionPane;

public class Calc extends JFrame implements ActionListener
{
    JTextArea txtArea;
    static final String key1 = "789", key2 = "456", key3 = "123", key4 = "-*/";
    int a = key1.length(), x, y, b = key2.length(), c = key3.length(), d = key4.length(), ctr = 0;
    JButton button789[], button456[], button123[], buttonComm[];
    String buffer = "", a1, a2, a3, a5;
    char a4;
    int total = 0;
    JButton numLock, zero, dot, enter, plus, ast, min, div;

    Calc()
    {
        setTitle("Bryan Mark's Calculator (c)");
        setBackground(new Color(100,100,100));
        setLayout(null);

        add(txtArea = new JTextArea());
        txtArea.setBounds(10, 10, 270, 50);
        add(numLock = new JButton("A/C"));
        numLock.setBounds(20,65,60,60);
        numLock.addActionListener(this);
        add(div = new JButton("/"));
        div.setBounds(82, 65, 60, 60);
        div.addActionListener(this);
        add(ast = new JButton("*"));
        ast.setBounds(144, 65, 60, 60);
        ast.addActionListener(this);
        add(min = new JButton("-"));
        min.setBounds(206, 65, 60, 60);
        min.addActionListener(this);
        x = 20; y = 127;
        button789 = new JButton[a];
        for (int i = 0; i < a; i++)
        {
            button789[i] = new JButton( "" + key1.charAt(i) );
            add(button789[i]);
            button789[i].setBounds(x, y, 60, 60);
            button789[i].addActionListener(this);
            x = x + 62;
        }
        x = 20; y = 189;
        button456 = new JButton[b];
        for (int i = 0; i < b; i++)
        {
            button456[i] = new JButton( "" + key2.charAt(i) );
            add(button456[i]);
            button456[i].setBounds(x, y, 60, 60);
            button456[i].addActionListener(this);
            x = x + 62;
        }
        x = 20; y = 251;
        button123 = new JButton[c];
        for (int i = 0; i < c; i++)
        {
            button123[i] = new JButton( "" + key3.charAt(i) );
            add(button123[i]);
            button123[i].setBounds(x, y, 60, 60);
            button123[i].addActionListener(this);
            x = x + 62;
        }
        add(zero = new JButton("0"));
        zero.setBounds(20,313,122,60);
        zero.addActionListener(this);
        add(dot = new JButton("."));
        dot.setBounds(144,313,60,60);
        dot.addActionListener(this);
        add(plus = new JButton("+"));
        plus.setBounds(206,127,60,122);
        plus.addActionListener(this);
        add(enter = new JButton("Enter"));
        enter.setBounds(206,251,60,122);
        enter.addActionListener(this);

        setResizable(false);
        resize(300,450);
        show();
    }
    public void actionPerformed(ActionEvent e)
    {
        int a = key1.length(), x, y, b = key2.length(), c = key3.length(), d = key4.length();
        if (e.getSource() == numLock)
        {
            txtArea.setText("");
            ctr = 0;
        }
        else if (e.getSource() == plus)
        {
            ctr = 1;

            txtArea.setText("");
        }
        else if (e.getSource() == min)
        {
            ctr = 2;
            a1 = txtArea.getText();
            txtArea.setText("");
        }
        else if (e.getSource() == ast)
        {
            ctr = 3;
            a1 = txtArea.getText();
            txtArea.setText("");
        }
        else if (e.getSource() == div)
        {
            ctr = 4;
            a1 = txtArea.getText();
            txtArea.setText("");
        }
        else if (e.getSource() == zero)
        {
            txtArea.append("0");
        }
        else if (e.getSource() == dot)
        {
            txtArea.append(".");
        }
        else if (e.getSource() == enter)
        {
            a2 = txtArea.getText();
            if(ctr==1)
            {
                total = Integer.parseInt(a1)+Integer.parseInt(a2);
                txtArea.setText(String.valueOf(total));
            }
            else if(ctr==2)
            {
                total=Integer.parseInt(a1)-Integer.parseInt(a2);
                txtArea.setText(String.valueOf(total));
            }
            else if(ctr==3)
            {
                total=Integer.parseInt(a1)*Integer.parseInt(a2);
                txtArea.setText(String.valueOf(total));
            }
            else
            {
                total=Integer.parseInt(a1)/Integer.parseInt(a2);
                txtArea.setText(String.valueOf(total));
            }
        }
        else
        {
            for (int i = 0; i < a; i++)
            {
                if (e.getSource() == button789[i])
                {
                    a3 = txtArea.getText();
                    a4 = (char)key1.charAt(i);
                    a5 = a3 + a4;
                    txtArea.setText(a5);
                }
            }
            for (int i = 0; i < b; i++)
            {
                if (e.getSource() == button456[i])
                {
                    a3 = txtArea.getText();
                    a4 = (char)key2.charAt(i);
                    a5 = a3 + a4;
                    txtArea.setText(a5);
                }
            }
            for (int i = 0; i < c; i++)
            {
                if (e.getSource() == button123[i])
                {
                    a3 = txtArea.getText();
                    a4 = (char)key3.charAt(i);
                    a5 = a3 + a4;
                    txtArea.setText(a5);
                }
            }
        }
    }
    public static void main(String[] args)
    {
        Calc c = new Calc();
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

here's my error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Calc.actionPerformed(Calc.java:135)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

i post this cause i don't have an idea what my problem is.. the problem happens when i click enter for getting the result of the numbers that i click with the operation also i click..

Recommended Answers

All 5 Replies

you are getting that NumberFormatException while trying to convert string to integer. because of the parseInt() method . Make sure you are giving the valid number as parameter to the parseInt() method.

parseInt method converts string to integer provided string contains only numbers (strictly numbers , not even a space after the number). Otherwise it throws an NumberFormatException.

PS: trim() method will be quite useful in this case

String s="567     ";
int num=Integer.parseInt(s.trim());

Here eventhough the string 's' contains spaces , trim method will trim the spaces thus avoiding exception

commented: Good :) +13

so you suggest that i use trim() method?.. but where do i put it?.. for getting the result?..

btw, i try to run it again and i don't encounter the error.. :O
i don't know how, but i haven't changed anything..
btw thanks for your comment here sir.. wish u luck.. thanks again..

import java.awt.import javax.swing.import java.awt.event.class Calculator implements ActionListener

ehm ... what is this added post supposed to mean?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.