2017-06-29 60 views
1

在該方法中,我有所有這些初始化我的陣列打印NULL

Scanner input = new Scanner(System.in); 
    File file = new File("order.dat"); 
    File viewOrder = new File("ViewOrder.dat"); 

    String orderNo, itemNo, itemNameHolder, qtyHolder, priceHolder, status; 
    int hold, count = 0, countArray = 0; 
    double tempPriceHolder, totalPrice = 0; 
    String tempStatus = ""; 

    String[] holdItemNo = null; 
    String[] holdName = null; 
    Integer[] holdQty = null; 
    Double[] holdTotal = null; 
    String[] holdStatus = null; 

後,我嘗試閱讀我的文件中的所有內容和存儲內容爲holdX陣列

 try { 
     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 

     String line = null; 

     while ((line = br.readLine()) != null) { 
      String tokens[] = line.split(";"); 

      orderNo = tokens[0]; 
      itemNo = tokens[1]; 
      itemNameHolder = tokens[2]; 
      qtyHolder = tokens[3]; 
      priceHolder = tokens[4]; 
      status = tokens[5]; 

      if (orderNo.equalsIgnoreCase(userOrderNo)) { 

       tempPriceHolder = Double.parseDouble(priceHolder); 
       hold = Integer.parseInt(qtyHolder); 
       tempPriceHolder = tempPriceHolder * hold; 
       totalPrice += tempPriceHolder; 

       countArray++; 
       holdItemNo = new String[countArray]; 
       holdName = new String[countArray]; 
       holdQty = new Integer[countArray]; 
       holdTotal = new Double[countArray]; 
       holdStatus = new String[countArray];     

       if (status.matches("s")) { 

        tempStatus = "Success"; 
       } else if (status.matches("p")) { 
        tempStatus = "Partially Full"; 
       } else if (status.matches("o")) { 
        tempStatus = "Out of Stock"; 
       } 

       holdItemNo[count] = itemNo; 
       holdName[count] = itemNameHolder; 
       holdQty[count] = hold; 
       holdTotal[count] = tempPriceHolder; 
       holdStatus[count] = tempStatus; 

       count++; 
      } 
     } 

    } catch (Exception e) { 
     System.out.println("Error"); 
    } 

決賽,我把我的所有數組寫入一個新文件。

 System.out.printf("%s %15s %15s %10s %10s\n", "Item No", "Description", "Quantity", "Total", "Status"); 

    for (int i = 0; i < holdItemNo.length; i++) {    
     System.out.printf("\n%-11s %-18s %-13s $%-8s %s \n", holdItemNo[i], holdName[i], holdQty[i], holdTotal[i], holdStatus[i]);   
    } 
    System.out.println("-----------------------------------------------------------------------"); 
    System.out.printf("%46s %s\n", "$", totalPrice); 

    System.out.print("Print Order to file Y/N: "); 
    String choice = input.next(); 

    if (choice.equalsIgnoreCase("y")) { 
     try { 
      PrintWriter bw = new PrintWriter(new FileWriter("ViewOrder.dat", true)); 

      for (int i = 0; i < holdItemNo.length; i++) { 
       bw.write(userOrderNo + ";" + holdItemNo[i] + ";" + holdName[i] + ";" + holdQty[i] + ";" + holdTotal[i] + ";" + holdStatus[i] + "\n"); 
       bw.flush(); 
      } 

      bw.flush(); 
      bw.close(); 

      System.out.println("Sucessfull!"); 
     } catch (Exception e) { 
      System.out.println("Error"); 
     } 

    } else if (choice.equalsIgnoreCase("n")) { 

     System.out.println(""); 
    } 

但問題是即使我的代碼工作,但輸出不是我所期望的。它打印出最後的內容並且子價格也可以正常工作,但其餘的只打印出NULL。 Example

而且,它給了我在array.length

for (int i = 0; i < holdItemNo.length; i++) { 
       bw.write(userOrderNo + ";" + holdItemNo[i] + ";" + holdName[i] + ";" + holdQty[i] + ";" + holdTotal[i] + ";" + holdStatus[i] + "\n"); 
       bw.flush(); 
      } 
+2

時間來啓動舊的調試器,以真正看到自己爲什麼事情不像預期的那樣 –

回答

1

猜測警告Derefencing可能的空指針:

holdItemNo = new String[countArray]; 

和下面幾行:要創建這些數組對象您的閱讀循環(在一個條件內)。

所以可能這種情況永遠不會變成現實;因此你的數組保持全空。但即使條件得到滿足 - 你可能預計會發生更多然後一次。並猜測:你正在創建完全陣列。而扔掉以前創建的數組。每當if條件變爲真時,您將失去先前存儲的值!

所以答案是:在進入循環之前創建您的陣列。這意味着您必須先查詢「要創建多少個插槽」;或者你必須創建一個有100個空插槽的數組;然後在你的循環中,你必須檢查你是否還有空閒插槽。

或者您開始​​使用java.util.List resp。 ArrayList - 它允許動態添加元素。

+0

感謝您的幫助!仍然試圖找到一種方法來消除剩餘的插槽後,我創建了其中幾個。 –

+1

循環完成後,找出有多少個插槽。創建較小尺寸的新陣列,然後複製數據插槽。 – GhostCat

+0

好吧,所以我決定在whileLoop'for(int i = 0; i