r/javahelp • u/Madridi77 • Jun 08 '19
Why is there a NullPointerException?
Hi all!
Please check my method below, for some reason I get NullPointerException for line 9.
countryArray is an array of strings, with countries, but then it is also filled with a bunch of null after the list of countries is over.
public static void golds(String[] countryArray) {
//int count = 0;
String currentCountry = new String();
for (int i = 0; i < countryArray.length; i++) {
currentCountry = countryArray[i];
int count = 0;
for (int j = 0; j < countryArray.length; j++) {
if (countryArray[j] != null) {
if (currentCountry.equals(countryArray[j])) { // FIX THE NULL POINTER ERROR!!!!!!!!!
count++;
}
}
else {
j = countryArray.length;
}
}
}
countryArray = removeDuplicates(countryArray);
System.out.println("Count of gold medalists by country:");
for (int g = 0; g < countryArray.length; g++) {
System.out.println(countryArray[g]);
}
1
u/johnmclucas1 Jun 08 '19
I am not sure what you are trying to do at lines, 13, 14, 15. Are you just trying to break out of the for loop? Do you allow nulls in the array in the first place? If you do allow nulls in the array and you hit one, I would think you would just use a continue statement. When you find your match, count++; break;
for (int j = 0; j < countryArray.length; j++) {
if (countryArray[j] != null) {
if (currentCountry.equals(countryArray[j])) {
count++;
break;
}
} else { continue;}
You also never check for null when working through the first for loop. If you are checking for nulls in the array in the second loop, you should be checking in the first loop as well.
for (int i = 0; i < countryArray.length; i++) {
if(countryArray[i] != null)
currentCountry = countryArray[i];
Just my two cents. Cleaning it up 2nd for loop and null check should help.
1
u/iPissVelvet Jun 08 '19
Are you using an IDE by chance? An IDE let’s you debug your code by walking through it line by line. You’ll be able to find your NPE with this tool. Let me know if you have any other questions!
1
u/Fizz-Buzzkill Jun 08 '19
Instead of countryArray, use a List<String> and that will allow you to use a for each loop to iterate the list values and to insure you are only accessing non null values.
1
u/bostown03 Jun 08 '19
The for loop that has countryArray[j] should be encapsulated in a try catch ..
1
u/ThatLesbian Extreme Brewer Jun 08 '19
There’s a few issues here. You assign currentCountry but don’t do a null check before using it again. You can either check for null before using it, or initialize it to “” instead of filling your array with nulls.
Second minor issue is your line that assigns currentCountry = new String();
Pretty much never do this in Java, it’s an advanced option for very particular use cases. Instead do currentCountry = “”;
1
u/anthropaedic Jun 08 '19
Another option that is null-safe is to use Optionals.
4
u/iPissVelvet Jun 08 '19
Bad advice. Don’t use optionals. OP is a beginner — optionals should be used in conjunction with streams, and I don’t think OP is close to that yet.
1
u/anthropaedic Jun 08 '19
Don’t see how it’s bad advice. It’s a part of the language and Oracle’s answer to null-conditional operators.
It can be used with streams but nothing about it requires that.
https://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html
2
u/lbkulinski Jun 08 '19
Did you initialize the elements of your array? They are
nullby default, which would be whycurrentCountryisnull.