线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException错误,该怎么办?

时间:2013-10-09 18:38:14

标签: java swing file-io jtable actionlistener

程序将更新一次,我可以进入并更改表中的内容并点击更新。我可以更新,只要我不退出程序,一旦我再次运行程序,我注意到它保存了我更改的单元格,但它也添加了一行空白单元格。我试图再次更新一些更改它对我的错误:

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)

它还会擦除我的整个信息文件,留下一个空白的txt文件。 下面是代码:

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()); 
     } 
}}); 

以下是代码的其余部分:

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);
    }
}

是什么导致第267行为空,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

Testadress.java中哪一行是267? 我敢打赌这是这样的:

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

所以我猜table.getValueAt(i,j)会返回null,演员会带来NullPointerException

但是如果不了解您的代码,就无法提供帮助。

<强>更新

您将覆盖整个文件。如果你想追加,你必须pass true as second parameter

new FileWriter(tmp, true);