title: Why am I unable to add elements into a LinkedList in Java?
body: <p>This is part of a lab I couldn't figure out... I can't figure out what is wrong in the addGrade method in the roster class, where I have to add a grade to a student, and if the student doesn't already exist, create a new student and then add the grade. Note that initially, this class didn't have the instance variable Student stu, which I added when trying to get things to work.</p>
  
  <p>Student is provided with a constructor, student scores are saved in a linkedlist. I only put a part of the code here... it has methods to get student name, get score, add score, and get score average.</p>
  
  <p>My code is no longer working after some edits... when it was partially working, it just overwrote the previous students with the latest one. Student a was added with grade 5, then student b with 7, then student a is added again with 10. This should have student a with 2 entries (5, 10) in the linkedlist. When I ran my code it only had student a with 10, but also didn't work completely. </p>
  
  <pre><code>public class Student {
  private String name;
  private List scores = new LinkedList&lt;&gt;();
  
    public Student(String name)
    {
       this.name = name;
    }
  
    public void addGrade(int score)
    {
       scores.add(score);
    }
  
  
  public class Roster {
  
  String name;
  int score;
  Student stu;
  
     //Adds a grade to the end of a list of grades for the named student.
     //Should work even if no student with this name has ever been seen before.
     public void addGrade(String name, int score) {
         Student temp = new Student(name);
         stu.addGrade(score);
     }
      //Gets the specified grade from the named student's scores.
     public int getGrade(String name, int index) {
         int a = stu.getScore(index);
         return a;
     }
      //Gets the average for the named student.
     public double getAverage(String name) {
         return stu.getAverage();
     }
  }
  </code></pre>