当出现此种错误的时候 可能的原因有以下几种:

变量只声明了 但是没有初始化:字符串 接口类型的对象没有用具体的类进行初始化。

而且有时候 不一定初始化为什么数值才叫初始化,对于类来说 直接new class();就行了。

还有一种可能的错误 就是同名的构造器参数和类的属性一定要用this.num = num链接起来 this很重要。
就像下面这样,不这样做就会有null pointer错误。

class Solution {
    
    int[] nums;
    Random rand;
    int N;

    public Solution(int[] nums) {
        this.nums = nums; //同名的一定要用nums不然会出现null pointer的错误
        N = nums.length;
        rand = new Random();
    }
    
    public int pick(int target) {
        int count = 0;
        int res = 0;
        for (int i = 0; i < N; i++) {
            if (nums[i] != target) {
                continue;
            } else {
                count++;
                int random = rand.nextInt() % count;
                if (random == 0) {
                    res = i;
                }
            }
        }
        return res;
        
    }
}