java.lang.NullPointerException error confusion
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: java.lang.NullPointerException error confusion

  1. #1
    Join Date
    Mar 2012
    Posts
    5

    java.lang.NullPointerException error confusion

    Hi all, first post here. I am in a college programming course and have hit a slight roadblock. Attached you will see StudentDriver, Student, and Course. StudentDriver was code that the professor wrote that we are not supposed to edit in any way. Student and Course were provided to us and they had many unimplemented methods that needed to be implemented. I can not figure out why I am getting this error:

    Exception in thread "main" java.lang.NullPointerException
    at Student.calculateGpa(Student.java:37)
    at Student.merge(Student.java:139)
    at StudentDriver.main(StudentDriver.java:18)

    Everything compiles fine. When I run the StudentDriver, it prompts me to "Please enter the file with the courses taken:", which works fine, and than "Please enter the file with the current courses.", which also seems to be work, but after I enter both files that error pops up.

    Thanks in advance.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: java.lang.NullPointerException error confusion

    I've had a quick look at your code and the answer is very simple. But rather than just telling you what it is, it will be better for you in the long run if you use the debugger to find the object which is causing the null pointer exception:
    Code:
    Exception in thread "main" java.lang.NullPointerException
    at Student.calculateGpa(Student.java:37)
    Look at line 37 of Student.java. Now search for this data member in your code, and you will see you are not initializing it anywhere.
    Then, decide where it should be initialized (is it in the Student constructor? Or later by calling a method?)

  3. #3
    Join Date
    Mar 2012
    Posts
    5

    Re: java.lang.NullPointerException error confusion

    I hate to ask, but do you have any more hints? I am drawing a blank.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: java.lang.NullPointerException error confusion

    I won't download code so haven't looked at it but Peter_B's description seems very clear to me.

    What code is on line 37 of Student.java?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2009
    Posts
    596

    Re: java.lang.NullPointerException error confusion

    I'll post the relevant bit of bigredaltoid's code here, with line 37 - the one causing the exception - highlighted in red:
    Code:
    public class Student {
    	
    	private String id;
    	private String firstName;
    	private String lastName;
    	private String major;
    	private String minor;
    	private ArrayList<Course> coursesTaken;
    	private ArrayList<Course> currentSemesterCourses;
    	private double gpa;
    	
    	/**
    		Course constructor
    	*/
    	public Student(String id, String firstName, String lastName, String major, String minor, ArrayList<Course> coursesTaken, ArrayList<Course> currentSemesterCourses) {
    	this.id = id;
    	this.firstName = firstName;
    	this.lastName = lastName;
    	this.major = major;
    	this.minor = minor;
    	this.gpa = gpa;
    	}
    /* This method calculates a student's gpa by iterating through the list of courses taken and obtaining the number of credits and the letter grades
    	for each course.  It uses the letterGradeConverter() method to give the numeric value for a given letter grade.  E.g., if the letter
    	grade is "A", it returns 4, etc.*/
    	public double calculateGpa() {
    		double studentGpa = 0;
    		int totalCredits = 0;
    		double GPA = 0;
    		for (int i=0; i < this.coursesTaken.size(); i++){
    			int myCredits = this.coursesTaken.get(i).getCredits();
    			String myGrade = this.coursesTaken.get(i).getLetterGrade();
    			double gradeNum = letterGradeConverter(myGrade);
    			double gpaPoints = myCredits+ gradeNum;
    			studentGpa =+ gpaPoints;
    			totalCredits =+ myCredits;
    		}
    		GPA = (studentGpa/totalCredits);
    		this.gpa = GPA;
    		return GPA;
    	}
    
    	// Plus lots of other irrelevant code...
    }
    @bigredaltoid: Look carefully at this line. What could be causing a NullPointerException on this line? Then find where this variable is initialized in your code (hint - it isn't, that's the problem )

  6. #6
    Join Date
    Mar 2012
    Posts
    5

    Re: java.lang.NullPointerException error confusion

    I am confused because it seems like everything on that line in initialized.. i is initialized in the for loop, and the coursesTaken variable is intialized on line 15. What else is missing that needs to be initialized?

  7. #7
    Join Date
    May 2009
    Location
    Surrey, UK
    Posts
    285

    Re: java.lang.NullPointerException error confusion

    Is line 15 this line?
    Code:
    private ArrayList<Course> coursesTaken;
    That line declares coursesTaken as being an ArrayList of Course, but you ar enot assigning any value (a reference to a new ArrayList, for instance), so it is null (i.e. not initialised).

  8. #8
    Join Date
    Mar 2012
    Posts
    5

    Re: java.lang.NullPointerException error confusion

    After three tutoring sessions and a visit to my Professor (whom had trouble finding the issue himself), it seems that after my files are being read into the program through the driver, that the variables have no value.

    He added this to the code:

    public void merge() {
    System.out.println(coursesTaken == null); //This line
    System.out.println(currentSemesterCourses == null); //This line
    this.coursesTaken.addAll(this.currentSemesterCourses);
    calculateGpa();
    this.currentSemesterCourses.clear();


    which both returned true..which means my variables are empty for whatever reason. The text files are in the same file as my programs...so I am very confused. He did not mention that I needed to initialize any variables, as the fact that the Array<Course> cousesTaken is private does not affect it, and i is initialized in the for loop.

  9. #9
    Join Date
    Jan 2009
    Posts
    596

    Re: java.lang.NullPointerException error confusion

    Quote Originally Posted by bigredaltoid View Post
    After three tutoring sessions and a visit to my Professor (whom had trouble finding the issue himself), it seems that after my files are being read into the program through the driver, that the variables have no value.

    He added this to the code:
    ...

    which both returned true..which means my variables are empty for whatever reason. The text files are in the same file as my programs...so I am very confused. He did not mention that I needed to initialize any variables, as the fact that the Array<Course> cousesTaken is private does not affect it, and i is initialized in the for loop.
    Not meaning to be rude to your Professor, but if he had trouble finding this he should get a new job

    This is how I found it - it took about 2 minutes.
    1) Look at line 37 of Student.java, which is where the exception is being thrown (highlighted in red):
    Code:
    public double calculateGpa() {
    	double studentGpa = 0;
    	int totalCredits = 0;
    	double GPA = 0;
    	for (int i=0; i < this.coursesTaken.size(); i++){
    		int myCredits = this.coursesTaken.get(i).getCredits();
    		String myGrade = this.coursesTaken.get(i).getLetterGrade();
    		double gradeNum = letterGradeConverter(myGrade);
    		double gpaPoints = myCredits+ gradeNum;
    		studentGpa =+ gpaPoints;
    		totalCredits =+ myCredits;
    	}
    	GPA = (studentGpa/totalCredits);
    	this.gpa = GPA;
    	return GPA;
    }
    This means the only possibility could be that 'courseTaken' is null.

    2) Look for all occurrences of coursesTaken in Student.java to see where it is being initialized. Apart from statements which use coursesTaken, we find these lines:
    Firstly, the declaration which doesn't give it a value
    Code:
    private ArrayList<Course> coursesTaken;
    Then the constructor, which is taking in a parameter with the same name but not using it!. (It's not using currentSemesterCourses either )
    Code:
    public Student(String id, String firstName, String lastName, 
    	String major, String minor, 
    	ArrayList<Course> coursesTaken, 
    	ArrayList<Course> currentSemesterCourses) {
    	this.id = id;
    	this.firstName = firstName;
    	this.lastName = lastName;
    	this.major = major;
    	this.minor = minor;
    	this.gpa = gpa;
    	}
    And there is the problem.

    P.S. : There is also a method to set this data member:
    Code:
    public void setCoursesTaken(ArrayList<Course> coursesTaken) {
    	this.coursesTaken = coursesTaken;
    }
    but this is never called either.

  10. #10
    Join Date
    Mar 2012
    Posts
    5

    Re: java.lang.NullPointerException error confusion

    You know..it's almost sad how easy of a fix this was. I have no idea why this was so difficult for tutors and a Professor to find out.

    Thank you very much for the help. It's all good to go now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center