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);
}
});