0

I am working on a voxel engine, currently I am stuck on the creation of blocks. You can ignore the part about LWJGL/OpenGL, that is not the problem. When I try to create the blocks I get a bunch of java.lang.ArrayIndexOutOfBoundsException errors.

package LWJGL.TESTS.WORLS;

import java.util.Random;


public class Chunck {

private int x, y, z, id;

Chunck(int id, int x, int y, int z){
    this.x = x;
    this.y = y;
    this.z = z;
    this.id = id;
}

This is the part that doesn't work which no post have been able to answer the fix:

`   void loadChunck(){
    //x
    for(int x = 1; x < 16; x++){
        //z
        for(int z = 1; z < 16; z++){
            //y
            for(int y = 1; y < 128; y++){
                try{
                    Block[][][][][][] blockObject = new Block[16][16][128][0][0][0];
                    blockObject[x][y][z][this.x][this.y][this.z] = new Block(x, y, z, this.x, this.y, this.z, String.valueOf(x)+String.valueOf(y)+String.valueOf(z), this.id);
                }catch(java.lang.ArrayIndexOutOfBoundsException e){
                    e.printStackTrace();
                }

            }

        }

    }
}

}

This is the block class:

package LWJGL.TESTS.WORLS;

import org.lwjgl.opengl.GL11;

public class Block {

private int x, y, z, cx, cy, cz, cID;
String ID;

Block(int x, int y, int z, int cx, int cy, int cz, String ID, int cID){

    this.x = x;
    this.y = y;
    this.z = z;
    this.cx = cx;
    this.cy = cy;
    this.cz = cz;
    this.ID = ID;
    this.cID = cID;
    System.out.println("Block ID: " + ID);
    System.out.println("Block Chunck ID: " + cID);
    System.out.println("Block X: " + x);
    System.out.println("Block Chunck X: " + cx);
    System.out.println("Block Y: " + y);
    System.out.println("Block Chunck Y: " + cy);
    System.out.println("Block Z: " + z);
    System.out.println("Block Chunck Z: " + cz);
}
private Block[][][][][][] InitBlock(){


    return null;

}
public void render(){

    GL11.glBegin(GL11.GL_QUADS);    
        GL11.glColor3f(1.0f,1.0f,0.0f);           
        GL11.glVertex3f(this.x, this.y,-1*this.z);        
        GL11.glVertex3f(-1*this.x, this.y,-1*this.z);        
        GL11.glVertex3f(-1*this.x, this.y, this.z);
        GL11.glVertex3f(this.x, this.y, this.z);  
        GL11.glColor3f(this.x,0.5f,0.0f);            
        GL11.glVertex3f(this.x,-1*this.y, this.z);
        GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
        GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
        GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
        GL11.glColor3f(1.0f,0.0f,0.0f);
        GL11.glVertex3f(this.x, this.y, this.z);
        GL11.glVertex3f(-1*this.x, this.y, this.z);
        GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
        GL11.glVertex3f(this.x,-1*this.y, this.z);
        GL11.glColor3f(1.0f,1.0f,0.0f);
        GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
        GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
        GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
        GL11.glVertex3f(this.x, this.y,-1*this.z);
        GL11.glColor3f(0.0f,0.0f,1.0f);
        GL11.glVertex3f(-1*this.x, this.y, this.z);
        GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
        GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
        GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
        GL11.glColor3f(1.0f,0.0f,1.0f);
        GL11.glVertex3f(this.x, this.y,-1*this.z);
        GL11.glVertex3f(this.x, this.y, this.z);
        GL11.glVertex3f(this.x,-1*this.y, this.z);
        GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
    GL11.glEnd();    


}

}

This is the Saves class (ignore this):

package LWJGL.TESTS.WORLS;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Saves {

void Save(){

    PrintWriter writer = null;
    try {
        writer = new PrintWriter("data.txt", "UTF-8");
    } catch (FileNotFoundException | UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    writer.print("");
    writer.close();
}

void Load() throws FileNotFoundException{

    Scanner scanner = new Scanner(new File("data.txt"));
    while(scanner.hasNextInt()){
        System.out.println(scanner.nextInt());

    }


}

}

And finally this is the main class, sorry if this post is really long:

package LWJGL.TESTS.WORLS;

import java.io.FileNotFoundException;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
    Saves load = new Saves();
    Chunck chunck = new Chunck(1, 0, 0, 0);
    chunck.loadChunck();
    load.Save();
    load.Load();

}

}
3
  • 2
    An array with these dimensions: new Block[16][16][128][0][0][0], will have zero room for any elements, and you can never assign anything into it. What were you trying to do? Commented Jun 7, 2015 at 1:44
  • Why do you create an array (one which can't actually hold anything), assign one value to it, and then let it fall out of scope without ever using it? Unless I'm missing something, it's really not clear what you're even trying to do. Commented Jun 7, 2015 at 1:52
  • I believe the [16][16][128] is the blocks location within the chunk and the [0][0][0] is the chunks location which makes this even stranger because even if the array was non empty the values would all be this.x, this.y, this.z but he directly sets the chunks coordinates on the block anyway. some clarification on what you are trying to accomplish would be nice. Commented Jun 7, 2015 at 2:05

1 Answer 1

1

Either I am tired or the answer is really simple. Your multidimensional array is instantiated having zero length for the last 3 dimensions. And yet at the very next step you are trying to access data from those dimensions. Let me try to simplify your case. What you are doing is equivalent to

Block[] blockObject = new Block[0];
blockObject[x] = new Block(...);

In this case access to blockObject[x] will trigger an exception as the array doesn't have an element in that slot no matter what x is. You array has zero length.

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

1 Comment

Thank you for the basic concept. It led me to mess around with some numbers and get this: try{ Block[][][][][][] blockObject = new Block[16][16][128][16][16][16]; blockObject[x][y][z][this.x][this.y][this.z] = new Block(x, y, z, this.x, this.y, this.z, String.valueOf(x)+String.valueOf(y)+String.valueOf(z), this.id); }catch(java.lang.ArrayIndexOutOfBoundsException e){ e.printStackTrace(); } This completely fixed the problem.

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.