2014-10-09 36 views
0

下面找出NullPointerException異常錯誤是錯誤:也不會在Java

Exception in thread "main" java.lang.NullPointerException 
at CellularData.addCountry(CellularData.java:34) 
at CellularData.addCountry(CellularData.java:24) 
at TestCSVReader.main(TestCSVReader.java:35) 

我不斷收到上述錯誤,但似乎有麻煩修復它。我正在讀取一個csv文件並在屏幕上顯示數據。基本上它讀取的是國家,這一年,它的蜂窩數據統計數據是雙倍數據。下面是CellularData類:

public class CellularData { 

private Object [][]array; 
private int year; 

public CellularData(int rows, int columns, int year) 
{ 
    array = new Object[rows+1][columns+1]; //add +1 because initializes the header. 
    this.year = year; 
    for(int i=1;i<=columns;i++) 
    { 
    array[0][i] = year++; //increments the year 
    } 
} 

public void addCountry(String country, double []num) 
{ 
    for(int i=0;i<array.length;i++) 
    {  
    if(array[i][0] == null)  //checks if the first row is empty 
    { 
     //CellularData.addCountry(CellularData.java:24) 
     addCountry(country, num, i); //inserts the data 
     break; 
    } 
    } 
} 
private void addCountry(String country, double []num, int row) 
{ 
    array[row][0] = country; 
    for(int j = 1;j<array[row].length;j++) 
    { 
     array[row][j] = num[j-1]; //CellularData.addCountry(CellularData.java:34) 
    } 
} 
public double getNumSubscriptionsInCountryForPeriod(String country, int sYear, int eYear) 
{ 
    double sum = 0; 

    for (int i = 1; i < array.length; i++) { 

      if (country.equalsIgnoreCase((String) array[i][0])) { //matches with the parameters passed ignoring CAPS 
       int start = 1 + sYear - year; //first index position of the year 
       int end = start + (eYear - sYear); //end position of the year 

       if (start >= 0 && end < array[i].length) { //checks to see if index position is out of bounds 

        for (int k = start; k <= end; k++) { 
         // System.out.println(">> " + country + " adding " + array[i][k]); 
         sum += (Double) array[i][k]; //sum of the stats 
        } 
       } 
       else { 
        //returns Error messgae and -1 
        System.out.println("ERROR : requested year "+sYear+" from "+ country+" is less than starting year "+this.year); 
        sum = -1; 
        } 
      } 
      } 
    return sum; 
    } 

public String toString() 
{ //prints the array. 
    for(Object []a: array) 
    { 
     for(Object k:a) 
     { 
      System.out.print(k + "\t"); 
     } 
     System.out.println(); 
    } 
    return " "; 
} 
    } 

這裏是我的CSV閱讀文件,而這其中,我從文件中讀取:

public class CSVReader { 
String[] countryNames; 
int[] yearLabels; 
double[][] cellularDatatables; 
Scanner scan; 

public CSVReader(String filename)// throws FileNotFoundException 
{ 
    try{ 
    File file = new File(filename); 
    scan = new Scanner(file); 
    scan.nextLine(); 
    String numLine = scan.nextLine(); 
    final int n = Integer.parseInt(numLine.split(",")[1]); //Number is the string portion after the first comma 

    //Allocate arrays with length n 
    countryNames = new String[n]; 
    cellularDatatables = new double[n][]; 

    //Read in the header line of years, parse and copy into yearNum 
    String[] yearHeaders = scan.nextLine().split(","); 
    final int m = yearHeaders.length-1; 
    yearLabels = new int[m]; 
    for(int i=0;i<m;i++) 
    { 
    yearLabels[i] = Integer.parseInt(yearHeaders[i+1]); //i+1 to skip the first entry in the string arr 
    } 

    //Now read until we run out of lines - put the first in country names and the rest in the table 
    int c = 0; 
    while(scan.hasNext()) 
    { 
    String[] inputArr = scan.nextLine().split(","); 
    countryNames[c] = inputArr[0]; 
    cellularDatatables[c] = new double[m]; 
    for(int i = 0; i < m; i++) 
    { 
     cellularDatatables[c][i] = Double.parseDouble(inputArr[i+1]); 
     } 
    } 
     scan.close(); 
    } 

    catch(FileNotFoundException e) 
    { 
     System.out.println(e.getMessage()); 
    } 
} 
    public String[] getCountryNames(){ 
     return countryNames; 
    } 
    public int[] getYearLabels(){ 
     return yearLabels; 
    } 
    public double[][] getParsedTable(){ 
     return cellularDatatables; 
    } 
    public int getNumberOfYears() 
    { 
     return yearLabels.length; 
    } 
} 

而且還TestCSVReader文件:

public static void main(String[] args) 
{ 
    final String FILENAME = "data/cellular.csv"; 

    CSVReader parser = new CSVReader(FILENAME); 

    String [] countryNames = parser.getCountryNames(); 
    //System.out.println(countryNames.length); 
    int [] yearLabels = parser.getYearLabels(); 
    //System.out.print(yearLabels.length); 
    double [][] parsedTable = parser.getParsedTable(); 


    CellularData datatable; 
    int numRows = parsedTable.length; 
    int numColumns =parser.getNumberOfYears(); 
    int startingYear = yearLabels[0]; 
    datatable = new CellularData(numRows, numColumns, startingYear); 


    for (int countryIndex = 0; countryIndex < countryNames.length; countryIndex++) 
    { 
     double [] countryData = parsedTable[countryIndex]; 
     datatable.addCountry(countryNames[countryIndex], countryData);//Error TestCSVReader.java:35     
    } 


    System.out.printf(countryNames[0] + " (1960 to 2012): %.2f \n", datatable.getNumSubscriptionsInCountryForPeriod(countryNames[0],1960,2012)); 
    // the output is: Aruba (1960 to 2012): 1170.50 
+0

這段代碼毫無意義。爲什麼要創建一個Object類型的二維數組,然後在其中填入整數年份值? – duffymo 2014-10-09 19:56:51

+0

很可能,您的錯誤來自'double [] CountryData'與CellurarData數組的大小不一樣 - 1.運行調試器,在'CellurarData'的第34行放置一箇中斷點,看看會發生什麼在那個循環中。兩個陣列的大小是否相差超過1? – nem035 2014-10-09 20:00:36

回答

0
array[row][0] = country; 
for(int j = 1;j<array[row].length;j++) 
{ 
    array[row][j] = num[j-1]; //CellularData.addCountry(CellularData.java:34) 
} 

由於第一行不會給出NullPointerException,我們知道array不爲空,並且array[row]不是 空值。因此,num必須爲空。

+0

但num與double [] countryData相同 – user3497437 2014-10-09 20:18:40