空指针异常:将字符串一维数组解析为整型二维数组时发生错误。

huangapple 未分类评论45阅读模式
英文:

NullPointException error when parsing string 1D array to int 2D array

问题

void allocatedUnits() {
    String au;
    start_alloRow = 2;                             // start row 2 of text file
    end_alloRow = start_alloRow + num_process;     // length is how many processes were indicated
    allocatedArray = new int[num_process][num_resource];

    for (int i = start_alloRow; i < end_alloRow; i++) {
        au = fileArray[i];                          // fileArray element i into String au (i=start-end)
        String[] array = au.split(" ");             // split String au "i" into array elements
        for (int j = 0; j < array.length; j++) {     // for element j 0-arraylength (0-num_resource)
            allocatedArray[i - start_alloRow][j] = Integer.parseInt(array[j]); // use allocatedArray[i-start_alloRow] row for [j] element
            System.out.println(allocatedArray[i - start_alloRow][j]); // print each element
        }
    }
}

Please let me know if you have any questions or need further assistance.

英文:

Background:
I am writing a program that uses BufferedReader to store several lines of a text file into a String array.

Example of text file:

>4 5

>4 5 2 3 3

>3 2 1 3 2

>2 3 1 2 3

>3 3 2 1 2

>2 3 1 2 1

ArrayList<String> of textfile lines:
>["4 5"]["4 5 2 3 3"]["3 2 1 3 2"]["2 3 1 2 3"]["3 3 2 1 2"]["2 3 1 2 1"]

Need a 2D array like this:
>[3]2[3][2]

>[2]3[2][3]

>[3][3]2[2]

>[2]32

The first line(array element) essentially indicates the size of my array which I successfully created a new array using array.split(" "): [4][5]. The second line is used for a different method which was successful as well using array.split(" "). I'm having an issue with lines 3-6.

void allocatedUnits(){
	String au;
	start_alloRow = 2; 							//start row 2 of text file
	end_alloRow = start_alloRow + num_process;	//length is how many processes were indicated

	for (int i = start_alloRow; i &lt; end_alloRow; i++){
		au = fileArray[i];						//fileArray element i into String au (i=start-end)
		String[] array = au.split(&quot; &quot;);			//split String au &quot;i&quot; into array elements
		for (int j = 0; j &lt;= array.length; j++){         //for element j 0-arraylength (0-num_resource)
			allocatedArray[i][j] = Integer.parseInt(array[j]);	//use alloArray[i] row for [j] element
			System.out.println(allocatedArray[i][j]); //print each element
		}

	}}

The error I get is NullPointerExcetion at the line of allocatedArray[i][j] = Integer.parseInt(array[j]);

I have printed the String[] array elements without the 2D array and it prints correctly, so I'm unsure if I'm creating the 2D array incorrectly.

Error screenshot

EDIT:// I now have an ArrayIndexOutOfBounds exception which I understand, but don't know how to fix it. This is my updated code with initialized array (which helped)

void allocatedUnits(){
	String au;
	start_alloRow = 2; 							//start row 2 of text file
	end_alloRow = start_alloRow + num_process;	//length is how many processes were indicated
	allocatedArray = new int[num_process][num_resource];

	for (int i = start_alloRow; i &lt; end_alloRow; i++){
		int k = 0;
		au = fileArray[i];						//fileArray element i into String au (i=start-end)
		String[] array = au.split(&quot; &quot;);			//split String au &quot;i&quot; into array elements
		for (int j = 0; j &lt; array.length; j++){ //for element j 0-arraylength (0-num_resource)
			allocatedArray[k][j] = Integer.parseInt(array[j]);	//use alloArray[i] row for [j] element
			System.out.println(allocatedArray[k][j]); //print each element
			k++;
		}
	}
}

答案1

得分: 0

你需要完全初始化 allocatedArray。为了做到这一点,你需要知道两件事情:

  1. 你想要有多少个子数组。
  2. 每个子数组应该有多大。

所以在你的情况下,你应该首先执行:

allocatedArray = new int[end_alloRow - start_alloRow][];

然后像这样初始化子数组:

au = fileArray[i];
String[] array = au.split(" ");
allocatedArray[i] = new int[array.length];

最后像这样设置值:

allocatedArray[i - start_alloRow][j] = Integer.parseInt(array[j]);
英文:

You need to fully initialize allocatedArray. To do that, you need to know two things:

  1. How many subarrays you want it to have.
  2. How big each subarray should be.

So in your case, you should first do:

allocatedArray = new int[end_alloRow - start_alloRow][];

then initialize the subarray like this:

au = fileArray[i];
String[] array = au.split(&quot; &quot;);
allocatedArray[i] = new int[array.length];

and finally set the values like this:

allocatedArray[i - start_alloRow][j] = Integer.parseInt(array[j]);

huangapple
  • 本文由 发表于 2020年4月10日 04:09:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61129427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定