2014-02-24 63 views
-1

希望我能得到一些幫助。我試圖用通用arrayList實例化一個通用對象Student作爲成績列表。我無法讓我的Add方法正常工作。我不斷收到一個空指針異常 - 這是顯而易見的,因爲它被初始化爲null - 只是不知道我的add方法有什麼問題。任何幫助將不勝感激。此外,這項任務的要求非常嚴格 - 我必須這樣做。謝謝。通用類與通用數組列表

我有兩類:第一,學生:

import java.util.ArrayList; 

public class Student <S>{ 

    private String name; 
    private ArrayList<S> grades; 



    private double average; 


    // the constructor 
    public Student (String studentName, ArrayList<S> studentGrades){ 
     name = studentName; 
     grades = studentGrades; 
    } 

    // get the name 
    public String getName() { 
     return name; 
    } 

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

    // add a grade to the array 
    public void addGrade(S n){ 

     grades.add(n); 

    } 

    // return a grade from the array. This will be used for the calculation. 
    public S getGrade(int n){ 

     return grades.get(n); 


    } 

    // compute the average grade for each student 
    public double computeAverage(){ 

     Double sum = 0.00; 
     for(S grade : grades){ 

      if (grade instanceof Double){ 
      sum += (Double)grade; 
      } 
      if (grade instanceof Integer){ 
       sum += (Integer)grade; 
      } 
     } 
     average = sum.doubleValue()/ grades.size(); 
     return average; 

    } 

    public String toString() 
    { 
     return name + "'s average grade is " + average; 
    } 
} 

;第二,測試:

public class Test { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     // create the studentList array 
     ArrayList<Student> studentList = new ArrayList<Student>(); 
     HashMap<String, Student> studentMap = new HashMap<String, Student>(); 

     // the values for math.random to create the grades 
     int min = 0; 
     int max = 100; 
     double dmin = 0.00; 
     double dmax = 100.00; 

     // initialize the variables 

     ArrayList grades = null; 

     //------------------------------------ 
     // Create the students (the method signature is wrong. It blows up regardless of what I try to add.) 
     Student<Integer> Fred = new Student<Integer>("Fred", grades); 
     //null pointer exception. Also points to the add method in the student class. 

     //------------------------------------- 

     Student<Integer> Wilma = new Student<Integer>("Wilma", grades); 
     Student<Double> Barney = new Student<Double>("Barney", grades); 
     Student<Double> Betty = new Student<Double>("Betty", grades); 

     // add the random grades 
     for(int i = 0; i < 5; i++){ 
      Fred.addGrade((int) (Math.random() * (max - min) + min)); 
     } 

     for(int i = 0; i < 5; i++){ 
      Wilma.addGrade((int) (Math.random() * (max - min) + min)); 
     } 

     for(int i = 0; i < 5; i++){ 
      Barney.addGrade((double) (Math.random() * (dmax - dmin) + dmin)); 
     } 

     for(int i = 0; i < 5; i++){ 
      Betty.addGrade((double) (Math.random() * (dmax - dmin) + dmin)); 
     } 
     // add the students to the array list 
     studentList.add(Fred); 
     studentList.add(Wilma); 
     studentList.add(Barney); 
     studentList.add(Betty); 

     studentMap.put(Fred.getName(), Fred); 
     studentMap.put(Wilma.getName(), Wilma); 
     studentMap.put(Barney.getName(), Barney); 
     studentMap.put(Betty.getName(), Betty); 


     //iterate through the list & print to the console 
     Iterator<Student> itr = studentList.iterator();{ 
      while(itr.hasNext()){ 
       System.out.println(itr.next().<Student>toString()); 

       //Initialize the array to hold the key variables 
       Set<String> mapKeys = studentMap.keySet(); 
       //mapKeys.add(something); 

      } 

     } 

    } 
} 
+0

您需要創建一個新的ArrayList。 – SLaks

+0

爲什麼你根本就沒有通用的'學生'類?我沒有看到任何需要。 –

+0

這是分配所必需的。 –

回答

1

要初始化的測試成績爲空

ArrayList grades = null; 

,當您嘗試添加一個檔次與

Fred.addGrade(...); 

在學生的內部代碼做

grades.add(n); 

但成績初始化爲空創建空例外。

用適當的類型初始化等級。

例子:

Student<Integer> Wilma = new Student<Integer>("Wilma", new ArrayList<Integer>()); 
Student<Double> Barney = new Student<Double>("Barney", new ArrayList<Double>()); 
Student<Double> Betty = new Student<Double>("Betty", new ArrayList<Double>()); 
1

在您的測試類,你有 ArrayList grades = null; - 你需要創建等級ArrayList的並在使用前填入數據。

0

你可以做這樣的事情:

public class Student<S> 
{ 
    // declare array 
    private final ArrayList<S> grades = new ArrayList<S>(); 
    private String name; 

    // One arg constructor 
    public Student(String name) 
    { 
     this.name = name; 
    } 

    // Two arg constructor 
    public Student(String name, ArrayList<S> grades) 
    { 
     this(name); 
     this.grades.addAll(grades); 
    } 

    // etc 
} 
2

需要初始化ArrayList的成績。目前您正在傳遞一個空值。不要在該行的下面:

ArrayList grades = new ArrayList(); 

一個很好的做法是使用泛型在初始化的ArrayList但因爲你的成績似乎是不同類型的,我跳了出來。

0

編輯代碼以下列方式在測試類:

希望,這將有助於。