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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Which programming language? If Java, add the Java tag. If C++, add the C++ tag, etc... Edit your question to improve it. – Basile Starynkevitch Sep 21 '14 at 07: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`. – Tom Sep 21 '14 at 07:15
  • The 2d array was set to Null at the begging as well and that array works fine before trying to do this –  Sep 21 '14 at 07: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? – Tom Sep 21 '14 at 07:26
  • possible duplicate of [How to convert a 1d array to 2d array?](http://stackoverflow.com/questions/5134555/how-to-convert-a-1d-array-to-2d-array) – Kick Buttowski Sep 21 '14 at 07:30
  • @Kick no its opposite ab it more awkward to do –  Sep 21 '14 at 08:41

1 Answers1

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++;
        }
    }
Visruth
  • 3,430
  • 35
  • 48
  • Doesn't matter, at arrayCount[count] = data[row][col] it still gives me a null pointer exception –  Sep 21 '14 at 08:56
  • It works! I don't understand why does it matter if both arrays are at null? Thanks a lot for your help! –  Sep 21 '14 at 10:12
  • @lighty sorry for the late reply. It's my pleasure to help you. Please this reference link [Creating, Initializing, and Accessing an Array](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) and [What Does a Java Array Look Like in Memory](http://java.dzone.com/articles/what-does-java-array-look), it may be very helpful to you. – Visruth Sep 23 '14 at 18:10