Basmeuw Basmeuw - 7 days ago 6
Java Question

BufferedReader Error?

I get an error and I really don't know why and I can't fix it. It's an error with the BufferedReader library. Tell me if I need to include the error itself too. I've searched about all the syntax but I think all of it's right.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

public class Words extends JFrame implements ActionListener {
public static void main(String[] args) {
new Words();
}

private JTextArea text;

private Words() {

super("Words");
setVisible(true);
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(true);

initialize();
}

private void initialize(){

JTextArea text = new JTextArea();
add(text);


JMenuBar bar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem save = new JMenuItem("Save");
JMenuItem saveas = new JMenuItem("Save as");
JMenuItem open = new JMenuItem("Open");
JMenuItem exit = new JMenuItem("Exit");

//ActionListener toevoegen aan elk menuItem
JMenuItem[] items = {open, save, saveas, exit};
for(JMenuItem item : items){
item.addActionListener(this);
}


setJMenuBar(bar);
bar.add(file);
file.add(open);
file.add(save);
file.add(saveas);
file.addSeparator();
file.add(exit);

}

@Override
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()){
case "Open":
open(); break;
case "Save":
save(); break;
case "Save as":
saveas(); break;
case "Exit":
System.exit(0);
}
}
private void open(){


try {
BufferedReader br = new BufferedReader(new FileReader("file.txt"));

String line;
while((line = br.readLine()) != null) {
text.append(line + "\n");
}

br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

private void save(){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));

bw.write(text.getText());

bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private void saveas(){
System.out.println("saveas");
}

}


This is the error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Words.save(Words.java:101)
at Words.actionPerformed(Words.java:70)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.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)

Answer

Your initialize method is creating a local variable text and initializing it. You should change your

JTextArea text = new JTextArea();

to only

text = new JTextArea();

in order to initialize the field.