简体   繁体   English

此NullPointer异常的原因可能是什么?

[英]What could be the cause of this NullPointer Exception?

The following is a part of my code. 以下是我的代码的一部分。

I get a NullPointerException in the line " buckets[i][h.hashBands(sum, bandRows)].add(j); ". 我在“ buckets [i] [h.hashBands(sum,bandRows)]。add(j);”行中得到了NullPointerException。

What could be the reason? 可能是什么原因?

public static void generateBuckets()
{
    hash h = new hash();
    buckets = new ArrayList[bands][bandRows];

     for(int i=0; i<bands; i++)
     {
         for(int j=0; j<preprocessedList.size(); j++)
         {
             int[] sum = new int[bandRows];
             int a=0;
             for(int k=i; k<bands; k++)
             {
                 sum [a] = sigMatrix[k][j];
                 a++;
             }
             buckets[i][h.hashBands(sum, bandRows)].add(j);
         }
     }

The h.hashBands() function is as follows h.hashBands()函数如下

public int hashBands(int[] in, int bucketSize) 
    {        
        BigInteger hashVal = BigInteger.ZERO;
        int k = in.length;
        BigInteger base = BigInteger.valueOf(3);
        BigInteger size = BigInteger.valueOf(bucketSize);

        for (int i = 0; i < in.length; i++)
            hashVal = (hashVal.add(BigInteger.valueOf(in[i]).multiply(base.pow(k-i-1))));

        hashVal = hashVal.mod(size);
        return hashVal.intValue();
    }

You are creating a 2D array of ArrayLists 您正在创建ArrayLists的2D数组

buckets = new ArrayList[bands][bandRows];

but never filling it with ArrayLists so every element is null. 但永远不要用ArrayLists填充它,因此每个元素都为null。 When you call .add(), you get a NullPointerExcepion 当您调用.add()时,您会得到一个NullPointerExcepion

You never initialize buckets[][] . 您永远不会初始化buckets[][]
You will need to add a buckets = new BucketType[xlength][ylength]; 您将需要添加一个buckets = new BucketType[xlength][ylength]; somewhere before using it. 在使用之前。

您尚未将buckets数组中的特定条目初始化为任何东西。

very hard to say for sure. 很难肯定地说。 either you haven't initialized buckets, or the elements in it, ... anyway, answering this for sure is pretty impossible. 无论是您尚未初始化存储桶,还是尚未初始化存储桶中的元素,...肯定地回答这个问题几乎是不可能的。 it would be easier if we saw a bit more of the code, and maybe the stacktrace. 如果我们看到更多的代码,也许是堆栈跟踪,将会更容易。

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

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