Hi, I have been asked to create a method to store information on the football players for the polish team. I have stored each player in an array of Players[]. My task is to output any players which match the search string. So the method I have to create is getAllContaining(String search). If any of the players match the name, their details are outputted.

I managed to do it fine in my Country class , the method looks like this :

public String getAllContainingName(String search){


    String str = "";

    for (Person p : squad) {
        if ((p instanceof Player) || (p instanceof Manager)) {
                    if(p.matchByName(search) == true){

                str += p.toString() + "\n";
            }
        }
    }
        return str;


    }

Which correctly outputs the players if they match the string.
I then have to implement the same method but into a different class called Group.
My method looks like this:

public String getAllContaining(String search){


       String str = "";
       for(Country c : teams ){

         str += c.getAllContainingName(search);

       }



       return str;


}

But the method does not work, it returns

Exception in thread "main" java.lang.NullPointerException
at hutchisonbarry.Group.getAllContaining(Group.java:50)
at hutchisonbarry.HutchisonBarry.main(HutchisonBarry.java:88)

Help is greatly appreciated thanks .

Exception in thread "main" java.lang.NullPointerException
at hutchisonbarry.Group.getAllContaining(Group.java:50)
at hutchisonbarry.HutchisonBarry.main(HutchisonBarry.java:88)

There is a variable on line 50 that has a null value when the code is executed. Look at line 50, find the variable with the null value and backtrack in the code to see why that variable doesn't have a valid non-null value.

line 50 is : str += c.getAllContainingName(search);

Still confusing me, i initialised it as String str = "" before hand so?

What about the other varibles: c, search

search is not initialised, neither is Country c. I used a for each so that i could use the method from Country, if that makes sense.

The java program found: java.lang.NullPointerException so there must be a null variable.
You need to find the variable with the null value and change the code so that it does not have a null value. Print out the value of all the variables used where the error happens to see which one is null.

I got it anyway thanks a lot for your time

Please mark this thread as solved if you are done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.