views:

283

answers:

2

I am currently creating a java application in which I have a 2d array which I want to get some data into.

I am creating the 2d array as such

String[][] addressData;

and then when I am trying to put data in I am using reference the exact position in the 2d array I want to enter the data into e.g

addressData[0][0] = "String Data";

The program compiles yet when I run I get a NullPointerException error. Am I using the wrong method to enter data into this 2d array?

+10  A: 

String[][] addressData - this is just declaration, you have to create actual object String[][] addressData = new String[size][size];

Btw, There is no 2d arrays in java String[][] is an array of arrays of strings

Luno
Thanks for the reply, when I set a size to the 2d array it will work yet I don't want a limit on the size of the 2d array. It could range for just a few piece of data to thousands so I don't want a limit or null values. Is there any way around this?
Student01
If you don't want to fix the size, make it an array of Lists instead of a 2D-array.
ammoQ
@Student01: The only way around it is not using arrays. You can use a list of lists of strings.
sepp2k
As I wrote, it's not 2d array but array of arrays. You have to give a size when creating an array, so you have to do:String[][] array = new String[Size][]; - it'll create an array of String[] type with length = size filled with nulls, but you can put arrays with different length into it, for example:array[0] = new String[10];array[1] = new String[50];etc
Luno
ammoQ - you Can't make array of lists ( at least when using generics ). It's imposible to make an array of generic types
Luno
ok thanks for the help, I am going to try implementing a lit of string instead. Cheers
Student01
A: 

How to set data and get data? from a string 2d array what methods are there?

Rida Mayram