1

The Program will update once, I can go in and change something in the table and hit update. I can update as long as I don't exit the program, once I run the program again I notice that it saved the cell I changed, but it also adds a blank line of cells. I tried to update some changes again it errors on me:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:157)
    at testaddress.Testaddress$3.actionPerformed(Testaddress.java:267)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

It also wipes my whole file of information, leaving a blank txt file. Heres the code:

update.addActionListener(new ActionListener() {


     @Override
     public void actionPerformed(ActionEvent e) {
     try{
        BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
        for (int i = 0 ; i < table.getRowCount(); i++)
        {
           bfw.newLine();
           for(int j = 0 ; j < table.getColumnCount();j++)
           {
              bfw.write((String)(table.getValueAt(i,j)));
              bfw.write("\t");
           }
        }
        bfw.close();
     } catch(IOException ea) {
       System.out.println(
       "Input/Output Exception occurred: " +
       ea.getMessage()); 
     } 
}}); 

Here is the rest of the code:

package testaddress;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.table.DefaultTableModel;



public class Testaddress extends JFrame {
   public static JTable table;
   public static DefaultTableModel model;
   public static JButton update;
   public static File tmp;

    public static void MyAddressBook(Container pane) throws FileNotFoundException, IOException{

        JLabel Name;
        pane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;

        Name = new JLabel("Name:");
        gbc.weightx = 0.5;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.NORTH;      
        pane.add(Name, gbc);

        final JTextField textName = new JTextField();
        gbc.weightx = 0.5;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.NORTH;
        pane.add(textName, gbc);

        JLabel HPhone = new JLabel("Home Phone #:");
        gbc.weightx = 0.5;
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.NORTH;
        pane.add(HPhone, gbc);

        final JTextField textHPhone = new JTextField();
        gbc.weightx = 0.5;
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.NORTH;
        pane.add(textHPhone, gbc);

       JLabel CellPhone = new JLabel("Cell Phone #:");
       gbc.weightx = 0.5;
       gbc.gridx = 0;
       gbc.gridy = 1;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(CellPhone, gbc);

       final JTextField textCPhone = new JTextField();
       gbc.weightx = 0.5;
       gbc.gridx = 1;
       gbc.gridy = 1;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(textCPhone, gbc);

       JLabel Address = new JLabel("Address");
       gbc.weightx = 0.5;
       gbc.gridx = 2;
       gbc.gridy = 1;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(Address, gbc);

       final JTextField textAddress = new JTextField();
       gbc.weightx = 0.5;
       gbc.gridx = 3;
       gbc.gridy = 1;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(textAddress, gbc);

       JLabel City = new JLabel("City:");
       gbc.weightx = 0.5;
       gbc.gridx = 0;
       gbc.gridy = 2;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(City, gbc);

       final JTextField textCity = new JTextField();
       gbc.weightx = 0.5;
       gbc.gridx = 1;
       gbc.gridy = 2;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(textCity, gbc);

       JLabel State = new JLabel("State:");
       gbc.weightx = 0.5;
       gbc.gridx = 2;
       gbc.gridy = 2;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(State, gbc);

       final JTextField textState = new JTextField();
       gbc.weightx = 0.5;
       gbc.gridx = 3;
       gbc.gridy = 2;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(textState, gbc);

       JLabel Zip = new JLabel("Zipcode:");
       gbc.weighty = 0.1;
       gbc.weightx = 0.5;
       gbc.gridx = 0;
       gbc.gridy = 3;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(Zip, gbc);

       final JTextField textZip = new JTextField();
       gbc.weighty = 0.1;
       gbc.weightx = 0.5;
       gbc.gridx = 1;
       gbc.gridy = 3;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(textZip, gbc);

       JLabel Country = new JLabel("Country:");
       gbc.weightx = 0.5;
       gbc.gridx = 2;
       gbc.gridy = 3;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(Country, gbc);

       final JTextField textCountry = new JTextField();
       gbc.weighty = 0.1;
       gbc.weightx = 0.5;
       gbc.gridx = 3;
       gbc.gridy = 3;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(textCountry, gbc);

       update = new JButton("Update");
       gbc.weightx = 0.5;
       gbc.gridx = 1;
       gbc.gridy = 5;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(update, gbc);

       JButton exit = new JButton("Exit");
       gbc.weightx = 0.5;
       gbc.gridx = 3;
       gbc.gridy = 5;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(exit, gbc);

       exit.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit",
    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    {
    System.exit(0);
    }
    }
    });

       JButton save = new JButton("Save");
       gbc.weightx = 0.5;
       gbc.gridx = 2;
       gbc.gridy = 5;
       gbc.anchor = GridBagConstraints.NORTH;
       pane.add(save, gbc);

       JSeparator sep = new JSeparator();
       sep.setPreferredSize(new Dimension(5,1));
       gbc.weighty = 0.2;
       gbc.weightx = 0.5;
       gbc.gridx = 0;
       gbc.gridy = 7;
       gbc.gridwidth = 5;
       gbc.anchor = GridBagConstraints.CENTER;
       pane.add(sep, gbc);

    File fold = new File(System.getProperty("user.home")
            + "/.MyAddressBook");
    if(!fold.exists()){      
    fold.mkdir();
    }
    tmp = new File(System.getProperty("user.home") +
            "/.MyAddressBook/MyAddressBook.txt");
    if(!tmp.exists()){
    tmp.createNewFile();
    }


    table = new JTable();
    JScrollPane scroll = new JScrollPane(table);
    String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
          "City:", "State:", "Zip Code:", "Country:"};
    model = new DefaultTableModel(colNames, 0);
    final FileInputStream is;
    is = new FileInputStream(tmp);
    InsertData(is);
    gbc.weighty = 1;
    gbc.weightx = 0.5;
    gbc.gridx = 0; 
    gbc.gridy = 8; 
    gbc.gridwidth = 4;
    gbc.gridheight = 9;
    gbc.anchor = GridBagConstraints.CENTER;
    pane.add(new JScrollPane(table), gbc);

    save.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    BufferedWriter writer;
    try {
    writer = new BufferedWriter(new FileWriter(tmp,true));
    writer.write(textName.getText() + "\t" + textHPhone.getText()
    + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
    + textCity.getText() + "\t" + textState.getText() + "\t" +
    textZip.getText() + "\t" + textCountry.getText());
    writer.newLine();
    writer.close();
    } catch(FileNotFoundException ex) {
    } catch (IOException ex) {
    }
    Scanner scan = new Scanner(is);
    String[] array;
    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        if(line.indexOf("\t")>-1)
            array = line.split("\t");
        else
            array = line.split("\t");
        Object[] data = new Object[array.length];
        for (int i = 0; i < array.length; i++)
            data[i] = array[i];
        model.addRow(data);
    }
    table.setModel(model);
    }});   

    update.addActionListener(new ActionListener() {


     @Override
     public void actionPerformed(ActionEvent e) {
     try{
     BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
     for (int i = 0 ; i < table.getRowCount(); i++)
     {
     bfw.newLine();
     for(int j = 0 ; j < table.getColumnCount();j++)
     {
     bfw.write((String)(table.getValueAt(i,j)));
     bfw.write("\t");;
     }
   }
  bfw.close();
        } catch(IOException ea) {
       System.out.println(
       "Input/Output Exception occurred: " +
       ea.getMessage());
     }
     }});  

    }


 public static void InsertData(FileInputStream is){
    Scanner scan = new Scanner(is);
    String[] array;
    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        if(line.indexOf(",")>-1)
            array = line.split(",");
        else
            array = line.split("\t");
        Object[] data = new Object[array.length];
        System.arraycopy(array, 0, data, 0, array.length);
        model.addRow(data);
    }
    table.setModel(model);


}



    public static void main(String[] args) throws FileNotFoundException, IOException {
        JFrame frame = new Testaddress();
        frame.setTitle("My Address Book");
        frame.setSize(900, 600);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyAddressBook(frame.getContentPane());
        frame.setVisible(true);
    }
}

What is causing line 267 to be null and how can I fix this?

  • true ???, nobody knows whats in the rest of your code, in File, nor in JTable, question in this form isn't answerable, – mKorbel Oct 09 '13 at 18:41
  • 1
    What is in line **267** of your `Testaddress.java` file? Better post the entire method around it. – PM 77-1 Oct 09 '13 at 18:43

1 Answers1

1

Which line is 267 in Testadress.java ? I woul bet it is this:

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

So I guess table.getValueAt(i,j) returns null and the cast will bring the NullPointerException

But without the knowledge about yoour code it is impossible to help.

update

You overwrite the complete file. If you want to append you must pass true as second parameter:

new FileWriter(tmp, true);
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79