Your bug is subtle.
for (int i = 0; i < arr[i].length(); i++) {
is apparently supposed to iterate over the strings in arr
. But it won't necessarily stop at the end ... because the stop condition for the for
loop is incorrect.
The expression arr[i].length()
gives you the number of characters in the i'th string, not the number of strings in an array. You need this:
for (int i = 0; i < arr.length; i++) {
There could be a second problem as well. The above bug will give you an ArrayIndexOutOfBoundsException
. But if you are getting a NullPointerException
, that implies that your method is being called with an array that contains null
values. To my mind, that is really an error in the code that calls isCapOrDigit
.
And I suspect that the entire method is flawed. Is this method supposed to test one string, or multiple strings?
- In the former case, why are you passing it an array of strings?
- In the latter case, what are you supposed to return if some of the strings match the criteria and others don't?
There are elements, 10 elements of a string array. I'm reading it from a file.
Well it appears that you are not actually reading exactly 10 elements and putting them all into the array correctly. Some of the array elements appear to be null
.
Finally, this is incorrect:
if (Character.isUpperCase(arr[i].charAt(0))
&& Character.isDigit(arr[i].charAt(0))) {
return true;
}
You are testing to see if the character is an uppercase character AND a digit ... at the same time. That's not possible. Digits aren't upper-case. Surely you should be using an OR; i.e.
if (Character.isUpperCase(arr[i].charAt(0))
|| Character.isDigit(arr[i].charAt(0))) {
return true;
}