Hello, I'm trying to figure out this method were i add courses to a student. and then call it in the main method .

public ArrayList getCourseList(){

    return courseList;
  }


public boolean addCourse(EnrolledCourse courseName){
    for (int i = 0; i <= courseList.size(); ++i )
        if(courseList.contains(null)){
             courseName.getCoursename();
             this.courseList.add(courseName);
             return true;
           
        }
        return false;
    }

then I call it from the main method like student.addcourse(courseName);
but when i run it keeps saying
Exception in thread "main" java.lang.NullPointerException
at cis35a.project.Student.addCourse(class.java:112)

Im also trying to figure out how to drop courses, but am stuck with adding courses.

The problem is in line 8, but it's triggered in line 10. For an ArrayList of size n, what is the range of valid indexes?

I just listed the courselist as an array, didnt really think about specifying on how many courses there are, so basically my index just goes through the array of courses. I was thinking of just redoing that part. Sorry if i can't reply to your question properly, but heres my entire code:

package cis35a.project4JoshBiso;

import java.util.ArrayList;
import java.util.Date;

class Student {
     
     private String studentName;
     private long studentID;
     private String address;
     private String phone;
     private boolean fulltimeStatus;
     private float gpa;
     private ArrayList courseList;


public Student(String studentName, long studentID){
      this.studentName=studentName;
      this.studentID=studentID;
}
public Student(String studentName, long studentID, String address, String phone, boolean fulltimeStatus, float gpa){
      this.studentName = studentName;
      this.studentID= studentID;
      this.address=address;
      this.phone=phone;
      this.fulltimeStatus= fulltimeStatus;
      this.gpa=gpa;
}




public void setStudentName(String studentName){
      this.studentName=studentName;

}
 public void setStudentID(long studentID){
      this.studentID=studentID;
}
 public void setAddress(String address){
      this.address=address;
 }
 public void setPhone(String phone){
      this.phone=phone;}

 public boolean setfulltimeStatus(boolean fulltimeStatus){
 if(courseList.size()>=3){
    this.fulltimeStatus=fulltimeStatus;
    System.out.print("Full-time Student");
    return true;
 }
 else {
    this.fulltimeStatus=fulltimeStatus;
    System.out.print("Part-time Student");
    return false;
    }

 }
 public void setGpa(float gpa){
 for(int i=1;i<=courseList.size();i++){
 
 }


 }
 public void setCourseList(EnrolledCourse className){
     
     this.courseList.add(className);}

public String getStudentName(){
    return studentName;
  }
public boolean getFulltimeStatus(){
    return fulltimeStatus;
  }
public long getID(){
    return studentID;
  }

public String getAddress(){
    return address;
  }

public String getPhone(){
    return phone;
  }

public boolean getStatus(){

    return true;
  }
public float getGPA(){
    return gpa;
  }

public ArrayList getCourseList(){

    return courseList;
  }


public boolean addCourse(EnrolledCourse courseName){
    for (int i = 0; i <= courseList.size(); ++i )
       if (courseList.contains(courseName)){
           courseList.add(courseName);
           return true;

        }
         return false;
       }
public boolean dropCourse(int callnumber){
    for (int i =0; i<courseList.size(); ++i){
        if(courseList.contains(callnumber)){
        courseList.remove(callnumber);
        return true;
        }
    }
    return false;
  }

public boolean updateGrade(EnrolledCourse courseName, char newGrade){
//    courseName.setGrade(newGrade);
    return true;
    }


    
    
//public char getCourseGrade(String courseName){
// return courseName.charAt(grade);

//}



public String hasEnrolled(String courseName){

   if(courseList.contains(courseName)){
       return courseName;
   }
   return null;

}  

}





 class EnrolledCourse{
     private String courseName;
     public int callNumber;
     private String instructor;
     private char grade;
     private String quarterTaken;
     private Date gradeRecordedON;

  
     EnrolledCourse(String courseName, int callNumber, String instructor, String quarterTaken){
        this.courseName=courseName;
        this.callNumber=callNumber;
        this.instructor=instructor;
        this.quarterTaken=quarterTaken;
     }
    EnrolledCourse(String courseName, int callNumber, String instructor, char grade,  String quarterTaken, Date gradeRecordedOn){
        this.courseName=courseName;
        this.callNumber=callNumber;
        this.instructor=instructor;
        this.grade=grade;
        this.quarterTaken=quarterTaken;
    }


public String toString(){
return String.format( "%s: %s %s\n%s: %s %s\n%s: %s %s\n%s: ",
        "Course Name", courseName,
         "Course Number", callNumber,
         "Instructor", instructor

        );
}
public void setCourseName(String courseName){
    this.courseName=courseName;
   }
public void setCallNumber(int callNumber){
    this.callNumber=callNumber;
   }
public void setInstructor(int callNumber,String instructor){
    this.instructor=instructor;
   }
public void setGrade(int callNumber ,char grade){
    this.grade=grade;
   }
public void setQuarterTaken(int callNumber, String quarterTaken){
    this.quarterTaken=quarterTaken;
   }
public void setGradeRecordedON(Date gradeRecordedOn){
    this.gradeRecordedON=gradeRecordedOn;
   }

public String getCoursename(){
    return courseName;
   }

public int getCallNumber(){
    return callNumber;
   }
public String getInstructor(){
    return instructor;
}
public char getGrade(){
return grade;
    }
public String getQuarterTaken(){
    return quarterTaken;
     }
public Date getGradeRecordedOn(){
    return gradeRecordedON;
      }
 }




