0

When I use my removeOrder() method it successfully takes the order from the list and then I have to use my save() method which runs this code. I get thrown out with a null pointer exception because the method doesn't complete when there are 0 items in the list. I need some help figuring this one out please. I understand why i'm getting the null pointer exception, printWriter is null and my method wont iterate over the orderList if there are no orders in the list so it goes to out.close() and it can't close with a null value. I just need help figuring out the best way to possibly do a if statement to make it work the way I want it too.

    private void writeToFile(List<Order> orderList) throws 
    FlooringMasteryDaoFileException {
    //PrintWriter out = null;
    LocalDate date;

    //if (orderList.size() >= 1)
    for (Order dateOrder : orderList) { // iterate over my orderList 
    and then get the date from each Order, then format that date to a 
    string.
        date = dateOrder.getDate();
        String stringDate = formatDate(date);

        try {
            out = new PrintWriter(new FileWriter("order_" + stringDate 
    + ".txt"));
        } catch (IOException e) {
            throw new FlooringMasteryDaoFileException("Could not save 
    order data.");
        }

        for (Order currentOrder : orderList) {
            if (currentOrder.getDate().isEqual(date)) {

                out.println(currentOrder.getOrderNumber() + DELIMETER
                        + currentOrder.getCustomerName() + DELIMETER
                        + currentOrder.getTaxRate() + DELIMETER
                        + currentOrder.getProduct() + DELIMETER
                        + currentOrder.getArea() + DELIMETER
                        + currentOrder.getMaterialCost() + DELIMETER
                        + currentOrder.getLaborCost() + DELIMETER
                        + currentOrder.getTax() + DELIMETER
                        + currentOrder.getTotal() + DELIMETER
                        + currentOrder.getDate());
            }
        }
        out.flush();
    }

    out.close();
}
  • Which line the exception originates in? – Manish Kumar Sharma May 13 '17 at 04:56
  • When it gets to the first for loop it jumps to out.close() and then throws the exception. – Tyler Gray May 13 '17 at 05:00
  • So if it doesn't enter the loop for empty list, out doesn't get initialized anf closing it therefore gets you a null. call out.close() as if(out != null){ out.close(); } – Manish Kumar Sharma May 13 '17 at 05:06
  • I tried that and it gets rid of my nullPointerException but it still doesn't clear up the problem of removing an order when there is only one order in the list. Thats my main issue – Tyler Gray May 13 '17 at 05:18
  • Can you post your removeOrder() ? – Manish Kumar Sharma May 13 '17 at 05:23
  • @Override public Order removeOrder(String orderNo, LocalDate date) throws FlooringMasteryDaoFileException, NoOrderException { loadOrders(date); if (!orders.isEmpty()) { findOrderById(orderNo); } Order removedOrder = orders.remove(orderNo); return removedOrder; } – Tyler Gray May 13 '17 at 05:26
  • I dont know how to post that as code – Tyler Gray May 13 '17 at 05:28
  • Well, it doesn't have a call to writeTofile(). Can you post your code for the relevant parts here: https://pastebin.com/ ? It would be much convenient to look it there. – Manish Kumar Sharma May 13 '17 at 05:34
  • https://pastebin.com/xG4g4HJD – Tyler Gray May 13 '17 at 05:40
  • After I use the removeOrder method it clears out the orderList so I then have to use the save() method which saves that orderList to the map. I posted all those methods – Tyler Gray May 13 '17 at 05:41
  • For every order, you are creating a file as well as a stream for that but aren't closing them all. For ex, for order1, out = new PrintWriter(...) and then you move on to next while you close out at last. out got reassigned several times without closing. How about, you move out.close() inside the loop ? – Manish Kumar Sharma May 13 '17 at 06:02
  • I can try that and debug again – Tyler Gray May 13 '17 at 06:04
  • That didnt work, the main problem is my try, catch block only runs if there is an order in orderList. If I do removeOrder and then try to save that to the map there are no orders in the orderList. – Tyler Gray May 13 '17 at 06:16

0 Answers0