带有 getText() 的 NullPointerException
我试图从文本字段获取文本,但每次运行该程序时都会收到 NullPointerException。我在互联网上发现的类似问题涉及将文本字段重新声明为局部变量,但我看不到我在这里这样做。
错误是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
在指定的行。任何帮助将不胜感激
编辑:
这个简短的程序演示了这个问题
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Test{
public static final Test application = new Test();
private JFrame jFrame = null;
private JPanel jContentPane = null, buttonsPanel = null, cardPanel = null, manualPanel = null, uploadPanel = null,
defaultPanel = null;
private JPanel[] mainPanel = {getDefaultPanel(), getManualPanel(), getUploadPanel()};
private JButton manualButton = null, uploadButton = null, manualAssignButton = null;
private JTextField manualEntryField = null;
private JLabel manualLabel = null;
final static String DEFAULTPANEL = "Default";
final static String MANUALPANEL = "Manual";
final static String UPLOADPANEL = "Upload";
/**
* Main method
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
application.getJFrame().setVisible(true);
}
});
}
/**
* Get the main application frame
* @return JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jFrame.setResizable(false);
// Add the main content pane
jFrame.setContentPane(getJContentPanel());
jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
closeApplication();
}
});
}
jFrame.pack();
jFrame.setLocationRelativeTo(null);
return jFrame;
}
/**
* Get main content
* @return JPanel
*/
private JPanel getJContentPanel(){
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setPreferredSize(new Dimension(500, 200));
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getButtonsPanel(), BorderLayout.NORTH);
jContentPane.add(getCardPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
private JPanel getButtonsPanel() {
if(buttonsPanel == null){
buttonsPanel = new JPanel();
buttonsPanel.setPreferredSize(new Dimension(500, 50));
buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
manualButton = new JButton("Enter manually");
manualButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)(cardPanel.getLayout());
cl.show(cardPanel, MANUALPANEL);
}
});
uploadButton = new JButton("Upload file");
uploadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)(cardPanel.getLayout());
cl.show(cardPanel, UPLOADPANEL);
}
});
buttonsPanel.add(manualButton);
buttonsPanel.add(uploadButton);
}
return buttonsPanel;
}
private JPanel getCardPanel() {
if(cardPanel == null){
cardPanel = new JPanel(new CardLayout());
cardPanel.setPreferredSize(new Dimension(500, 200));
cardPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
cardPanel.add(mainPanel[0], DEFAULTPANEL);
cardPanel.add(mainPanel[1], MANUALPANEL);
cardPanel.add(mainPanel[2], UPLOADPANEL);
}
return cardPanel;
}
private JPanel getDefaultPanel() {
if(defaultPanel == null){
defaultPanel = new JPanel();
}
return defaultPanel;
}
private JPanel getManualPanel() {
if(manualPanel == null){
manualPanel = new JPanel();
manualPanel.setLayout(new FlowLayout());
manualEntryField = new JTextField(10);
manualEntryField.setText("Enter code");
manualAssignButton = new JButton("Assign");
manualAssignButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Here is the exception
System.out.println(manualEntryField.getText().trim());
}
});
manualLabel = new JLabel("Please enter a code and press assign");
manualPanel.add(manualEntryField);
manualPanel.add(manualAssignButton);
manualPanel.add(manualLabel);
}
return manualPanel;
}
private JPanel getUploadPanel() {
if(uploadPanel == null){
uploadPanel = new JPanel();
}
return uploadPanel;
}
private void closeApplication() {
System.exit(0);
}
}
I'm trying to get text from a text field but every time I run the program I get a NullPointerException. Similar problems I've found on the internet involve the text field being redeclared as a local variable, but I can't see that I am doing that here.
The error is :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at the line specified. Any help would be much appreciated
EDIT:
This short program demonstrates the problem
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Test{
public static final Test application = new Test();
private JFrame jFrame = null;
private JPanel jContentPane = null, buttonsPanel = null, cardPanel = null, manualPanel = null, uploadPanel = null,
defaultPanel = null;
private JPanel[] mainPanel = {getDefaultPanel(), getManualPanel(), getUploadPanel()};
private JButton manualButton = null, uploadButton = null, manualAssignButton = null;
private JTextField manualEntryField = null;
private JLabel manualLabel = null;
final static String DEFAULTPANEL = "Default";
final static String MANUALPANEL = "Manual";
final static String UPLOADPANEL = "Upload";
/**
* Main method
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
application.getJFrame().setVisible(true);
}
});
}
/**
* Get the main application frame
* @return JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jFrame.setResizable(false);
// Add the main content pane
jFrame.setContentPane(getJContentPanel());
jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
closeApplication();
}
});
}
jFrame.pack();
jFrame.setLocationRelativeTo(null);
return jFrame;
}
/**
* Get main content
* @return JPanel
*/
private JPanel getJContentPanel(){
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setPreferredSize(new Dimension(500, 200));
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getButtonsPanel(), BorderLayout.NORTH);
jContentPane.add(getCardPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
private JPanel getButtonsPanel() {
if(buttonsPanel == null){
buttonsPanel = new JPanel();
buttonsPanel.setPreferredSize(new Dimension(500, 50));
buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
manualButton = new JButton("Enter manually");
manualButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)(cardPanel.getLayout());
cl.show(cardPanel, MANUALPANEL);
}
});
uploadButton = new JButton("Upload file");
uploadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)(cardPanel.getLayout());
cl.show(cardPanel, UPLOADPANEL);
}
});
buttonsPanel.add(manualButton);
buttonsPanel.add(uploadButton);
}
return buttonsPanel;
}
private JPanel getCardPanel() {
if(cardPanel == null){
cardPanel = new JPanel(new CardLayout());
cardPanel.setPreferredSize(new Dimension(500, 200));
cardPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
cardPanel.add(mainPanel[0], DEFAULTPANEL);
cardPanel.add(mainPanel[1], MANUALPANEL);
cardPanel.add(mainPanel[2], UPLOADPANEL);
}
return cardPanel;
}
private JPanel getDefaultPanel() {
if(defaultPanel == null){
defaultPanel = new JPanel();
}
return defaultPanel;
}
private JPanel getManualPanel() {
if(manualPanel == null){
manualPanel = new JPanel();
manualPanel.setLayout(new FlowLayout());
manualEntryField = new JTextField(10);
manualEntryField.setText("Enter code");
manualAssignButton = new JButton("Assign");
manualAssignButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Here is the exception
System.out.println(manualEntryField.getText().trim());
}
});
manualLabel = new JLabel("Please enter a code and press assign");
manualPanel.add(manualEntryField);
manualPanel.add(manualAssignButton);
manualPanel.add(manualLabel);
}
return manualPanel;
}
private JPanel getUploadPanel() {
if(uploadPanel == null){
uploadPanel = new JPanel();
}
return uploadPanel;
}
private void closeApplication() {
System.exit(0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看您的 SSCCE 后,我通过注释掉实例化的冗余
Test
并在构造函数中创建面板数组,成功解决了 NPE。 EG注意:我无法完全解释为什么这些更改解决了 NPE,我只是重构“可疑”代码。
After looking at your SSCCE, I managed to solve the NPE by commenting out the redundant
Test
that was instantiated, and creating the panel array in the constructor. E.G.Note: I cannot quite explain why the changes solved the NPE, I was just refactoring 'suspicious' code.
我确实解决了与 NullPointerException 相关的查询:
问题是您声明类变量:
只需编写:
只需删除那个 null 的东西,反对它,其余的代码就可以了。无法说出为什么会发生,但可能是其他人可以。
这是您的代码,仅进行了更改,请立即尝试:
问候
I did solved your query related to NullPointerException :
The problem being that you declaring your class variable :
Just write :
Just remove that null thing, against it and the rest of the code is fine. Cannot say as to why it happened, but might be someone else can.
Here is your code with only that change, do try this out now :
Regards
如果textBox为空,则此异常是正常的。试试这个
if(getText().size()<1) 条件或使用 try-catch,当捕获异常时设置值非大小字符串“”。
If textBox is empty, it's normal for this exception. Try this
if(getText().size()<1) condition or use try-catch and when you catch an exception set the value non-size string "".
当文本框为空时您是否单击按钮?如果是这样,那么当您调用trim()函数时,您可能会遇到该异常,因此在调用trim()之前,请先查看getText()返回的内容
Are you clicking the button when your text box is empty ? If so, then u can end up with that exception when u r calling the trim() function , so see what getText() returns , before calling the trim()