[英]I can't figure out why my array isn't filling here in Java
所以,arrays 絕對不是我的強項。 While testing this application in my driver class, I get the thrown exception Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "this.order[x]" is null at ics240assignment1. ShoppingCart.add(ShoppingCart.java:37) at ics240assignment1.ShoppingCartDriver.main(ShoppingCartDriver.java:8) 在代碼的這個區域中,我剛開始添加購物車永遠不會填充任何東西,從而給出“空”結果。 至少,這就是我認為正在發生的事情。 我在這里做錯了什么明顯的事情嗎?
public class ShoppingCart {
private String customer;
private GroceryItem order[];
private int numItems;
public ShoppingCart() { // must instantiate an array called GroceryItems
numItems = 0;
order = new GroceryItem[10];
customer = "";
}
public ShoppingCart(String name) {
this.customer = name;
}
public ShoppingCart(String name, int size) {
order = new GroceryItem[size];
this.customer = name;
}
public boolean add(String name, double price, int quantity) {
// changes made here
String name1 = name;
double price1 = price;
int quant = quantity;
int tempPlace = 0;
boolean found = false;
boolean full = false;
if(order == null) {
order[numItems++] = new GroceryItem(name, price, quantity);
return true;
}
for (int x = 0; x < order.length; x++) {
if (order[x].equals(name)) {
tempPlace = x;// store place in array
found = true;
}
if (found == true) {
// get current qty, add to it, then update it
int tempQty = order[tempPlace].getQty();
tempQty = tempQty + quantity;
order[tempPlace].setQty(tempQty);
full = false;
}
else if (found == false) {
for (int y = 0; y < order.length; y++) {
if (order[y] == null) {
order[y] = new GroceryItem(name, price, quantity);
full = false;
break;
}
}
full = true;
}
}
if (full == false) {
return true;
}
if (full == true) {
return false;
}
return false;
}
// added all this
public int find(String name) {
boolean found = false;
int location = 0;
GroceryItem tempItem = new GroceryItem(name, 0, 0);
for (int x = 0; x < order.length; x++) {
if (order[x].equals(tempItem)) {
found = true;
location = x;
}
}
if (found == false) {
return -1;
}
if (found == true) {
return location;
}
return 0;
}
public double getTotalBeforeTax() {
double total = 0.0;
for (int x = 0; x < order.length; x++) {
if (order[x] != null) {
total += order[x].getPrice() * order[x].getQty();
}
}
return total;
}
public double getTax(double taxRate) {
double tax = 0;
// Loops
for(int i = 0; i < numItems; i++)
// Calculates tax
tax += (order[i].getQty() * order[i].getPrice()) * (taxRate / 100.0);
// returns total tax
return tax;
}
public int getNumGroceryItems() {
int total = 0;
for (int x = 0; x < order.length; x++) {
if (order[x] != null) {
total += 1;
}
}
return total;
}
public int getTotalQuantity() {
int total = 0;
for (int x = 0; x < order.length; x++) {
if (order[x] != null) {
total += order[x].getQty();
}
}
return total;
}
public String toString() {
String output = "";
output += customer;
output += order;
output += numItems;
output += getNumGroceryItems();
return output;
}
}
驅動class供參考
public class ShoppingCartDriver {
public static void main(String[] args) {
ShoppingCart Randy = new ShoppingCart();
ShoppingCart Sharron = new ShoppingCart();
Randy.add("Eggs", 1.50, 1);
System.out.println(Randy);
}
}
兩點:
因為您將“訂單”數組大小初始化為 10(在默認構造函數下),所以當您遍歷數組時,您將得到一些項目為 null。 您應該考慮使用 numitems 作為您的限制循環,或者使用可動態調整大小的替代結構,例如 ArrayList。
但是 - 而且你的數組永遠不會填滿,因為你的 if 語句檢查 order==null,這總是錯誤的,因為 order 是一個數組。
數組就像一個小地址簿。 new GroceryItem[20]
不會生產 20 件雜貨。 它只是創建一個新的地址簿,上面有 20 頁; 每個頁面都有空間寫一個指向單個雜貨項目的指針。 這本書一開始是空白的:有 20 個參考文獻的空間,但它總是以空白頁開始( null
值)。
order = new GroceryItem[10];
是的,所以,每當您創建購物車時,它的order
價值將始終引用一個新創建的帶有 10 個空白頁的小地址簿。
第一個構造函數創建一個新的 10 頁的小地址簿,然后更新order
值以引用這個地址簿(指向它)。
您的第二個構造函數不執行任何操作,這意味着order
將保持null
(指向任何內容;根本不存在通訊簿)。
您的第三個構造函數創建一個具有規定頁數(通過參數提供)的地址簿,全部為空白。
這是你的第一個問題——第二個構造函數是奇怪的,這是沒有意義的。
if(order == null) {
如果您使用第二個構造函數創建了此 ShoppingCart 則為 true,否則不能為 null。 下一行是:
order[numItems++]
這是沒有意義的。 []
將“取消引用”-它將遵循引用。 當您對 null 值執行此操作時,您會得到一個空指針異常。 這里是null
- 您剛剛檢查過:您可能必須編寫類似: order = new GroceryItem[10]
東西。 或者更有可能的是,您永遠不想對order
進行空檢查,並且您總是希望將order
設置為指向每個構造函數中的實際 GroceryItem 數組(即更新構造函數 2,並刪除if
檢查,因為它沒有意義)。
因此,如果你調用了第二個構造函數,這個 add 方法就會在這里崩潰。 如果你調用了第一個或第三個,這個if
塊被跳過了,我們 go on,用:
for (int x = 0; x < order.length; x++) { if (order[x].equals(name)) {
同樣的問題 - order[x]
存在(這將是通過檢查您的小地址簿中的第 x 頁來查找通向 GroceryItem 的地址的指令。不幸的是,最初此頁面是空白的。
.
也取消引用,因此 NullpointerException 將始終在此處發生。
Arrays 是您不應該使用的低級構造函數。 請改用List<GroceryItem>
。 這樣的清單可以擴大和縮小。 沒有大小和空指針的討厭的事情。
如果這是某種家庭作業,並且您必須在此處使用 arrays,請記住頁面是空白的,您應該在取消引用之前檢查它(使用.
取消引用)。 您可以使用if (order[x].= null && order[x].getName().equals....
請注意,任何“真正的”java 項目(一個不是為了教學或探索的項目)都不應該這樣做。 使用列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.