title: Generic class with generic arraylist
body: <p>Hoping I can get some help on this. I'm trying to instantiate a generic object Student with a generic arrayList as a list of grades. I can't get my Add method to work properly. I keep getting a null pointer exception- which is obvious as it is initialized to null- just not sure what is wrong with my add method. Any help would be greatly appreciated. Also, the requirements of this assignment are very strict- I have to do it this way. Thanks.</p>
  
  <p>I have two classes: the first, Student:</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class Student &lt;S&gt;{
  
      private String name;
      private ArrayList&lt;S&gt; grades;
  
  
  
      private double average;
  
  
      // the constructor
      public Student (String studentName, ArrayList&lt;S&gt; 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;
      }
  }
  </code></pre>
  
  <p>And the second, test:</p>
  
  <pre><code>public class Test {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          // create the studentList array
          ArrayList&lt;Student&gt; studentList = new ArrayList&lt;Student&gt;();
          HashMap&lt;String, Student&gt; studentMap = new HashMap&lt;String, Student&gt;();
  
          // 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&lt;Integer&gt; Fred = new Student&lt;Integer&gt;("Fred", grades);   
          //null pointer exception. Also points to the add method in the student class.
  
          //-------------------------------------
  
          Student&lt;Integer&gt; Wilma = new Student&lt;Integer&gt;("Wilma", grades);
          Student&lt;Double&gt; Barney = new Student&lt;Double&gt;("Barney", grades);
          Student&lt;Double&gt; Betty = new Student&lt;Double&gt;("Betty", grades);   
  
          // add the random grades 
          for(int i = 0; i &lt; 5; i++){
              Fred.addGrade((int) (Math.random() * (max - min) + min));
          }
  
          for(int i = 0; i &lt; 5; i++){
              Wilma.addGrade((int) (Math.random() * (max - min) + min));
          }
  
          for(int i = 0; i &lt; 5; i++){
              Barney.addGrade((double) (Math.random() * (dmax - dmin) + dmin));
          }
  
          for(int i = 0; i &lt; 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 &amp; print to the console
          Iterator&lt;Student&gt; itr = studentList.iterator();{
              while(itr.hasNext()){
                  System.out.println(itr.next().&lt;Student&gt;toString());
  
                  //Initialize the array to hold the key variables
                  Set&lt;String&gt; mapKeys = studentMap.keySet();
                  //mapKeys.add(something);
  
              }
  
          }
  
      }
  }
  </code></pre>