-1

I need help moving numbers from an ArrayList (listInt) to three different arrays (iN, v and w).

`import java.io.; import java.util.;

public class ReadingFileThree {

public static void main(String[] args) throws FileNotFoundException{

    //Try Catch for error catching
    try{

        int n = 0,C;  //Number of items and maximum capacity
        int iN[] = null,v[] = null,w[] = null;  //Item number, weights and values of items
        int V[][];  //Table to store results of sub-problems

        //Calling inbuilt methods to open the file we wish to read
        Scanner s = new Scanner(new File("easy.20.txt"));

        //An ArrayList is used because it increases in size dynamically
        ArrayList<Integer> listInt = new ArrayList<Integer>(); //Int ArrayList

        //Method for adding the numbers in the file to the Int ArrayList
        while (s.hasNextLine()){
            listInt.add(s.nextInt());
            n++;
        }

        //Closing the file. Need to be done or we get errors and memory leaks
        s.close();

        //Hold the maximum capacity of the knapsack (The last int in the file)
        C = listInt.get(n-1);




        for(int i = 0; i < n; i++){
            iN[i] = listInt.get(i+1);   //item number
            v[i] = listInt.get(i+2);  //weight of item
            w[i] = listInt.get(i+3);  //value of item
            i = i+2;
        }

        //Untested next stage
        //V = new int[n+1][C+1];  //initialising the table to hold results
        //for(int i = 0; i <= C; i++) V[0][i] = 0;
        //}

        //Print out commands for testing purposes
        System.out.println("All the numbers in the ArrayList are: " + listInt);
        System.out.println("n = " + n);
        System.out.println("C = " + C);


    }
    catch(Exception e) {
        System.out.println("File not found yo");
    }

}

} `

The problem appears in the for loop. It gives an Error when trying to use an array in there. The error is: null pointer access problem

The file being read looks like this.

4 1 16 34 2 3 30 3 46 34 4 42 47 10

  • The number on the first line is the Number of Items
  • The number on the last line is the Capacity of the Knapsack
  • The four lines in between show the: Item Number - Value - Weight

I haven't used Java much, Please help.

Mcclaine
  • 1
  • 1
  • where do you allocate iN? – MeBigFatGuy Mar 28 '17 at 16:18
  • 2
    by the looks of it, you have not used any programming language much. You cannot "debug a program via stackoverflow". You will need to learn to debug. Or at least to embed `System.out.println()` statements within your code, so as to figure out what you are doing wrong. – Mike Nakis Mar 28 '17 at 16:18
  • I took my debugging out to make it more readable on here. I have System.out's all over the place. Should I paste the entire code next time? – Mcclaine Mar 30 '17 at 08:07

2 Answers2

0

Quite simply, the issue is that you are accessing null objects when attempting to assign values to iN, v, and w. At minimum you should be initializing your arrays at some point before assigning values to them.

Since you're already using ArrayList, it seems to be something that is allowed for your assignment so why not just declare those array variables to that object type and insert into that object instead of creating static arrays?

Lastly, more along the lines of style, I generally like to have more descriptive variable names in my code, it leads to less confusion as to what they are meant for. For example my guess is that v is for value and w is for weight in the example above, but the comments in your for loop would suggest that that assumption is wrong.

Brandon
  • 25
  • 1
  • 7
0

I think the for loop is incorrect. I think it should be like the following ( i havnt tested this code)

for(int i = 0; i < n; ){
    iN[i] = listInt.get(i+0); //item number 
    v[i] = listInt.get(i+1); //weight of item 
    w[i] = listInt.get(i+2); //value of item
    i = i+3; 
}
doddi76
  • 61
  • 3
  • This but was all wrong. It should have looked like this: int temp = 0; ` for(int i = 0; i < n; i++){ iN[i] = listInt.get(temp+1); //item number v[i] = listInt.get(temp+2); //value of item w[i] = listInt.get(temp+3); //weight of item temp = temp + 3; } ` – Mcclaine Mar 31 '17 at 09:57