开发者

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开发者_JAVA百科 synchronized blocks so I nowhere choose a specific monitor explicitly).


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.


As @Gabe says, it's probably that routingTable[commonBitGroups] is null. Java does not have real multidimensional arrays: a 2-d array is an array of arrays (and a 3-d array is an array of arrays of arrays).

Incidentally you don't have to initialize references in an array to null in Java, that is their default value. In this case it's also your problem. You're setting the second level of array values to null. What you meant was

for (int i = 0; i < 32; i++) {
    Arrays.fill(routingTable[i], null);
}

But as above, this is unnecessary. So just remove your call to Arrays.fill.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