class classJoshBiso{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
           java.util.Random gen = new java.util.Random();
          ArrayList<Student> student = new ArrayList<Student>();
          final String[] quarters = {"fall", "winter", "spring", "summer"};
          final String[] courseNames = {"math", "physics", "history", "CS"};
          final String[] grades = {"A","B","C","D","F","W"};
        
        
         EnrolledCourse math = new EnrolledCourse("Math63: Differential Equations", 2150, "Alfred Einstein","fall 2010");
         EnrolledCourse physics = new EnrolledCourse("Physics17B: Quantum Physics", 2250, "Phil Jackson", "Winter 2010");
         EnrolledCourse history = new EnrolledCourse("HIS4N: Norse Mythology", 2100, "Odin", "Winter 2010");
         EnrolledCourse compscience = new EnrolledCourse("CS1337: StarcraftII", 1337, "Jim Raynor", "summer 2010");

           //student1
           Student student1 = new Student("Uzumaki Naruto", 31192556);
           student1.setAddress("9123 Konoha Ct, Fire Country");
           student1.setPhone("408-238-5836");
          
           System.out.println("Student Name:_" + student1.getStudentName() + "\n ID_:" + student1.getID() +
           "\n address:_" + student1.getAddress() + "\n phone:_" + student1.getPhone() + "\n full-time:_" +
                   student1.getFulltimeStatus());
         

        
          

       
              

           //student2
           Student student2 = new Student ("Manny Pacquiao", 12345678);
           student2.setAddress("7timeChampion St, General Santos City");
           student2.setPhone ("408-386-8239");
           System.out.println("Student Name:_" + student2.getStudentName() + "\n ID_:" + student2.getID() +
           "\n address:_" + student2.getAddress() + "\n phone:_" + student2.getPhone() + "\n full-time:_" + student2.getFulltimeStatus());


           

       
             //student3
           Student student3 = new Student ("Thrall Durotan", 23468976);
           student3.setAddress("Valley of Honor, Orgrimmar City");
           student3.setPhone("Mok-nath-tar");

           System.out.println("Student Name:_" + student3.getStudentName() + "\n ID_:" + student3.getID() +
           "\n address:_" + student3.getAddress() + "\n phone:_" + student3.getPhone() + "\n full-time:_" + student1.getFulltimeStatus());
           student3.addCourse(compscience);
           
          



          




        
      
         
          
         


         
          
          

       }


    }

Don't need the entire code - the error is right there. Try it this way: You create a new ArrayList, empty. You add an item to it. What's the item's index in the ArrayList? Add four more items. What is the value of arraylist.length()? What index numbers do the items have?
What happens if you try to do this:

arraylist.get(arraylist.length());

Do i add arraylist on my for loop? replace courseList.size()?

The answer will become very obvious if you think about the questions above. This is your part of the "show a little effort" game.

But yes, I guess you could replace (or rather, modify) courseList.size(), though there's a simpler solution - what would you replace it with?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.