I'm trying to get the string input from the user and store it in a file but an error comes out saying: I don't know what the issue is, it doesn't pop up as an error until I actually run the code. I just want the input from the user stored in a file that is made
Exception in thread "main" java.lang.NullPointerException
at BingoHelper.<init>(BingoHelper.java:53)
at BINGO.main(BINGO.java:8)
Main Class Code:
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);
}
}
Second Class
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) {}
}
This is the problem:
String nameToWrite = fn;
...
outStream.write(nameToWrite.getBytes());
fn
will only have a non-null value after actionPerformed
has been called at least once. So when you call the constructor, fn
is null, so nameToWrite
is null, so nameToWrite.getBytes()
throws a NullPointerException
. What name did you expect to get at this point? Why are you writing to a file during a constructor? That seems a very odd thing to do. Shouldn't it be written in response to a user action?
(As an aside, don't use getBytes()
without specifying a charset anyway - and prefer creating an OutputStreamWriter
around the OutputStream
, rather than performing the text to binary conversion yourself. And close the stream either with a finally block, or a try-with-resources statement.)
See more on this question at Stackoverflow