-1

我正在尝试从用户那里获取字符串输入并将其存储在一个文件中,但出现一个错误:我不知道问题是什么,在我实际运行代码之前它不会作为错误弹出. 我只想将用户的输入存储在一个文件中

Exception in thread "main" java.lang.NullPointerException
    at BingoHelper.<init>(BingoHelper.java:53)
    at BINGO.main(BINGO.java:8)

主类代码:

import javax.swing.*;
import java.io.*;

    public class BINGO {
        public static void main(String[] args) throws IOException{
            JLabel bg = new JLabel();
            //JButton b = new JButton("Click to enter name");
            BingoHelper EnterFaze = new BingoHelper();
            EnterFaze.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            EnterFaze.setSize(500,500);
            EnterFaze.setVisible(true);
            EnterFaze.setLocationRelativeTo(null);
            EnterFaze.setLayout(null);
            EnterFaze.add(bg);
        }
    }

二等舱

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class BingoHelper extends JFrame implements WindowListener, ActionListener{
    JTextField text = new JTextField();

JLabel bg = new JLabel();

private JButton b; {
        b = new JButton("Click to enter name");
        }

JPanel pnlButton = new JPanel();


public static String fn;
public static String sn;

public void actionPerformed (ActionEvent e) {
    //BingoHelper.fn;
    //BingoHelper.sn;
    fn = JOptionPane.showInputDialog("What is your first name?");
    sn = JOptionPane.showInputDialog("What is your second name(Optional)");
    //JOptionPane.showMessageDialog(null, "Welcome " + fn + " " + sn + ".", "", JOptionPane.INFORMATION_MESSAGE);
    text.setText("Welcome " + fn + " " + sn + ".");
    b.setVisible(false);
    text.setVisible(true);
    text.setBounds(140,0,220,20);
    text.setHorizontalAlignment(JLabel.CENTER);
    text.setEditable(false);
    text.setBackground(Color.YELLOW);
} 

public BingoHelper() throws IOException{
    super("BINGO");
    add(text);
    text.setVisible(false);
    add(b);
    this.add(pnlButton);
    pnlButton.setBackground(Color.BLUE);
    //pnlButton.add(b);+
    b.setVisible(true);
    b.setBounds(145,145,145,20);
    //b.setPreferredSize(new Dimension(150,40));
    b.addActionListener(this);
    b.setBackground(Color.GREEN);
    rootPane.setDefaultButton(b);
    File f = new File("test.txt");

    String nameToWrite = fn;
    OutputStream outStream = new FileOutputStream(f);
    outStream.write(nameToWrite.getBytes());
    outStream.close();
}

public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);

}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}

}

4

3 回答 3

2

这就是问题:

String nameToWrite = fn;
...
outStream.write(nameToWrite.getBytes());

fnactionPerformed至少在被调用一次后才会有一个非空值。因此,当您调用构造函数时,fn为 null,因此nameToWrite为 null,因此nameToWrite.getBytes()抛出NullPointerException. 你希望在这一点上得到什么名字?为什么在构造函数期间写入文件?这似乎是一件很奇怪的事情。它不应该是为了响应用户操作而编写的吗?

(顺便说一句,不要在getBytes()不指定字符集的情况下使用 - 并且更喜欢在OutputStreamWriter周围创建一个OutputStream,而不是自己执行文本到二进制转换。并使用 finally 块或 try-with-resources 语句关闭流.)

于 2013-10-16T20:37:26.530 回答
1

将您的帮助代码更改为以下内容以获得您正在寻找的内容。我所做的改变是,

  1. 在构造函数中删除了写入文件的代码。
  2. 添加了写入文件的新方法。
  3. 从 actionPerformed 调用新方法。


public class BingoHelper extends JFrame implements WindowListener, ActionListener {
    JTextField text = new JTextField();
    JLabel bg = new JLabel();
    private JButton b;
    {
        b = new JButton("Click to enter name");
    }
        JPanel pnlButton = new JPanel();
        public static String fn;
        public static String sn;

public void actionPerformed(ActionEvent e) { // BingoHelper.fn; // BingoHelper.sn; fn = JOptionPane.showInputDialog("What is your first name?"); sn = JOptionPane.showInputDialog("What is your second name(Optional)"); // JOptionPane.showMessageDialog(null, "Welcome " + fn + " " + sn + ".", "", // JOptionPane.INFORMATION_MESSAGE); text.setText("Welcome " + fn + " " + sn + "."); b.setVisible(false); text.setVisible(true); text.setBounds(140, 0, 220, 20); text.setHorizontalAlignment(JLabel.CENTER); text.setEditable(false); text.setBackground(Color.YELLOW); writeToFile(); } private void writeToFile() { File f = new File("test.txt"); String nameToWrite = fn; OutputStream outStream = null; try { outStream = new FileOutputStream(f); outStream.write(nameToWrite.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != outStream) { try { outStream.close(); } catch (IOException e) { // do nothing } } } } public BingoHelper() throws IOException { super("BINGO"); add(text); text.setVisible(false); add(b); this.add(pnlButton); pnlButton.setBackground(Color.BLUE); // pnlButton.add(b);+ b.setVisible(true); b.setBounds(145, 145, 145, 20); // b.setPreferredSize(new Dimension(150,40)); b.addActionListener(this); b.setBackground(Color.GREEN); rootPane.setDefaultButton(b); } public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } public void windowOpened(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { }}
于 2013-10-16T21:16:21.360 回答
0

由于 fn 为空,您得到了 NullPointerException。NullPointerException 在 Java 编程中非常常见,您应该查看所有抛出异常的行号中的变量。为了更好地理解 NullPointerException,请参阅http://javapapers.com/core-java/nullpointerexception-null-bad-good-and-ugly/

您真的要从构造函数中写入文件吗?

于 2013-10-16T20:53:01.163 回答