gpt4 book ai didi

java - 如何从 JTextField 读取 int 值?

转载 作者:行者123 更新时间:2023-11-30 06:44:03 24 4
gpt4 key购买 nike

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

   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)

最佳答案

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

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

如果您使用这些构造函数之一,则文档为空。

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

关于java - 如何从 JTextField 读取 int 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43957854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com