空指針訪問:變量b只能在這個位置為空

[英]Null pointer access: The variable b can only be null at this location


I get the above mentioned error in the last line of this following portion of my code.

我在下面代碼的最后一行中得到了上面提到的錯誤。

String[][] b = null;
try {
  br = new BufferedReader(new FileReader(csvFile));
  while ((line = br.readLine()) != null) {
    // use comma as separator
    String[] country = line.split(cvsSplitBy);
    String[] a=country[1].split(splits);
    i = 0;
    n = 0;
    while(i<a.length) {
      b[j][n]+=a[i];
    }
  }
} catch (...) {}

What is the reason for this error and how to solve this?

這個錯誤的原因是什么?如何解決這個問題?

3 个解决方案

#1


2  

The only two uses of b are

b的兩個用途是

b = null;
...
*b[j][n] ...

You need to set b to an actual array before you can try indexing it.

您需要將b設置為一個實際的數組,然后才能嘗試對它進行索引。

#2


0  

String[][] b is null. Look like you should not use Array here as you don't know the size up front. You can use an ArrayList with some custom object.

String[][]b是null。看起來你不應該在這里使用數組,因為你不知道前面的大小。您可以使用ArrayList與一些自定義對象。

#3


0  

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2) [...] A component of an array is accessed by an array access expression (§15.13) that consists of an expression whose value is an array reference followed by an indexing expression enclosed by [ and ], as in A[i].

在Java編程語言中,數組是對象(§4.3.1)是動態創建的,可能是分配給變量類型的對象(§4.3.2)[…)組件訪問數組的數組訪問表達式(§15.13),由一個表達式的值是一個數組引用緊隨其后的是一個索引表達式包圍[和],在[我]。

How do array access expression work? Like this.

數組訪問表達式如何工作?像這樣。

Relevant bit:

相關:

Otherwise, if the value of the array reference expression is null, then a NullPointerException is thrown.

否則,如果數組引用表達式的值為null,則拋出NullPointerException。

Long story short: arrays are objects, and b is a reference to an (array) object - like basically everything in Java except primitive types.

長話短說:數組是對象,而b是對(數組)對象的引用——就像Java中除了基本類型之外的所有東西。

A reference can point to something or be a null reference.

引用可以指向某物,也可以是空引用。

A null-reference is something entirely different from any other sort of object in the heap, including an empty array: you can't do anything with it, including trying to access its elements, except assigning a value to it (imagine it as a blank "phone" line in an address book: before calling you have to have a number to call!)

空引用是完全不同於其他類型的對象在堆中,包括一個空數組:你不能做任何事情,包括試圖訪問它的元素,除了分配一個值(假設這是一個空白“電話”在地址簿行:在調用之前你必須有電話號碼!)


So, you have to first make your reference point to an actual array, with

所以,你必須首先將你的參考點指向一個實際的數組

b = new String[10][10].

b =新的字符串[10][10]。

Using this right after

使用這個后

String[][] b = null;

String[][]b =零;

would be a good idea; or you can just String[][] b = new String[10][10]().

這是個好主意;或者你可以把[][]b =新的字符串[10][10]()


At this point, b points to a string array in the heap, and you can do things with it.

此時,b指向堆中的一個字符串數組,您可以對它進行處理。

Note that you have to specify a size.

注意,您必須指定一個大小。

If you don't know it yet you might be better off with other containers, for example ArrayList, which grows dynamically.

如果您還不知道它,那么最好使用其他容器,例如ArrayList,它是動態增長的。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/07/25/729897b414e36b4892160db4a3e6fa38.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com