0

I'm having problems settings my JTextField from another class. When i press button "=" it is supposed to do calculations inside AddNumbers class and set JTextField as result of calculations. But for now it is throwing NullPointerException. I can't figure out how to set up JTextField. I have to set it inside AddNumbers due to Chain of Responsibilities Design.

public class AddNumbers implements Chain {
private Chain nextInChain;
public CalcGui calcgui;
int result=0;

@Override
public void setNextChain(Chain nextChain) {
    // TODO Auto-generated method stub
    this.nextInChain=nextChain;
}

@Override
public void calculate(Numbers request) {
    // TODO Auto-generated method stub
    if(request.GetCalcWanted()=="add") {
        result = request.GetNumber1()+ request.GetNumber2();
        this.calcgui.txtWynik.setText(String.valueOf(result)); //NULLPOINTER
    }else {
        nextInChain.calculate(request);
    }
}

And here is CalcGui class

public class CalcGui {

private JFrame frame;
JTextField txtWynik; 
static Numbers request;
int x,y;
String operation;
boolean clicked = false;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CalcGui window = new CalcGui();
                window.frame.setVisible(true);


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public CalcGui() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 270, 361);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    txtWynik =new JTextField();
    Chain chainCalc1=new AddNumbers();    
    JButton btneq = new JButton("=");
    btneq.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            request = new Numbers(x,y,operation);
            chainCalc1.calculate(request);

        }
    });
marek
  • 29
  • 5
  • I know it has problem with JTextField or CalcGui, probably need to pass reference somehow? But i have no idea how. – marek Nov 21 '18 at 23:55
  • 1
    You've got a few problems: (1) you're doing string comparison with `==` instead of `.equals`. (2) The value of `String operation` is never set, so it's null - for the same reason, your `x` and `y` are always 0. (3) You never initialize `this.calcgui` so it's also null. (4) You have a circular dependency between `CalcGui` and `AddNumbers` - while not necessarily a problem, is likely a key source of your confusion and issues – Krease Nov 21 '18 at 23:56
  • I'm setting operation,x,y in my program but i didn't post that part of code. I just posted the necessary fragments because there is a lot of this. – marek Nov 21 '18 at 23:59
  • `calculate` needs to return the result of the operation, either directly as a `return` value or indirectly via the `Numbers` class – MadProgrammer Nov 22 '18 at 01:38

1 Answers1

-3

I will give you one idea, this method:

public void calculate(Numbers request) {

Could receive the reference to your JTextField, like this:

public void calculate(Numbers request,JTextField out) {
    if(request.GetCalcWanted().equals("add")) {
        result = request.GetNumber1()+ request.GetNumber2();
        out.setText(String.valueOf(result)); //NO NULLPOINTER SINCE YOU PASS PARAMETER
    }else {
        nextInChain.calculate(request);
    }
}

I think that solve your NPE issue.

Cheers,

-Rod

rod.dinis
  • 1,233
  • 2
  • 11
  • 20
  • Ok, then please click the button up, then I can make some points answering your question, I did it for your question. Thanks and have a good one. – rod.dinis Nov 22 '18 at 00:16
  • I would love to but votes from users having less than 15 reputation do not change the publicly displayed post score. – marek Nov 22 '18 at 00:22
  • 2
    `if(request.GetCalcWanted()=="add") {`?? Are you really recommending this equality check? – Hovercraft Full Of Eels Nov 22 '18 at 01:32
  • 2
    `if(request.GetCalcWanted()=="add") {` I'm sorry, what?! – MadProgrammer Nov 22 '18 at 01:35
  • 1
    And no, I wouldn't pass the text field to the method. It's not the methods responsibility to update the component, it's beyond its scope of responsibility. A "better" solution would be to return the result of the calculation – MadProgrammer Nov 22 '18 at 01:37
  • @marek - yes you can - the question asker can vote on answers to their question. That said, please read the comments on this answer before doing so... – Krease Nov 22 '18 at 19:45