3

Whenever i try to save a file i get

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.io.Writer.write(Unknown Source) at reu.FileMod.saveFile(FileMod.java:47)

my code so far

    package reu;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import userInterface.TabOne;

public interface FileMod {

    public static void saveFile() {

        File file= null;

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Specify a file to save");

        int userSelection = fileChooser.showSaveDialog(null);

        if (userSelection == JFileChooser.APPROVE_OPTION) {
             file = fileChooser.getSelectedFile();

        }

        BufferedWriter bfw;
        try {

            if(!file.exists()){

                file.createNewFile();
            }

            bfw = new BufferedWriter(new FileWriter(file));

            for (int i = 0; i < TabOne.table.getColumnCount(); i++) {
                bfw.write(TabOne.table.getColumnName(i));
                bfw.write("\t");
            }

            for (int i = 0; i < TabOne.table.getRowCount(); i++) {
                bfw.newLine();
                for (int j = 0; j < TabOne.table.getColumnCount(); j++) {
                    bfw.write((String) (TabOne.table.getValueAt(i, j)));
                    bfw.write("\t");
                    ;
                }
            }
            bfw.close();
            JOptionPane.showMessageDialog(null, "Successful!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

The file gets created but empty.

  • Post the full stack trace and tell us which line in your code throws the exception. – user1803551 Dec 23 '15 at 12:45
  • @user1803551, as far as I understood there is no exception. The problem rather is that the table you access (`TabOne.table`) is empty. You are not using an instance of the class in particular, neither a singleton design pattern. `TabOne tableOne = new TabOne()` and filling the table should solve the issue. – LordAnomander Dec 23 '15 at 12:48
  • @tbrown The OP states in their *first sentence* that he gets an exception and posts the message. How can you say that there is no exception? – user1803551 Dec 23 '15 at 12:50
  • @user1803551 True enough, sorry. As for the code, I would try to access a certain cell of the table and see if it leads to a NullPointerException as well, my guess is yes, because the access to the table doesn't seem to be right. – LordAnomander Dec 23 '15 at 12:52
  • Are you sure this is an interface? How are you implementing a non-`default` method in an interface? – user1803551 Dec 23 '15 at 12:58
  • My table does have empty cells. Is that causing the exception? – Drake224025 Dec 23 '15 at 13:14

1 Answers1

1

bfw.write(TabOne.table.getColumnName(i)); bfw.write((String) (TabOne.table.getValueAt(i, j)));

either getColumnName(i) is returning null, or getValueAt(i,j) is returning null. Writer throws a NullPointerException if you write to write a null value.

In both cases, you should save into a variable, test for null, and replace with "" if it is, like

String val = (String) TabOne.table.getValueAt(i, j);
if (val == null) {
  val = "";
}
btw.write(val);
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80