Setting JTextField from another class [duplicate]
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
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);
}
});
java
marked as duplicate by Krease, Hovercraft Full Of Eels
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 1:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
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);
}
});
java
marked as duplicate by Krease, Hovercraft Full Of Eels
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 1:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
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 ofString operation
is never set, so it's null - for the same reason, yourx
andy
are always 0. (3) You never initializethis.calcgui
so it's also null. (4) You have a circular dependency betweenCalcGui
andAddNumbers
- 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 areturn
value or indirectly via theNumbers
class
– MadProgrammer
Nov 22 '18 at 1:38
add a comment |
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
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);
}
});
java
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
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);
}
});
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
java
java
asked Nov 21 '18 at 23:50
marekmarek
123
123
marked as duplicate by Krease, Hovercraft Full Of Eels
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 1:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Krease, Hovercraft Full Of Eels
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 1:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
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 ofString operation
is never set, so it's null - for the same reason, yourx
andy
are always 0. (3) You never initializethis.calcgui
so it's also null. (4) You have a circular dependency betweenCalcGui
andAddNumbers
- 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 areturn
value or indirectly via theNumbers
class
– MadProgrammer
Nov 22 '18 at 1:38
add a comment |
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 ofString operation
is never set, so it's null - for the same reason, yourx
andy
are always 0. (3) You never initializethis.calcgui
so it's also null. (4) You have a circular dependency betweenCalcGui
andAddNumbers
- 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 areturn
value or indirectly via theNumbers
class
– MadProgrammer
Nov 22 '18 at 1:38
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
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
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
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
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 1:38
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 1:38
add a comment |
1 Answer
1
active
oldest
votes
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
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.poli.diniz
Nov 22 '18 at 0: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 0:22
2
if(request.GetCalcWanted()=="add") {
?? Are you really recommending this equality check?
– Hovercraft Full Of Eels
Nov 22 '18 at 1:32
2
if(request.GetCalcWanted()=="add") {
I'm sorry, what?!
– MadProgrammer
Nov 22 '18 at 1: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 1:37
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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.poli.diniz
Nov 22 '18 at 0: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 0:22
2
if(request.GetCalcWanted()=="add") {
?? Are you really recommending this equality check?
– Hovercraft Full Of Eels
Nov 22 '18 at 1:32
2
if(request.GetCalcWanted()=="add") {
I'm sorry, what?!
– MadProgrammer
Nov 22 '18 at 1: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 1:37
|
show 1 more comment
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
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.poli.diniz
Nov 22 '18 at 0: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 0:22
2
if(request.GetCalcWanted()=="add") {
?? Are you really recommending this equality check?
– Hovercraft Full Of Eels
Nov 22 '18 at 1:32
2
if(request.GetCalcWanted()=="add") {
I'm sorry, what?!
– MadProgrammer
Nov 22 '18 at 1: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 1:37
|
show 1 more comment
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
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
edited Dec 21 '18 at 2:49
answered Nov 22 '18 at 0:03
rod.poli.dinizrod.poli.diniz
116118
116118
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.poli.diniz
Nov 22 '18 at 0: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 0:22
2
if(request.GetCalcWanted()=="add") {
?? Are you really recommending this equality check?
– Hovercraft Full Of Eels
Nov 22 '18 at 1:32
2
if(request.GetCalcWanted()=="add") {
I'm sorry, what?!
– MadProgrammer
Nov 22 '18 at 1: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 1:37
|
show 1 more comment
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.poli.diniz
Nov 22 '18 at 0: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 0:22
2
if(request.GetCalcWanted()=="add") {
?? Are you really recommending this equality check?
– Hovercraft Full Of Eels
Nov 22 '18 at 1:32
2
if(request.GetCalcWanted()=="add") {
I'm sorry, what?!
– MadProgrammer
Nov 22 '18 at 1: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 1:37
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.poli.diniz
Nov 22 '18 at 0:16
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.poli.diniz
Nov 22 '18 at 0: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 0:22
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 0:22
2
2
if(request.GetCalcWanted()=="add") {
?? Are you really recommending this equality check?– Hovercraft Full Of Eels
Nov 22 '18 at 1:32
if(request.GetCalcWanted()=="add") {
?? Are you really recommending this equality check?– Hovercraft Full Of Eels
Nov 22 '18 at 1:32
2
2
if(request.GetCalcWanted()=="add") {
I'm sorry, what?!– MadProgrammer
Nov 22 '18 at 1:35
if(request.GetCalcWanted()=="add") {
I'm sorry, what?!– MadProgrammer
Nov 22 '18 at 1:35
1
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 1:37
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 1:37
|
show 1 more comment
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 ofString operation
is never set, so it's null - for the same reason, yourx
andy
are always 0. (3) You never initializethis.calcgui
so it's also null. (4) You have a circular dependency betweenCalcGui
andAddNumbers
- 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 areturn
value or indirectly via theNumbers
class– MadProgrammer
Nov 22 '18 at 1:38