-1

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 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]1[3][2]

[2][3]1[2][3]

[3][3]2[2]

[2][3]1[2]1

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 < 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][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 < end_alloRow; i++){
        int k = 0;
        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[k][j] = Integer.parseInt(array[j]);  //use alloArray[i] row for [j] element
            System.out.println(allocatedArray[k][j]); //print each element
            k++;
        }
    }
}
malb
  • 1
  • 1
  • 1
    Either `allocatedArray` or `array[j]` is null, meaning not defined. Please show us more of the error. – cegredev Apr 09 '20 at 20:11
  • Another problem I see, is that you are looping through `array`, until `j <= array.length`. That's one iteration to much. Use `<` instead. – cegredev Apr 09 '20 at 20:13
  • @Schred - I added a screenshot. I also added that in to try, but forgot to take it out. It doesn't work without it either. I'll go ahead and revert it now, thanks! – malb Apr 09 '20 at 20:14
  • Don't post stuff like that as screenshots, copy it as a blockquote into the question. Based of the image however, I'm pretty sure it has something to do with `allocatedArray`. Make sure you fully initialize it. – cegredev Apr 09 '20 at 20:16
  • Can you explain fully initializing it? I have it declared as int[][] allocatedArray, then that line of code should populate it. – malb Apr 09 '20 at 20:17
  • Ah I see, I'll write an answer below. – cegredev Apr 09 '20 at 20:18
  • So in my method, put allocatedArray = new int[num_process][num_resource]? – malb Apr 09 '20 at 20:19
  • That will work, depending on what those variables contain. It's a step in the right direction however. – cegredev Apr 09 '20 at 20:20
  • I think a link to how to initialize arrays would be better @EJoshuaS-ReinstateMonica . – cegredev Apr 09 '20 at 20:22
  • So initializing that in my method definitely helped, but I realized I have a different issue. – malb Apr 09 '20 at 20:26

1 Answers1

0

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(" ");
allocatedArray[i] = new int[array.length];

and finally set the values like this:

allocatedArray[i - start_alloRow][j] = Integer.parseInt(array[j]);
cegredev
  • 1,485
  • 2
  • 11
  • 25
  • Ok let me try that really quick. – malb Apr 09 '20 at 20:29
  • I saw you're using the letter 'k' in your edited version, keep in mind that my answer was meant for the previous version. Just a heads up. – cegredev Apr 09 '20 at 20:35
  • I'd also check that `Integer.parseInt(...)` is not always returning 0. – cegredev Apr 09 '20 at 20:36
  • Yes I removed k once I saw your post about i - start_alloRow. I was trying k to make sure it counted the rows correctly. How would I check that it is not always returning 0? This is my error now. – malb Apr 09 '20 at 20:40
  • Textfile name: test Number of Processes: 4 Number of Resource Types: 5 R0:4 R1:5 R2:2 R3:3 R4:3 00000 00000 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4 at TextReader.allocatedUnits(TextReader.java:88) – malb Apr 09 '20 at 20:41
  • Just do `System.out.println(Integer.parseInt(array[j]));` before assigning the value. – cegredev Apr 09 '20 at 20:41
  • I figured out why. It worked!! Thank you for your help – malb Apr 09 '20 at 20:42
  • You're welcome. ;) I don't know if this is disabled for closed questions, but if not please mark this answer as accepted. – cegredev Apr 09 '20 at 20:44