2013-09-22 26 views
0

我目前正在一個程序和任何時候我打電話產品[1]沒有空指針錯誤,但當我打電話產品[0]或產品[2]我得到一個空指針錯誤。但是,我仍然得到2個不同的輸出,就像陣列中有[0]和1或1和2一樣。這裏是我的代碼空指針,對我沒有意義嗎?

FileReader file = new FileReader(location); 
    BufferedReader reader = new BufferedReader(file); 

    int numberOfLines = readLines(); 
    String [] data = new String[numberOfLines]; 
    Products = new Product[numberOfLines]; 
    calc = new Calculator(); 

    int prod_count = 0; 
    for(int i = 0; i < numberOfLines; i++) 
    { 
     data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+"); 
     if(data[i].contains("input")) 
     { 
      continue; 
     } 
     Products[prod_count] = new Product(); 
     Products[prod_count].setName(data[1]); 
     System.out.println(Products[prod_count].getName()); 
     BigDecimal price = new BigDecimal(data[2]); 
     Products[prod_count].setPrice(price); 


     for(String dataSt : data) 
     { 

      if(dataSt.toLowerCase().contains("imported")) 
     { 
       Products[prod_count].setImported(true); 
     } 
      else{ 
       Products[prod_count].setImported(false); 
      } 

     } 



     calc.calculateTax(Products[prod_count]);  
     calc.calculateItemTotal(Products[prod_count]); 
     prod_count++; 

這是輸出:

imported box of chocolates 
1.50 
11.50 
imported bottle of perfume 
7.12 
54.62 

這版畫作品System.out.println(Products[1].getProductTotal());

這將成爲一個空指針System.out.println(Products[2].getProductTotal());

這也成爲一個空指針System.out.println(Products[0].getProductTotal());

+0

了什麼價值'readlines方法()'返回? –

+1

我強烈建議你學會使用調試器。 –

+0

readLines()返回一個int值,表示文件中有多少行 – user2786754

回答

3

您正在跳過包含「輸入」的行。

if(data[i].contains("input")) { 
    continue;   // Products[i] will be null 
} 

也許這將是更好地使products一個ArrayList,只有有意義的行添加到它。

products也應該以小寫字母開頭以遵循Java約定。類型以大寫字母開始,參數&變量以小寫字母開頭。並非所有的Java編碼規範都是完美的 - 但這個非常有用。

代碼的結構很好,但數組並不是一個非常靈活的類型來從程序邏輯構建(因爲長度必須預先確定,跳過要求您跟蹤索引,並且它不能跟蹤你建立它的大小)。

通常你應該建立List(ArrayList)。 Map(HashMap,LinkedHashMap,TreeMap)和Set(HashSet)也是有用的。


第二個錯誤:波希米亞說:在data[]你弄得所有行的列表的概念,並data[]正在分析從單線/分的標記。

「數據」通常是一個沒有意義的術語。使用有意義的術語/名稱&您的程序不太可能在其中存在錯誤。

您應該只使用tokens作爲行標記,而不是在需要之前在外部/之前聲明它,而不是嘗試按行索引它 - 因爲很簡單,應該絕對不需要。

for(int i = 0; i < numberOfLines; i++) { 
    // we shouldn't need data[] for all lines, and we weren't using it as such. 
    String line = reader.readLine(); 
    String[] tokens = line.split("(?<=\\d)\\s+|\\s+at\\s+"); 
    // 
    if (tokens[0].equals("input")) {  // unclear which you actually mean. 
    /* if (line.contains("input")) { */  
     continue; 
    } 

當您爲某個問題提供樣例輸入時,請將其編輯到問題的主體中,以使其可讀。把它放在評論中,不能正確閱讀的地方,只是在浪費試圖幫助你的人的時間。

+0

是的,它suppossed做到這一點,因爲輸入如下輸入2: 1進口盒巧克力在10.00 1瓶進口香水在47.50 – user2786754

+0

那麼如何做到這一點,即使2個輸出顯示跳過第一行使陣列中的0空間和陣列中的2個空間都爲空?並且陣列中的3個空間超出邊界? – user2786754

2

錯誤警告:您將要覆蓋data

String [] data = new String[numberOfLines]; 

然後在循環:

data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+"); 

因此,誰知道它是多麼大的 - 取決於分割的成功 - 但你的代碼依靠它是numberOfLines長。

+0

正確,這裏有重大錯誤。看起來'數據'應該用作單個行內的標記,但最初是在一個點聲明的(「跳過輸入」檢查)是通過行號索引的。它應該是單一行的「令牌」。 _ –

0

您需要爲行號和新產品對象使用不同的索引。如果你有20行,但其中5個是「輸入」,那麼你只有15個新產品對象。

例如:

int prod_count = 0; 

for (int i = 0; i < numberOfLines; i++) 
{ 
     data = reader.readLine().split("(?<=\\d)\\s+|\\s+at\\s+"); 
     if (data[i].contains("input")) 
     { 
      continue; 
     } 
     Products[prod_count] = new Product(); 
     Products[prod_count].setName(data[1]); 
     // etc. 
     prod_count++; // last thing to do 
} 
+0

jarmod我仍然在索引大於0的索引處得到空指針,但是您的解決方案確實將可用產品轉換爲索引0。 – user2786754

+0

您是否正在訪問產品[1](及更高版本)*在創建它們之前?如果是這樣,它們將爲空並導致nullpointerexception。在執行Products [prod_count] = new Product()*之前,您無法訪問Products [1],其中* prod_count爲1。 – jarmod