Java 프로젝트에서 작업 중이며 현재 Driver, OrdersProcessor, Items 및 Purchase 클래스가 4 개 있습니다 ( ). 테스트를 실행하면 다음과 같이 NullPointerException이 발생합니다. 옆에 (** * **) 두 줄이 있습니다. 나는 ..Java Null 포인터 예외
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;
}
디버거가 당신과 함께 할 것입니다. – DGomez
'OrdersProcessor'에서'items'는 어디에 초기화합니까? –
나는 개인 정적 항목 항목 = null을가집니다; 아주 처음에는 이 충분하지 않습니까? – CCC