Search code examples
javanullrminullpointerexception

NullPointerException at a simple null check in java


I get a NullPointerException at a line on which just a simple null check takes place.The line is the following:

if(routingTable[commonBitGroups][nextNumberOfOther-1]==null)

I verify that the array is not null just before this line. commonBitGroups and nextNumberOfOther are both simple int types.

I should add that this line is part of an app that uses rmi and part of a class which extends UnicastRemoteObject and implements a RemoteInterface.I specify that because I am under the impression that a NullPointerException can occur when you deal with synchronization even if nothing is realy null (maybe when something is locked) ,and I deal with synchronization in this app.The method that contains the line though is not synchronized and nowhere in my code I try to use the array as a monitor (I only have some synchronized methods ,no smaller synchronized blocks so I nowhere choose a specific monitor explicitly).


Solution

  • If the following line throws an NPE:

    if (routingTable[commonBitGroups][nextNumberOfOther - 1] == null)
    

    then either routingTable is null or routingTable[commonBitGroups] is null.


    You say that the array is initialized as follows:

    routingTable = new NodeId [32][15]; 
    Arrays.fill(routingTable, null);
    

    "Well there's your problem!"

    The first line gives you an array of 32 NodeId[]'s, with the elements initialized to non-null values ... arrays of size 15. (So far so good ...)

    The second line sets routingTable[i] to null for all i .... Ooops!!

    Delete the second line.