0

I am making a Realm plugin for my server, and I am using a multiarray to detect the location of users portals, below is the code:

public static String[][][] realms;

@EventHandler
public void onPlayerInteract(final PlayerInteractEvent event) throws Exception {
  if( event.getMaterial() == Material.NETHER_STAR ) {
    int x = (int) event.getClickedBlock().getX();
    int y = (int) event.getClickedBlock().getY();
    int z = (int) event.getClickedBlock().getZ();
**  realms[x][y][z] = event.getPlayer().getName();
    createPortal();
  }
}

I get a NullPointerException at the line with the '**', can someone please explain what I am doing wrong? I have googled 'java multiarrays', and they all seem to work the same way.

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
MRVDOG
  • 1,717
  • 2
  • 13
  • 20
  • 3
    You have not initialized the array(s) in any way, shape, or form. – Brian Roach Aug 12 '13 at 21:55
  • Is 'realms' ever initialized? If yes, how? – Evgeny Aug 12 '13 at 21:56
  • http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Brian Roach Aug 12 '13 at 21:56
  • 1
    Multidimensional String arrays - please not. Java has Collection classses. Use them. – Martin Schröder Aug 12 '13 at 22:04
  • 3
    Answering this question properly would literally be rehashing the tutorial on using arrays in Java. Arrays in Java are objects; you have a variable that can hold an array that holds arrays that hold arrays of String objects. Arrays are also fixed length, and are instantiated to a given size. Aside from that, you probably would be better served by a dynamic Collection as noted above. – Brian Roach Aug 12 '13 at 22:08
  • okay then, how can I create a collection, I need x,y,z = playerName, that way the server knows where that players realm portal is located – MRVDOG Aug 12 '13 at 22:15
  • @MRVDOG: Please make this a different question. I suggest to read some tutorial on Java Collections first, though. – Martin Schröder Aug 13 '13 at 11:30
  • All this is too confusing, I'm just going to use my website and mysql database to add/get/remove realm portal locations – MRVDOG Aug 13 '13 at 12:56
  • I got it working through my website :), I have another problem though if you can help: http://stackoverflow.com/questions/18212244/bukkit-teleport-nullpointerexception – MRVDOG Aug 13 '13 at 14:56

4 Answers4

2

You are getting a null pointer exception because you haven't initialized the array.

You can initialize the array like this:

String string[][][] = new String[3][3][3];

you need to know what would be the length of the arrays, because if you try to access or save a value with an index that doesn't exists you are going to get a IndexArrayOutOfBounds exception

jsedano
  • 4,088
  • 2
  • 20
  • 31
1

Array life cycle consists of three things 1. Array type declaration 2. Array initialization 3. Array utilization

But you have not declared an array rather you have just declared an variable and informed compiler that you wish to have an 3- dimensional array named "realms" of type "String" but you forgot to allocate that array in memory and there after relating its pointer to the variable. Sample for Array declaration

public static String[][][] realms; /// array variable declared
/*now allocate the memory and point it to the array variable*/
realms = new String [<length index>][<breath index>][<height index>]

try this (replace length index, breath index, height index as per you)

public static String[][][] realms;

@EventHandler
public void onPlayerInteract(final PlayerInteractEvent event) throws Exception {

    realms = new String [<length index>][<breath index>][<height index>]

  if( event.getMaterial() == Material.NETHER_STAR ) {
    int x = (int) event.getClickedBlock().getX();
    int y = (int) event.getClickedBlock().getY();
    int z = (int) event.getClickedBlock().getZ();
**  realms[x][y][z] = event.getPlayer().getName();
    createPortal();
  }
}
Roshan
  • 50
  • 1
  • 8
  • problem with that is, everytime a player interacts with something, it will overwrite the Array with a new one, I don't want that! – MRVDOG Aug 12 '13 at 22:25
  • then just put this line before the **function call** in the **calling function** realms = new String [][][] – Roshan Aug 12 '13 at 22:36
  • It won't work, someone already said I can't have negatives in an array, and with it being coordinates, that's impossible to avoid – MRVDOG Aug 12 '13 at 22:44
  • @MRVDOG then just put this line before the **function call** in the **calling function** realms = new String [][][] example 'void write() { srt[1][1]="sa"; } void init() { srt= new String[2][2]; write(); }' – Roshan Aug 12 '13 at 22:45
  • O yes you can not have a negative index example `realm[1][-2][3] // this is invalid statement as it has a negative index` and its true that gps co-ordinates have negative numbers – Roshan Aug 12 '13 at 22:52
  • @MRVDOG Why don't you use a 2 - dimentional array on row 1 hold the co-ordinates seperated by "," and in the row 2 hold the name of the player Example `public static String[][] realms; public static int count=0; @EventHandler public void onPlayerInteract(final PlayerInteractEvent event) throws Exception { if( event.getMaterial() == Material.NETHER_STAR ) { String cordinate=event.getClickedBlock().getX()+","+event.getClickedBlock().getY()+","+event.getClickedBlock().getZ(); ** realms[0][count] = cordinate;realms[1][count] = event.getPlayer().getName();count++; createPortal(); } }` – Roshan Aug 12 '13 at 22:59
1

Judging by your comments in other answers, I don't think a multidimensional array is the data structure you want. You suggest your indices are potentially unbounded (or at least very large) and can be negative, and will presumably only be sparsely filled. I think you therefore want an Octree implementation to store your data in. There's one available at http://www.java-gaming.org/index.php?topic=27334.0 - I've never used it, but have used the Quadtree implementation (basically the same thing with 2 dimensions rather than 3) successfully in the past.

Jules
  • 14,841
  • 9
  • 83
  • 130
  • There has to be a way to do it without having to add other libraries! – MRVDOG Aug 12 '13 at 22:32
  • Not easily, no. Java doesn't have any built-in data structures that can be referenced with three dimensions other than the multidimensional array, which requires a fixed size, cannot have negative indices, and would consume a large amount of memory if you had nontrivial size for the three dimensions. – Jules Aug 12 '13 at 22:34
0

Here's an example of how to initialize a multi array:

Initialize 2D array

The bottom of the accepted answer shows the syntax:

String[][] table = new String[5][5];
Community
  • 1
  • 1
MattG
  • 1,416
  • 2
  • 13
  • 31