NullPointerException with multi-dimentional array.

Hello everyone,

What im trying to do is that the MySQL class retrieves characters from a certain account id and inserts them into a multi-dimentional array. It does this inside the getCharacters function. But when i insert the values into the multi-dimentional array and run my application it give’s a null pointer exception on a certain line. This is the code:

public String[][] getCharacters(int accId) {
		String[][] characters = null;
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM `characters` WHERE `account_id`="+ accId);
			int i = 1;
			while(rs.next()) {
				String username = rs.getString("username");
				int level = rs.getInt("level");
				System.out.println("I: "+ i +" User:"+ username +", Lvl: "+ level);
				characters[i][0] = username;
				characters[i][1] = ""+ level;
				i++;
			}
			rs.close();
			st.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("[Login Server][Error]: Error with retrieving characters: "+ e.getMessage());
		} catch (Exception e) {
			System.out.println("");
		}
		return characters;
	}

NullPointerException happens on the following line: “characters[i ][0 ] = username;” (Note there are no spaces between the brackets but the forum views it as BB code)

Iv been stuck with this problem for 4 days now and im sirreusly stressing alot over it, iv asked a few application developers at my school if they could help out but they couldnt find the problem either. I hope someone know’s what im doing wrong here and if possible send me a link or type some text informing me on what i did wrong and what i could do to prevent this from happening again…

Thanks in advance :slight_smile:

You haven’t initialized your array.

String[][] array=new String[12][];
array[0]=new String[3];
or
String[][] array=new String[12][10];
or
String[][] array=new String[][]{{“one”,“two”},{“three”,“four”}};

But what if i have no limit to how big it can get?

The first array is the character, the array in the array is the character info so a player could have a bunch of diffrent characters. Well ill prob set a limit to it but i still would like to know how i could have an unlimited amount of value’s in the array (new String[][] doesnt work :frowning: )

Use an ArrayList.

ArrayList doesnt have multiple dimentions

When you know the limit in advance you can do something like this:
int limit1=…;
int limit2=…;
String[][] array=new String[limit1][limit2];
or
int limit1=…;
String[][] array=new String[limit1][];
int limit2=…;
array[…]=new String[limit2];

array lists:
ArrayList<ArrayList> arrayList=new ArrayList<>();
arrayList.add(new ArrayList<>());

Looking at OP’s original code, it seems like he only needs 2 strings per entry, so a ArrayList<String[]> would probably be the best.

yess.

It has only 2 string right now yes, but i’ll be expanding it alot in the future, im still in very low stages of my project :stuck_out_tongue:

Also, ill just try out ArrayList then, ill edit it if it works

You have to ask the question: which dimensions will need to be dynamic? You seem to be programming something that is TOO dynamic/flexible if you need an unlimited 2D array and it’s not map/level data.

Ye you might be right, iv been trying to make everything as dynamic as possible, maybe im trying to hard and its best to just limit certain things.

Also, a small eclipse problem (for people who use the eclipse IDE), when i type an object E.G: ArrayList. after the ‘.’ it should show a tool tip like feature with suggestions of possible functions inside that class but since i switched over to ubuntu it doesnt show anymore, anyone know where i can make it work again (i googled it but i didnt get any results, mainly because i cant think of a decent search query for my problem)

Init your array after execute query, so you can get number of rows of result and use it.

Try Googling on ‘eclipse intellisense not working ubuntu’.

Thanks alot got the eclipse thing working :slight_smile:

Also got my code working after i switched to ArrayList<string[]> thanks alot everyone responding quickly :slight_smile: i appriciate all the help.