• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Calculator Problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
t3.setText(valueOf(z));

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

next problem
JPanel p;
...
getContentPane().add(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 ]
 
Marshal
Posts: 80627
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I Thank you very much for your suggestions.They helped me a lot.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic