Skip to content
Advertisement

Works fine in for loop but when do manually, there’s a NullPointerException error

In my program with the for loop, it works perfectly, without errors, but when I remove the for loop and manually increment it because I only want to input once and store in array, there’s a NullPointerException. It looks the same for me, the only difference is it doesn’t loop, but instead increment it manually. How can I fix it?

JavaScript

Advertisement

Answer

The issue is in this loop

JavaScript

As you have not completed the input for all the members, when you try to iterate this loop, it would work OK for the first time as there is only one member in array. But next time, as there is no member at i=1, it will give NullPointerException when it is passed to checkQualification method. Instead of for each loop, just pass member[i] to that function and after that increment value of i.

Advertisement