I\'m working on a program that launches an applet that will demonstrate how the Extended-Euclid algorithm performs. I have two JTextFields that values will be entered in and
Solution: don't use a DocumentListener as that's not only overkill, it's wrong. If you want the value on button push, then get the value(s) in the button's action, not from a DocumentListener. If your getting values from within the button's action listener doesn't work, then let's figure out what you're doing wrong and help you solve that error.
In fact, I see that you tried to do this once, but commented it out:
System.out.println(event.getActionCommand());
System.out.println(event.getID());
String quotient = "";
//nText = nField.getText(); // **** here ****
//mText = mField.getText(); // **** and here ****
so uncomment these lines and get rid of your DocumentListener.
A problem I see below is that you are trying check if Strings are equivalent using the == operator:
if("Find GCD" == event.getActionCommand()){
int nInt = Integer.parseInt(nText);
int mInt = Integer.parseInt(mText);
int q = mInt/nInt;
quotient = (Integer.toString(q));
}
Don't do this as this can work sometimes and fail at other times. You don't really care if the two Strings are the same object (which is what the == operator tests), but rather you want to know if they contain the same string data. For this you should use the equals or equalsIgnoreCase method:
if ("Find GCD".equals(event.getActionCommand())) {
int nInt = Integer.parseInt(nText);
int mInt = Integer.parseInt(mText);
int q = mInt/nInt;
quotient = (Integer.toString(q));
}
I'm getting a NullPointerException at the line where I set nText = nField.getText();
That probably because you defined nField as both a class variable and a local variable. The problem is that you are trying to reference the class variable which is null.
The solution is to get rid of the class variable.