Jospeh Cusco
Jospeh Cusco

Reputation: 35

NullPointerException in if-statement

I've run into a NullPointerException in the following if statement;

public Player players[];

public void sortPlayers() {
    int n = players.length;

    for (int pass=1; pass < n; pass++){
        for (int i=0; i < n-pass; i++) {
            if(players[i].getLastName().compareTo(players[i + 1].getLastName()) > 0)
            temp = players[i];
        players[i] = players[i+1];
        players[i+1] =  temp;
        }
    }
}

I've googled the error report, but nothing seems to fit. If it helps, below is the Player class and the getLastName() method. Also, the expected data to be sent to the Player class would be a name, in the form of first-name then last-name, so something like "Bobby Joe."

public class Player {

public String[] name;

public Player(String inputname) {

    name = inputname.split(" ");

}

public String getLastName() {
    return name[1];
}

Any help on the matter would be greatly appreciated!

EDIT- Sorry, yes, this is Java. DOUBLE EDIT -I've put the complete source code up onto a Pastebin if anyone would care to take a look at it. The error has been narrowed down to whenever the players[i] array is called in the Team.sortPlayers() method.

http://pastebin.com/sJKTpJA9

Upvotes: 0

Views: 598

Answers (9)

Jim
Jim

Reputation: 2702

The problem lies in the if statement:

for (int pass=1; pass < n; pass++){ 
    for (int i=0; i < n-pass; i++) { 
        if(players[i].getLastName().compareTo(players[i + 1].getLastName()) > 0) 
        temp = players[i]; 
    players[i] = players[i+1]; 
    players[i+1] =  temp;
    }
 }

If, in the first pass (i.e. i == 0, the compareTo method returns a value equal to or less than 0, the statement assigning temp a pointer to element 0 of the players array will not execute, temp will have a null pointer and the next two lines, not conditionally executed in the scope of the if statement, will assign element 1 to element 0 of the players array and a null pointer to element 1.

On the next pass (i.e. i == 1), this will then result in a java.lang.NullPointerException because now, the players[1] element has a null pointer from the first pass.

Depending on the order of names input, the exception might or might not be thrown if, on the first pass, the compareTo method returns a value > 0. From that point on, temp will have a valid pointer but whatever it points to will get swapped with the i+1th element even if it is not pointing to the ith element. The array's name contents will likely get mangled because the name sawp will not occur as desired.

Fixing the code segment to read:

for (int pass=1; pass < n; pass++) { 
    for (int i=0; i < n-pass; i++) { 
        if(players[i].getLastName().compareTo(players[i + 1].getLastName()) > 0) {
            temp = players[i]; 
            players[i] = players[i+1]; 
            players[i+1] =  temp;
        }
    }
}

will cure the problem and also have a better indentation practice.

One visible indicator of the problem is that the indentation in your sample, although not normal practice for if and for scopes, still indicates that only the first line of the array element swap is in the scope of the if statement instead of all three lines.

If scope mistakes are a common root of software errors.

Upvotes: 1

Nic Robertson
Nic Robertson

Reputation: 1208

Make sure that when you initialise your players array correctly

    for(int i=0; i < numPlayers; i++){
        playerArray[i] = new Player("Joseph Blog");
    }

Make sure all players have the space or else when getlastname is called there will be a null value for last name. Perhaps you should change the constructor to take two strings instead, because then you could allow middle names also, and would not need to split the string each time you needed the last name. Make sure you also do not access and index of the array that is out of bounds.

Upvotes: 0

chetan
chetan

Reputation: 3255

i think players[i + 1].getLastName() gives null pointer when pointer reaches to last index.

Upvotes: 0

special
special

Reputation: 641

Better you try to print the names before using if condition check whether you are getting a null exception or not.

Upvotes: 0

Andrew Cooper
Andrew Cooper

Reputation: 32576

Try using a debugger to see which part of the statement is giving the null result.

Alternatively, you could break out the code a bit to make it easier to find the problem.

String playerName = players[i].getLastName();
String nextPlayerName = players[i+1].getLastName();
if(playerName.compareTo(nextPlayerName) > 0) 
{
    ...
}

Upvotes: 0

TGH
TGH

Reputation: 39248

It looks like your .getLastName() returns null, and you get a null pointer exception when you try to call compareTo() on an object that is null.

Make sure the last name won't return null, or add a null check around it if null is valid

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120138

From the code you have shown.

players itself is null because you never initialize it (i.e. there is no new Player[x] in the code you have shown). When you do

int n = players.length;

it go boom. You want something like

public Player [] players = new Player[n];

somewhere after you know what n is. Perhaps initialize it in the constructor

public Player [] players; // like you do now
...
players = new Player[n]; // in some constructor

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124622

Well... something here is obviously null.

if(players[i].getLastName().compareTo(players[i + 1].getLastName()) > 0)

It could be

players
players[i]
players[i].GetLastName()
players[i + 1]
players[i + 1].GetLastName()

We can't know which, you'll have to use a debugger.

Upvotes: 0

icyrock.com
icyrock.com

Reputation: 28578

This one:

if(players[i].getLastName().compareTo(players[i + 1].getLastName()) > 0)

has a lot of possibilities to throw NullPointerException:

  • players[i] can be null (e.g. you are not properly initializing your array)
  • First getLastName can return null (if your name member variable is null)
  • players[i + 1] can be null

Upvotes: 0

Related Questions