Shuma
Shuma

Reputation: 293

Java: Inconsistent NullPointerException

I am having trouble with a method in Java thats throwing up an error:

Exception in thread "main" java.lang.NullPointerException
at FamilyTree$FamilyTreeNode.access$9(FamilyTree.java:5)
at FamilyTree.displayFamilyMember(FamilyTree.java:166)
at FamilyTreeTest.main(FamilyTreeTest.java:28)

The method in question is:

public void displayFamilyMember(){  
    boolean cascade = false;    
     int memberIdentifier;              
     displayFamily();
     memberIdentifier = Input.getInteger("Input member ID");
     currentNode = ancestor ;
        if (currentNode.identifier == memberIdentifier || currentNode.partner.identifier == memberIdentifier){
            cascade = true;
            if(currentNode.partner!= null){
                System.out.println(currentNode.Name + " ID[" + currentNode.identifier + "] Partner:  " + currentNode.partner.Name + " ID[" + currentNode.partner.identifier + "]");
                }else{
                    System.out.println(currentNode.Name + " ID[" + currentNode.identifier + "] has no partner");
                }
            }           
        if(currentNode.child != null){                              
                 currentNode = currentNode.child;                   
                 if (currentNode.identifier == memberIdentifier || cascade == true || currentNode.partner.identifier == memberIdentifier){                      
                     if(currentNode.partner!= null){
                            System.out.println("     " + currentNode.Name + " ID[" + currentNode.identifier + "] Partner:  " + currentNode.partner.Name + " ID[" + currentNode.partner.identifier + "]");
                            }else{
                                System.out.println("     " + currentNode.Name + " ID[" + currentNode.identifier + "] has no partner");
                            }
                     }
        }               

        if (currentNode.sibling!= null){
             while(currentNode.sibling != null){                        
                 currentNode = currentNode.sibling;                 
                    if (currentNode.identifier == memberIdentifier || cascade == true || currentNode.partner.identifier  == memberIdentifier){
                     System.out.println("Check for match performed, checking partner !- null");                 
                     if(currentNode.partner!= null){
                            System.out.println("     " + currentNode.Name + " ID[" + currentNode.identifier + "] Partner:  " + currentNode.partner.Name + " ID[" + currentNode.partner.identifier + "]");
                            }else{
                                System.out.println("     " + currentNode.Name + " ID[" + currentNode.identifier + "] has no partner");
                            }
                         }
                 }
                 }

        else{
            System.out.println("member not found");
        }


}

The line that is throwing the error:

 if (currentNode.identifier == memberIdentifier || cascade == true || currentNode.partner.identifier == memberIdentifier){  

Now as a guess, I would say I'm getting this error, because currentNode.partner.identifier, which I'm checking the value of, is null / the currentNode.partner node doesn't exist.

This doesn't prevent the previous two identical checks from functioning correctly, so I'm a bit perplexed as to why this line is any different.

The structure is

Alice[2] <--partner-- John[1] 
                     |
                   Ted[3] --sibling--> Eric[4] --sibling--> Joanne[5]

EDIT: Thanks for the replies, I understand how it works now.

if (currentNode.identifier == memberIdentifier || currentNode.partner.identifier == memberIdentifier){

On the first Node, this line SHOULD throw the exception, but doesn't because the first condition is met and the others don't get checked. Referencing currentNode.partner.identifier while currentNode.partner = Null was the problem.

Upvotes: 1

Views: 261

Answers (1)

After looking at your code, the only reason I'd expect that specific line to throw a null reference error was if currentNode.partner was null.

The reason why it's different every time, is because you keep changing currentNode like so

currentNode = currentNode.sibling;  

Additionally, if your If condition looks like if (A || B) than it will always be true when A is true regardless of what B is, so It won't check B when A is true.

because of this behavior and it's inverse with &&, you might see conditions that look like

if(myObj != null && myObj.value == otherVal)

Your previous line might never have executed

currentNode.partner.identifier == memberIdentifier

if currentNode.identifier == memberIdentifier or cascade was true


Because of this, Even if the exact same line executed when currentNode.partner was not null, that might not be the case after you change currentNode


If I were you, I'd find a good java debugger, and put a breakpoint before where the error occurs.

Upvotes: 5

Related Questions