0

Im new to software testing, i keep getting java.lang.NullPointerException for some reason and i don't know what it is

This is what i am testing on

public int getNumberOfStudents()
    {
        int validStudents = 0;
        int i = 0;
        boolean done = false;

        do {
            if (students[i] != null && students[i].exists())
                validStudents++;
            else
                done = true;
            i++;
        } while(i < students.length && !done);

        return validStudents;
    } // end of getNumberOfStudents



This is the test case



class Testing {

    RegistrationSystem GNS = new RegistrationSystem();

    @Test
    //@SpiraTestCase(testCaseId=5487)
    public void test() {
        assertEquals("validStudents", GNS.getNumberOfStudents());
    }
    }
  • Where is the NPE thrown from and how is students initialized? This question will probably be closed nonetheless – Stephan Hogenboom Aug 28 '19 at 09:25
  • Where is the NPE happening? – Julius Hörger Aug 28 '19 at 09:25
  • Your `assertEquals` statement is almost certainly wrong. You're asserting that the string literal `"validStudents"` is equal to the `int` that is returned from calling `getNumberOfStudents` - this comparison will always fail – JonK Aug 28 '19 at 09:28

2 Answers2

0

I guess at line if (students[i] != null && students[i].exists()) you have not initialized students while creating RegistrationSystem Object, create it and it will work also I can see a problem in assert that you are comparing string with int :)assertEquals("validStudents", GNS.getNumberOfStudents());

Prashant Sharma
  • 375
  • 3
  • 5
0

Where exactly are you getting error. One problem with your test case is you are comparing string "validstudents" with integer returned from function GNS.getNumberOfStudents(). You need to set a number of students, then call getNumberOfStudents() assert it with the expected value.

Pradeep
  • 261
  • 2
  • 7