0

I know this is a very basic question, but I'm just new to programming and I think I'am struggling with a very simple issue... but here we go :p

In my app I have an array of arrays like this storing data retrieved from a mysql database.

data_array[][]

0001 | data1 | data2 | data3 | data4 | data5
0002 | data1 | data2 | data3 | data4 | data5
...
... and so on. 

Now in my app, I need to retrieve for each of the rows the first of the colums, ending with something like :

array { 0001, 0002}

I know doing ' for iterations ' I can retrieve all the data:

public String[] itarray{
int cols = 5;
String[] xFINAL;
for (int i=0 ; i < data_array; i++) {
    for (int j=0; j < cols; j++){
        xFINAL = data_array[i];
        System.out.println("1" + data_array[i][0]);

    }

System.out.println(" ");
} return xFINAL;}

But how can I get the first column for each of the rows? I tried with something like:

int j=0;
for (int i = 0; i < data_array ; i++){
 xFINAL[j] = data_array [i][0];
j++;
}

But it's giving me a null pointer. How could I do this please?

Thanks in advance.

2
  • 2
    Just a little tip - You don't need that "j" there (for the last code block you have there). Just use the i (it increments by 1 as well). Commented Mar 24, 2013 at 16:03
  • 4
    You get a null pointer exception because xFinal is not initialized. Use xFinal = new String[data_array.length] first. Commented Mar 24, 2013 at 16:04

1 Answer 1

1
String[] xFINAL = new String[data_array.length];   

int k =0;
for (int i=0 ; i < data_array; i++) {
    array[k++] = data_array[i][0];


}
Sign up to request clarification or add additional context in comments.

1 Comment

had to do a little change here: for (int i=0 ; i < data_array.length; i++) . But its working now. Thanks a lot all of you guys!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.