Search code examples
javaswingnetbeansjtablefilewriter

JTable FileWriter error


I'm busy with a FileWriter, and with very limited knowledge of writing text files, I found a few examples of what I need, and with that I created my own coding. I'm working in NetBeans.

The Objective:
Export JTable contents to a text file on button pressed.

The Problem:
bw.write(model.getValueAt(i, j));

The pre-error show: No suitable method found for write(Output)...

What's going on here?

This is how the process works:
1)The administrator runs the First Run Configuration
2)The administrator clicks on Add User {1}
(Apps.Settings.FTRun)

First Screen with Table

3)The administrator creates the user by entering the fields. Clicking on insert, the app creates a userid, then uploads the user to the database. ALSO, it adds the username and password to the table in FTRun.It's supposed to add the elements, but it doesn't! (Code included below)
Apps.UserManager.AddUser User Adding Frame

4)The table doesn't populate, so I type in random strings in the table. I then click on . This throws the NullPointerException

Here's my code:
1) Export Code
2) Populate Table Code
Export Code

try {
            File file = new File("C:/Program Files/DocuLoc/bin/export.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            TableModel model = jTable1.getModel();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);

            for (int i = 0; i < model.getRowCount(); i++) {
                for (int j = 0; j < model.getColumnCount(); j++) {
                    //Create your File Writer
                    bw.write(model.getValueAt(i, j));
                }
            }
            bw.close();
            JOptionPane.showConfirmDialog(this, "Table exported successfully!\nFile located at " + file);
        } catch (Exception e) {
            e.printStackTrace();
        }

Populate Table Code

try {
     Apps.Settings.FTRun ftrun = new Apps.Settings.FTRun();
     DefaultTableModel model = (DefaultTableModel) ftrun.jTable1.getModel();
     model.addRow(new Object[]{UploadUName, UploadPwd});
     ftrun.jTable1.enableInputMethods(true);
} catch (Exception e) {
     e.printStackTrace();
}

Solution

  • I would use:

    bw.write( model.getValueAt(i, j).toString() );
    

    which will write the String representation of any Object you might be storiung in your TableModel.

    Edit:

    The NPE is caused by the bw.write(model.getValueAt(i,j).toString()); line

    So what is null, "bw", "model", the data from the getValue(...) method?

    I'm guessing the data, in which cause you can use code like:

    Object data = model.getValueAt(I, j);
    
    if (data == null)
        System.out.println("null data at - " + I + " : " + j);
    else
        bw.write( data.toString() );
    

    then once you know what cell(s) are null you investigate to find out why.