Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Having trouble writting and reading from file

sindrebsindreb Member Posts: 2
Hi!
I'm trying to make a program with a gui. I want it to be 2 textfields and one button. When the button is clicked I want the text from the first textbox to be written to a file. I also want the second textbox to display the content from the file. I have written the code, but for some reason I get a bunch of error messages. The code I use is this:
import javax.swing.JFrame;

[code]public class Main {
public static void main (String args[]){
GUI go = new GUI();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}[/code]
[code]import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class GUI extends JFrame{
private JTextField toFile;
private JTextField fromFile;
private JButton sendToFile;
private FileHandling fh;

public GUI(){
super("File");
setLayout(new FlowLayout());

toFile = new JTextField("Write what you want to send to the file",20);
add(toFile);

sendToFile=new JButton("Add to file");
add (sendToFile);

fh=new FileHandling();
fh.checkFile();
fh.readFile();

fromFile=new JTextField(fh.fromFile,20);
fromFile.setEditable(false);
add(fromFile);

HandlerClass hc=new HandlerClass();
sendToFile.addActionListener(hc);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
String a=fromFile.getText();
fh.writeFile(a);
}
}
}[/code]
[code]import java.io.*;
import java.util.*;

public class FileHandling {
private Formatter x;
private Scanner s;
private File y;
public String fromFile;

public void checkFile(){
y=new File("data.txt");
if(y.exists()){
try{
s=new Scanner(y);
}
catch(Exception e){
System.out.println("Could not scan the file");
}
}else{
try{
x=new Formatter("data.txt");
s=new Scanner(y);
}
catch(Exception e){
System.out.println("Could not create the file");
}
}
}
public void readFile(){
while(s.hasNextLine()){
fromFile=s.nextLine();
}
}
public void writeFile(String a){
x.format("%s
", a);
}
public void closeFile(){
x.close();
}
}[/code]
And the errors I get is these:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FileHandling.writeFile(FileHandling.java:35)
at GUI$HandlerClass.actionPerformed(GUI.java:37)
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.plaf.basic.BasicButtonListener.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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
Hope someone can help me out :)

Sign In or Register to comment.