Reputation: 45
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 need to be read/converted to ints, etc. I'm having trouble actually reading the input values to start with.
Updated question:
I've made changes to my code after reading the first answer. Instead of using a DocumentListener I'm doing everything in my actionPerformed method as suggested but when I try testing I'm still getting errors.
This is my current actionPerformed:
public void actionPerformed(ActionEvent event) {
System.out.println(event.getActionCommand());
String quotient = "";
nText = nField.getText();
mText = mField.getText();
if("Find GCD".equals(event.getActionCommand())){
int nInt = Integer.parseInt(nText);
int mInt = Integer.parseInt(mText);
int q = mInt/nInt;
quotient = (Integer.toString(q));
}
else quotient = "n/a";
//NOT the gcd, just to see if this will display
gcd.setText(quotient);
gcd.setEditable(false);
}
(note: gcd is another JTextField, but just needs to display the outcome)
Now after clicking my button, I'm getting these errors printed out in the console:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at EuclidApplet.actionPerformed(EuclidApplet.java:87) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
I'm getting a NullPointerException at the line where I set
nText = nField.getText();
but I'm still not sure why I'm not able to get the text that's been inserted into my JTextField. Every example I've found shows that this should work but I can't get it to. Thanks.
Upvotes: 1
Views: 4364
Reputation: 324088
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.
Upvotes: 1
Reputation: 285405
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));
}
Upvotes: 1