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