hi
i need to help me this is my calculator code but it's not work with me and i don't know what is the problem in this code can you help me to solve it and tell me what is the problem .
thank you .

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/


public class calculator2 extends Applet
{
TextField display;
int saveNum = 0;
int memNum = 0;
char op = '=';
char lastOp = '=';
boolean newNum = true;


public void init ()
{
setBackground(Color.pink);


display = new TextField("0", 6);
display = setEditable(false);//
add(display);


Panel p = new Panel ();
p.setLayout(new GridLayout (5, 4));
for (int i = 1; i <= 9; i++)
{
p.add(new Button("" + i));
if (i==3)
p.add(new Button("+"));
if (i==6)
p.add(new Button("-"));
}


p.add(new Button("*"));
p.add(new Button("%"));
p.add(new Button("0"));
p.add(new Button("±"));  //grabbed the sign from the character map
p.add(new Button("/"));
p.add(new Button("M*"));
p.add(new Button("RM"));
p.add(new Button("C"));
p.add(new Button("="));
add(p);
}


public boolean action(Event evt, Object arg)
{
if (arg instanceof String)
{
String s = (String) arg;
if (s.charAt(0) >= '0' && s.charAt(0) <='9')
{
if (newNum)
display.setText(display.getText() + s);
newNum = false;
}


else
{
if (s.charAt(0) == '-' && newNum)
{
display.setText("-");
newNum = false;
}


else {
op = s.charAt(0);
calc (Integer.parseInt(display.getText()));
newNum = true;
}
lastOp = op;
}
return true;
}
return super.action(evt, arg);
}


private TextField setEditable(boolean b)
{
return null;
}


public void calc(int n)
{
switch (op)
{
case '+'://saveNum += n;
case '-'://saveNum -= n;
case '*'://saveNum *= n;
case '/'://saveNum /= n;
case '%'://saveNum %= n;
saveNum = n;
return;
}


if (op == '=')
op = lastOp;


switch (op)
{
case '+':
saveNum += n;
break;
case '-':
saveNum -= n;
break;
case '*':
saveNum *= n;
break;
case '/':
saveNum /= n;
break;
case '%':
saveNum %= n;
break;
case 'M':
memNum = n;
saveNum = n;
break;
case 'R':
saveNum = memNum;
break;
case 'C':
saveNum = 0;
break;
case '±':
saveNum = -n;
break;
}
//  display.setText("" + saveNum);
}}

Dani AI

Generated

Immediate crash: the TextField is being replaced with a null return value from a helper instead of having its editable flag set. As noted, that makes the display variable null and causes the error when the applet tries to add it during init(). The digit entry logic is also reversed: the code only acts when starting a new number, so pressing more digits after the first typically does nothing.

The calculation flow is mixed up. The method that applies an operator currently short‑circuits before doing arithmetic because it checks the wrong cases first. The intended behaviour is: when an operator is pressed parse the current display as the operand; if there is no pending operation (first operand) store it as the accumulator; otherwise apply the pending operator to accumulator and operand, update the display, then remember the new operator. Rework the method to use a single “pending operator” variable (or a simple if lastOp == '=' test) and a single switch that performs +, -, *, /, % on the accumulator.

Other practical fixes and cautions:

  • Remove the bogus helper that returns null and call the TextField’s setter on the actual display instance instead of assigning its return value.
  • Treat multi-character buttons (like "M*" and "RM") by comparing the full button string, not only the first char.
  • Handle the ± label carefully — source encoding can be a problem; consider "+/-" or a Unicode escape.
  • Uncomment and call the display update after computing results so the UI actually shows saveNum.
  • Add try/catch around integer parsing, guard against division by zero, and decide whether integer or floating arithmetic is required.

Quick checklist

  • Stop assigning display from a setter that returns null.
  • Fix digit-append logic so the first digit replaces "0" and later digits append.
  • Rewrite calc to use the previous operator as the one to execute.
  • Use explicit string comparisons for memory buttons.
  • Consider moving away from the deprecated applet/action(Event,...) model to a listener-based UI for safer, more maintainable code.

This addresses the errors raised by and expands on ’s correct diagnosis.

Recommended Answers

All 2 Replies

Well first off, where do you get the error exactly?

Well, for one you have a function:

private TextField setEditable(boolean b) {
    return null;
}

And you use it in your init method as such:

    display = setEditable(false);//
    add(display);

SO this tells me that you set TextField 'display' to NULL, then try to add it to your display TextField. I suspect you want to set your 'display' TextField to editable, so you could do the following:

    display.setEditable(false);

Let me know if this is what you are talking about.

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.