Question

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();
}
Was it helpful?

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.

OTHER TIPS

None of BufferedWriter's write methods take an Object type as returned by getValueAt. You could do

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

Thanks to @camickr and @Reimeus for assistance in solving this!

This code shows how you write JTable contents to a text file.

If your table has values, your code should look like this:

try {
    File file = new File(filePathAndFileName);  //Format: C:/.../.../file.txt

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

    //OPTIONAL: clear the file contents. Omitting this will continue 
    //          writing the file
    PrintWriter writer = new PrintWriter(file.getAbsoluteFile());
    writer.print("");
    writer.close();

    //Start the writing process
    TableModel model = jTable1.getModel();
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);

    //Execute writer to write until all values in the table have been written
    for (int i = 0; i < model.getRowCount(); i++) {
        for (int j = 0; j < model.getColumnCount(); j++) {
        Object data = model.getValueAt(i, j);
        bw.write(data.toString());
        }
    }

    //OPTIONAL - If you have multiple columns and want to separate, use the 
    //           following instead of the execution above

    //FORMAT   Column1 : Column2 : ...
    //new line Column1 : Column2 : ...

    for (int i = 0; i < model.getRowCount(); i++) {
        for (int j = 0; j < model.getColumnCount(); j++) {
            Object data = model.getValueAt(i, j);
            bw.write(data.toString());

            //Custom coding here for the row. I used two columns
            if (j == 0) {
                bw.write(" : ");
            } else {
            }
        }
        bw.newLine();
    }

    //End the Writing Process
    bw.close();
    System.out.println("Writing complete");
}catch(Exception ex){
    e.printStackTrace();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top