File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Calculator Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Calculator Problem" Watch "Calculator Problem" New topic

Calculator Problem

Samarth Barthwal

Joined: Feb 16, 2006
Posts: 23
HI,I made this small calculator which asks yser to enter two number and then performs the operation selected by user on clicking the button on the form.but it is giving error.The error comes when I want to write on the text field.Another error is the NullpointerException in main thread.How can i solve this problem.the code is as following:
Thanks in advance

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632

setText() requires a String argument.
what is valueOf(..)?
is it your own method?
perhaps it should be

next problem
JPanel p;

somewhere, in between the declaration and its use, you will need
p = new JPanel(); //and set the layout if not FlowLayout

next problem
class variables
JButton b1,b2,b3,b4,b5;
local variables (to the constructor)
JButton b1 = new JButton("+");
JButton b2 = new JButton("-");
JButton b3 = new JButton("*");
JButton b4 = new JButton("/");
JButton b5 = new JButton("quit");

these will cause you problems in actionPerformed
change them to
b1 = new JButton("+");
b2 = new JButton("-");
b3 = new JButton("*");
b4 = new JButton("/");
b5 = new JButton("quit");

[EDIT] - same for the JTextFields, and the JLabels
[ April 15, 2006: Message edited by: Michael Dunn ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
I have come to the conclusion that there are three Exceptions which should not be tolerated:-
  • NullPointerException
  • XYZIndexOutOfBoundsException, and
  • ArithmeticException
  • They all mean you need to go back to your code and find out what you ahve done wrong.
    In your case, you don't tell us what sort of error messages you are getting.
    What are you getting from your compiler? Have you put in a method without its class?
    In the case of your NullPointerException, go to the line in question, and immediately before it put in a line like this:
    System.out.printf("this: %s, p %s%n", this, p);//test
    What that will do is print out the "toString()" method of each of the objects which might or might not be null. You will get a printout something like this:-

    this: calcu 8a2dfc7, f: null

    One of those is the name of the class with its hashcode, the other is null.
    Whichever of the two comes out as null, go back and find it, then:-
  • Check you haven't declared it twice by mistake; if you declare

  • JTextField t1, t2, t3; . . .
    JTextField t1 = new JTextField();

    you have got a local variable with the same name.
  • Check you have actually instantiated it. You can write, add(p = new JPanel());
  • Samarth Barthwal

    Joined: Feb 16, 2006
    Posts: 23
    I Thank you very much for your suggestions.They helped me a lot.
    I agree. Here's the link:
    subject: Calculator Problem
    It's not a secret anymore!