0

I trying to store my already found 2D array into a 1D array for faster processing later. However, I keep getting a nullPointerException when I try to fill the 1D array. What happens is a txt file has the number of rows and colums that we read first to get the row and column amount for doing the 2D array. Then each index reads the next data element on the txt file and stores it at that index until all 50 000 integer values are stored. That WORKS fine.

Now I want to take that 2D array and store all the elements into a 1D array for faster processing later when looking for answers without using an array list or put them in order, which is fine,

int [][] data = null; 
int[] arrayCount = null;

for (int row = 0; row < numberOfRows; row++)
{
    for (int col = 0; col < numberOfCols; col++)  
    {
        data[row][col] = inputFile.nextInt();
    }
} 
//Doesn't Work gives me excpetion
data[0][0] = arrayCount[0];

I tried this in for loops but no matter what I get a NullPointerException

6
  • Which programming language? If Java, add the Java tag. If C++, add the C++ tag, etc... Edit your question to improve it. Commented Sep 21, 2014 at 7:14
  • You get a NullPointerException when you access a variable which was manually set to null? This is surprising. Or in other words .. check the variable arrayCount. Commented Sep 21, 2014 at 7:15
  • The 2d array was set to Null at the begging as well and that array works fine before trying to do this Commented Sep 21, 2014 at 7:22
  • You're "filling" that array (btw I don't know how data[row][col] = inputFile.nextInt(); can work on a null array... should also throw a NPE). arrayCount is also null but you're trying to access his first index here data[0][0] = arrayCount[0]. How should be data in there if this array is still null? Commented Sep 21, 2014 at 7:26
  • possible duplicate of How to convert a 1d array to 2d array? Commented Sep 21, 2014 at 7:30

1 Answer 1

2

You haven't initialized the data and arrayCount variables, initialize it as follows :

int[][] data = new int[numberOfRows][numberOfCols];
int[] arrayCount = new int[numberOfRows * numberOfCols];

In your case, to copy from 2D to 1D array you may use something like this :

    numberOfRows = data.length;
    if (numberOfRows > 0) {
        numberOfCols = data[0].length;
    } else {
        numberOfCols = 0;
    }

    System.out.println("numberOfRows : "+numberOfRows);
    System.out.println("numberOfCols : "+numberOfCols);

    for (int row = 0, count = 0; row < numberOfRows; row++) {
        for (int col = 0; col < numberOfCols; col++) {
            arrayCount[count] = data[row][col];
            count++;
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't matter, at arrayCount[count] = data[row][col] it still gives me a null pointer exception
It works! I don't understand why does it matter if both arrays are at null? Thanks a lot for your help!
@lighty sorry for the late reply. It's my pleasure to help you. Please this reference link Creating, Initializing, and Accessing an Array and What Does a Java Array Look Like in Memory, it may be very helpful to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.