简体   繁体   English

在Java中进行简单的null检查时出现NullPointerException

[英]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: 我在只进行简单的null检查的行上得到了NullPointerException,该行如下所示:

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

I verify that the array is not null just before this line. 我验证数组在此行之前不为null。 commonBitGroups and nextNumberOfOther are both simple int types. commonBitGroups和nextNumberOfOther都是简单的int类型。

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). 我应该补充一点,这一行是使用rmi的应用程序的一部分,也是扩展UnicastRemoteObject并实现RemoteInterface的类的一部分。真正为null(也许是什么时候被锁定),并且我在此应用程序中处理同步。包含行的方法虽然未同步,但在我的代码中却没有尝试将数组用作监视器(我只有一些同步方法,没有较小的同步块,因此我无处可去选择明确的监视器)。

If the following line throws an NPE: 如果以下行抛出NPE:

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

then either routingTable is null or routingTable[commonBitGroups] is null . routingTablenullroutingTable[commonBitGroups]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 ...) 第一行为您提供32个NodeId []的数组,并将元素初始化为非空值...大小为15的数组。(到目前为止非常好...)

The second line sets routingTable[i] to null for all i .... Ooops!! 第二行将所有i ...的routingTable[i]null

Delete the second line. 删除第二行。

As @Gabe says, it's probably that routingTable[commonBitGroups] is null. 如@Gabe所说,routingTable [commonBitGroups]可能为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). Java没有真正的多维数组:2-d数组是数组的数组(而3-d数组是数组的数组的数组)。

Incidentally you don't have to initialize references in an array to null in Java, that is their default value. 顺便说一句,在Java中,您不必将数组中的引用初始化为null,这是它们的默认值。 In this case it's also your problem. 在这种情况下,这也是您的问题。 You're setting the second level of array values to null. 您正在将第二个数组值级别设置为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. 因此,只需删除对Arrays.fill的调用即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM