2013-10-14 97 views
-1

我有一個名爲sectionArray的2D數組,它是Student類的類型。每個學生都有一個名稱和一個數組[],考試成績爲5個等級。這些名稱和分數被分成一個文件中必須閱讀的部分。無論我改變什麼,我都會在我的sectionArray上得到一個nullPointer。謝謝你的任何建議。二維數組對象上的NullPointerException

import java.util.*; 
import java.io.*; 

public class ProgressReport { 

    private Student[][] sectionArray;// each student has a name, 
            // grade, average, 
            // and the array of scores 
    private File file; 
    private Scanner inputFile; 

    public ProgressReport() throws IOException { 
     sectionArray = new Student[2][]; 
     file = new File("Lab5A.in.txt"); 
     inputFile = new Scanner(file); 
    } 

    /** 
    * 
    * @return the values in the sectionArray 
    */ 
    public Student[][] getSectionArray() { 
     return sectionArray; 
    } 

    /** 
    * initialize sectionArray and set the values 
    * 
    * @param sectionArray 
    *   passed 2D array of Students 
    */ 
    public void setSectionArray(Student[][] sectionArray) { 
     for (int i = 0; i < sectionArray.length; i++) { 
      for (int j = 0; j < sectionArray[i].length; j++) { 
       this.sectionArray[i][j] = sectionArray[i][j]; 
      } 
     } 
    } 

    /** 
    * reads from the file and creates new Students 
    * 
    * @throws IOException 
    */ 
    public void readInputFile() throws IOException { 
     int colNum = 0; 
     int section = 0; 
     String name = " "; 
     int[] grades = new int[5]; 

     while (inputFile.hasNext()) { 
      colNum = inputFile.nextInt();// gets size of row 
      sectionArray[section] = new Student[colNum];// initialize array 

      // iterates through colNum amount of times 
      for (int j = 0; j < colNum; j++) { 
       name = inputFile.next();// gets next name in column 
       for (int i = 0; i < 5; i++) { 
        // stores scores for that name 
        grades[i] = inputFile.nextInt(); 
       } 
       // creates new Student with name and grades 
       sectionArray[section][j] = new Student(name, grades); 
       section++; 
      } 
     } 

     // passes the values in sectionArray 
     setSectionArray(sectionArray); 
    } 
} 

我的學生類看起來是這樣的:

public class Student { 

    private String name = " "; // Store the name of the student 
    private char grade; // Store the letter grade 
    private double average; // Store the average score 
    private int[] scores; // Store the five exam scores 


    public Student() { 
     grade = ' '; 
     average = 0.0; 
    } 

    public Student(String name, int[] score) { 
     this.name = name; 
     scores = new int[5]; 
     for (int i = 0; i < scores.length; i++) { 
      scores[i] = 0; 
     } 
     for (int i = 0; i < scores.length; i++) { 
      this.scores[i] = score[i]; 
     } 
    } 

    // getters 

    public String getName() { 
     return name; 
    } 

    public char getGrade() { 
     return grade; 
    } 

    public double getAverage() { 
     return average; 
    } 

    // think about changing this to return a different format 
    public int[] getScores() { 
     return scores; 
    } 

    // setters 
    public void setScore(int[] scores) { 

    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setGrade(char grade) { 
     this.grade = grade; 
    } 

    public void setAverage(double average) { 
     this.average = average; 
    } 

    /** 
    * determine the average of the five test scores for each student 
    */ 
    public void calculateAverage() { 
     double total = 0; 
     double average = 0; 
     for (int i = 0; i < scores.length; i++) { 
      total += scores[i]; 
     } 
     average = total/scores.length; 
     setAverage(average); 
    } 

    /** 
    * Determine the student's letter grade based on average of test scores 
    */ 
    public void calculateGrade() { 
     double average = 0; 
     average = getAverage(); 
     if (average <= 100 && average >= 90) { 
      setGrade('A'); 
     } else if (average <= 89 && average >= 80) { 
      setGrade('B'); 
     } else if (average <= 79 && average >= 70) { 
      setGrade('C'); 
     } else if (average <= 69 && average >= 60) { 
      setGrade('D'); 
     } else if (average <= 59 && average >= 0) { 
      setGrade('F'); 
     } 

    } 

    public String toString() { 
     return getName() + " " + getAverage() + " " + getGrade(); 
    } 
} 
+3

什麼行?哪裏? – Maroun

+1

我們應該猜測哪一行出現異常,或者想要更簡單的路由,並粘貼**完整的異常**的堆棧跟蹤? – ppeterka

+0

「無論我改變什麼」......那麼問題就無法解決。但有了更具體的信息,也許我們可以提供幫助。 – jltrem

回答

2

你試圖寫該陣列與sectionArray = new Student[2][];初始化。這將創建一個包含2列和只有一行的矩陣(二維數組),然後嘗試在該數組上設置新值。如果您已經知道要從文件讀取的矩陣的大小,請使用正確的值對其進行初始化。

無論如何,我沒有得到你爲什麼要嘗試使用二維數組。如果我正確理解了代碼的用途,則應該使用List來代替讀取數據,並且由於它的大小動態增加,因此不必像控制數組那樣控制索引。看看this tutorial瞭解如何使用列表。