写入文件时出现NullPointer异常

时间:2015-04-14 08:19:40

标签: java nullpointerexception iterator bufferedwriter

所以,我一直在尝试编写一个方法,我可以编写Item,Client和Order对象的字段,但是当我运行这个程序时,它会产生nullpointexception错误,我不知道是什么导致它。作为一个参数,我给出了一个文件的路径,例如:" C:\ Java \ info.txt"

对此问题的任何帮助都将不胜感激。

public class IO {
    ItemClass item;
    ClientClass client;
    OrderClass order;
    private static HashSet<ItemClass> Items;
    private static HashSet<ClientClass> Orders;
    private static HashSet<OrderClass> Clients;



public void writeToFile(String directory) throws IOException{
    File file = new File(directory);

    if(!file.exists()){
        file.createNewFile(); 
    }

    BufferedWriter bufferedW = new BufferedWriter(new FileWriter(directory));
    Orders=getOrders();
    Iterator <OrderClass>iteratorOrders = Orders.iterator();
    while(iteratorOrders.hasNext()){
        order = iteratorOrders.next();
        bufferedW.write(order.getIdNumber());
        bufferedW.write(order.getPersonalID());
        bufferedW.write(order.getAddress());
        bufferedW.write(order.getCountry());
        bufferedW.write(order.getStatus());
    }
    Clients=getClients();
    Iterator <ClientClass>iteratorClients = Clients.iterator();
    while(iteratorClients.hasNext()){
        client = iteratorClients.next();
        bufferedW.write(client.getPersonalID());
        bufferedW.write(client.getName());
        bufferedW.write(client.getSurname());
        bufferedW.write(client.getBirthDate());
        boolean active = client.getIsActive();
        boolean activeOrder = client.getHasActiveOrder();
        bufferedW.write(String.valueOf(active));
        bufferedW.write(String.valueOf(activeOrder));
    }
    Items=getItems();
    Iterator <ItemClass>iteratorItems = Items.iterator();
    while(iteratorItems.hasNext()){
        item = iteratorItems.next();
        bufferedW.write(item.getItemID());
        int amount = item.getAmount();
        double price = item.getPrice();
        bufferedW.write(String.valueOf(amount));
        bufferedW.write(String.valueOf(price));
        bufferedW.write(item.getName());
        bufferedW.write(item.getType());
        bufferedW.write(item.getMadeIn());
    }
    bufferedW.close();
}

}

0 个答案:

没有答案