Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NullPointerException with multi-dimentional array.  (Read 1040 times)
0 Members and 1 Guest are viewing this topic.
Offline Mikelmao

Senior Newbie





« Posted 2011-11-24 12:36:07 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
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 Smiley
Offline theagentd
« Reply #1 - Posted 2011-11-24 13:02:51 »

You haven't initialized your array.

Myomyomyo.
Offline Chromanoid

Junior Member


Medals: 3



« Reply #2 - Posted 2011-11-24 13:04:27 »

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"}};
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mikelmao

Senior Newbie





« Reply #3 - Posted 2011-11-24 13:09:22 »

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 Sad )
Offline theagentd
« Reply #4 - Posted 2011-11-24 13:11:18 »

Use an ArrayList.

Myomyomyo.
Offline Mikelmao

Senior Newbie





« Reply #5 - Posted 2011-11-24 13:13:32 »

ArrayList doesnt have multiple dimentions
Offline Chromanoid

Junior Member


Medals: 3



« Reply #6 - Posted 2011-11-24 13:14:48 »

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<String>> arrayList=new ArrayList<>();
arrayList.add(new ArrayList<>());
Offline theagentd
« Reply #7 - Posted 2011-11-24 13:16:01 »

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.

Myomyomyo.
Offline Chromanoid

Junior Member


Medals: 3



« Reply #8 - Posted 2011-11-24 13:16:33 »

yess.
Offline Mikelmao

Senior Newbie





« Reply #9 - Posted 2011-11-24 13:20:13 »

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 Tongue

Also, ill just try out ArrayList then, ill edit it if it works
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2011-11-24 13:23:16 »

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.

Myomyomyo.
Offline Mikelmao

Senior Newbie





« Reply #11 - Posted 2011-11-24 13:25:09 »

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.
Offline Mikelmao

Senior Newbie





« Reply #12 - Posted 2011-11-24 13:37:27 »

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)
Offline ReBirth
« Reply #13 - Posted 2011-11-24 13:38:10 »

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

Offline Jack_E

Senior Newbie





« Reply #14 - Posted 2011-11-24 13:51:35 »

Quote
Also, a small eclipse problem (for people who use the eclipse IDE), ..., (i googled it but i didnt get any results, mainly because i cant think of a decent search query for my problem)
Try Googling on 'eclipse intellisense not working ubuntu'.
Offline Mikelmao

Senior Newbie





« Reply #15 - Posted 2011-11-24 14:46:35 »

Thanks alot got the eclipse thing working Smiley

Also got my code working after i switched to ArrayList<string[]> thanks alot everyone responding quickly Smiley i appriciate all the help.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!