0

So im writing a Java programming, I have 2 classes, and a class test that uses these 2 classes. running eclipseIDE, it keeps telling me i have a NullPointerException at the "s+=c.getName() + " " +... "

this is a method in Student.

public String getCourses()
    {
        String s = "";
        for(Course c: this.courses)
        {
            s+=c.getName() + " " + c.getID() + " " + c.getScore(this.id);
            s+="\n";
        }
        return s;
    }

it calls this method that is in the Course class.

public String getID()
{
    return this.id;
}

i tried only doing getName(); it had no issue, however once i added getID() it became an issue. getName is the same type of code, it returns the "name" of the object as a string.

name and id is "initialized" via a constructor

Stacktrace:
Exception in thread "main" java.lang.NullPointerException
    at hw6.Student.getCourses(Student.java:47)
    at hw6.CourseStudent_test.main(CourseStudent_test.java:100)

this is the getScore method

public double getScore(String id)
    {
        int i = 0;
        for(; i < this.students.length;i++)
        {
            if(this.students[i].getID() == id )
            {
                break;
            }
        }
        return this.scores[i];
    }
Gary
  • 57
  • 7

2 Answers2

2

If the exception is occurring where your stacktrace + question says it is, then one of the elements in courses must be null.

Reasoning:

  • If the exception was thrown inside one of the Course method calls, then the stacktrace would show that method as the top stack frame.

  • If one of those 3 calls returned null, you wouldn't get an NPE. You would just get "null" in the resulting concatenation; see String concatenation with Null.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • yes, all the get...() returns a String value, – Gary Oct 26 '20 at 02:21
  • That confirms my diagnosis. Check what is in `courses`, and how a `null` has gotten into there. That will be where the problem is. – Stephen C Oct 26 '20 at 02:23
  • 1
    The two first points are sufficient to conclude. – Thomas Oct 26 '20 at 02:25
  • @StephenC how would i go about checking how a null gotten into the courses object array? – Gary Oct 26 '20 at 02:28
  • 1
    By looking at the code that initializes and assigns into the array! I assume you are aware of the fact that the elements of the array will be default initialized to `null`. – Stephen C Oct 26 '20 at 02:29
  • Most bugs can be found by carefully reading your code and thinking / reasoning about what it does. – Stephen C Oct 26 '20 at 02:32
  • so, i did a little debugging using the println statement. it is printing out all 3 returned values with no issue, and non are null. – Gary Oct 26 '20 at 02:37
  • @Gary Are you now saying that there is **no** NPE? – Scary Wombat Oct 26 '20 at 02:39
  • It is not the returned values that it the problem. It is the `null` values in the `this.courses` array. If my diagnosis is >>incorrect<< then there is something else going on, and we actually really do need to see a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Stephen C Oct 26 '20 at 02:41
  • no, it still giving my NPE, but neither of those 3 methods, returned a "null" @ScaryWombat – Gary Oct 26 '20 at 02:41
  • Well ... yea ... if you look for the broom in the fridge you won't find it :-) I already explained why the NPE is not caused by those 3 methods returning `null`. The broom won't fit in the fridge. Try looking in the broom cupboard ... – Stephen C Oct 26 '20 at 02:42
  • @gary I suggest that maybe your initial diagnosis of where the NPE is happening is incorrect. – Scary Wombat Oct 26 '20 at 02:43
  • so there are null values within the this.courses array. I do have an Addcourses method, and i think thats the issue. – Gary Oct 26 '20 at 02:45
  • 1
    I have to leave this now. Good luck. The trick to solving this is to think logically ... – Stephen C Oct 26 '20 at 02:47
0

Found the issue, Because of the "For each " loop, when it gets to an Null object, it tries to run the getter methods, however each time it returns null, as there is no "value" for each variable within that object, so i just added a if condition to check if the object is "null"

Gary
  • 57
  • 7