简体   繁体   English

Vector引起的java.lang.NullPointerException

[英]java.lang.NullPointerException caused by a Vector

I 'm using a matrix where each element is a vector of object "Person". 我正在使用一个矩阵,其中每个元素都是对象“ Person”的向量。 I defined my Person class. 我定义了Person类。

In an other class (and in the same package), I declared ma matrix as the following: 在另一个类(和相同的程序包中)中,我声明ma矩阵如下:

Vector<CKYCell> [] [] Score= new Vector [length][length];

I want to know the size of each element of the matrix, I use: 我想知道矩阵每个元素的大小,我使用:

System.out.println(Score[i][i].size());

But I have the following error: 但是我有以下错误:

java.lang.NullPointerException

Is there any one who could help me. 有谁能帮助我。 Thanks in advance. 提前致谢。

Vector [][] is an array of nulls. 向量[] []是空数组。 You need to allocate that actual vectors and stuff them into the array. 您需要分配该实际矢量并将其填充到数组中。

The problem is that you create a 2 dimensional array of Vector s here: 问题是您在这里创建Vector的二维数组

Vector<CKYCell> [] [] Score= new Vector [length][length];

but you leave it empty. 但您将其留空。 The first time you try to dereference an item in it: Score[i][i] you will get the default value form the array which is null . 首次尝试取消引用其中的项目: Score[i][i] ,将从数组中获取默认值null

To add an item to your array you can simply do something like this: Score[i][i] = myItem; 要将项目添加到数组中,您可以执行以下操作: Score[i][i] = myItem;

Keep in mind that Score does not refer to a Vector but an array containing Vector s . 请记住, Score不是指Vector而是包含Vector的数组 Vector objects are also considered to be deprecated in most cases. 在大多数情况下, Vector对象也被视为不赞成使用。

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

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