我正在开发一个 Java 项目,目前有 4 个类(Driver、OrdersProcessor、Items 和 Purchase),当我运行测试时,它告诉我在两行有一个 NullPointerException (** * * *) 在他们旁边。虽然我不确定他们有什么问题..
public class OrdersProcessor {
private static Items items = null;
//added
items = new Items(numOrders);
public static void runOrderProcessor(BufferedReader file, int id) {
double grandTotal = 0;
int clientId = 1000 + id;
try {
System.out.println("Reading order for client with id: " + clientId);
file.readLine();
while (true) {
grandTotal += items.buy(file.readLine().split(" ")[0], id); (*****)
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer writeReport = new StringBuffer();
writeReport.append("----- Order details for client with Id: "
+ clientId + " -----" + "\n");
for (String bought : items.allItems()) {
writeReport.append("Item's Name: " + items.getItem(bought)
+ items.getItem(bought).recipt(id));
writeReport.append("Order Total: "
+ NumberFormat.getCurrencyInstance().format(grandTotal)
+ "\n");
}
}
}
而另一类:
public class Items {
private Map<String, Purchase> items;
private double grandTotal;
private int numOrders;
public Items(int numOrders) {
this.numOrders = numOrders;
reportOrders = new TreeMap <Integer, String>();
items = new TreeMap<String, Purchase>();
grandTotal = 0;
public double buy(String name, int id) {
double price = getItem(name).purchaseItem(id); (*****)
synchronized (lockGT) {
grandTotal = grandTotal + price;
}
return price;
}