我正在開發一個Java項目,目前有4個類(Driver,OrdersProcessor,Items和Purchase) ,當我運行測試時,它告訴我,我有一個NullPointerException兩行旁邊有(** * **)。 我不知道這有什麼錯他們雖然..Java空指針異常
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;
}
調試器和你在一起,小padawan – DGomez 2013-04-30 21:35:42
你在'OrdersProcessor'中初始化'items'在哪裏? – 2013-04-30 21:36:11
我有 private static Items items = null; 在一開始,這是不夠的? – CCC 2013-04-30 21:38:21