如何从JTextField读取int值?

时间:2017-05-13 20:49:04

标签: java windowbuilder

我的表单上有2个JTextField,我全局声明了这两个变量

   int order = Integer.parseInt(textOrder.getText());
   int history = Integer.parseInt(textHistory.getText());

但是我遇到了这些错误。

    java.lang.NullPointerException
at FrameController.<init>(FrameController.java:39)//this is line 39: int history=Integer.parseInt(textHistory.getText());

at FrameController$1.run(FrameController.java:56)//this is line 56:     FrameController window = new FrameController();
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)

1 个答案:

答案 0 :(得分:1)

JTextField textOrder=new JTextField();
textOrder.getText() // NullPointerException if the document is null

根据文档getText()返回此TextComponent中包含的文字。如果底层文档为null,则会给出NullPointerException。

如果您正在使用其中一个构造函数,则document为null。

JTextField()JTextField(String text)JTextField(int columns)JTextField(String text, int columns)