Results 1 to 10 of 10
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default Write empty Jtable columns to disk

    <SOLVED>

    Evening all.

    Have this small problem, if i happen to have an empty column when i save the data from JTable to disk, it erase all data stored in the file and cast a


    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    I use Vectors with DefaultTableModel. Mabey I can solve it within the try and catch part? Or you guys have a better ide?
    Last edited by ocean; 12-03-2009 at 03:03 PM. Reason: Solved this. and dont know how to mark it "Solved"

  2. #2
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    post a bit of your code?

  3. #3
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    This is the method for saving it to disk.

    Java Code:
    void writeProject(String fileName){
    
              //  Finish editing the current cell before saving
          if  (custTable.isEditing() ){
    	   int row =custTable.getEditingRow();
    	   int col =custTable.getEditingColumn();
               custTable.getCellEditor(row, col).stopCellEditing();
           }
    
            int rows = custTable.getRowCount();
    	 int columns = custTable.getColumnCount();
          
           TableColumnModel header =custTable.getColumnModel();
    
    try {
           FileWriter fw = new FileWriter("/home/magnus/NetBeansProjects/files/" + fileName +".cust");
           BufferedWriter out = new BufferedWriter (fw);
    
             //  Write out the Column Headers
    	for (int k = 0; k < columns; k++){
    	TableColumn column = header.getColumn(k);
    	String value = (String)column.getHeaderValue();
            out.write(value);
            out.write("|");
    	} out.newLine();
    
             //  Write out the table rows
    	for (int j = 0; j < rows; j++){
    		for (int k = 0; k < columns; k++){
    		String value = (String)custTable.getValueAt(j, k);
                    out.write(value);
                    out.write("|");
    		}
                    out.newLine();
    		}
                    out.close();
     }catch(IOException ee) {
                 throw new RuntimeException(ee);
            }
    } // END WriteProject/

  4. #4
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    the line befor out.close(); write out.flush();
    does it saves now?

  5. #5
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    and by the way, does it saves sometimes?
    or does it always give a nullpointer exception?

  6. #6
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    It saves fine if there is a value in every column.

  7. #7
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Think i migth get it solved. Trying something like

    Java Code:
    if (value == null){
              code
     }
    Just gonna come up something good for the code part.

  8. #8
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    what's in your file when you save it if not every collumn has a value?

  9. #9
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    If i save with a collumn = null it erase all data stored in the file.

    Not sure yet how Im gonna handle this. Do I want the user to be able to save null columns or should i make sure he/she fills them. Makes more sence to allow user to save empty data i guess.

  10. #10
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Hm. Very Strange.. I had to do

    from
    Java Code:
    out.write(value);
    out.write("|");
    to
    Java Code:
    out.write(value +"|");
    It writes the word NULL for every empty column. and are not deleting file contens. Great. :)

Similar Threads

  1. Sorting and hiding columns in JTable
    By ProgrammingPup in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 09:59 PM
  2. Replies: 7
    Last Post: 09-23-2009, 07:28 PM
  3. How to sort a JTable for multiple columns?
    By BLR in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2009, 10:41 AM
  4. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 09:01 PM
  5. sort columns in jtable
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-14-2007, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •