This may be a stupid question, but I'm getting sick of this. How do you properly initialize a class object so you can access methods and variables in it? Here's my code:
public class GenChunk {
Chunk c;
VBO vbo;
int worldSize = 64;
int var1 = 16; //x
int var2 = 16; //z
int var3 = 16; //y
public GenChunk(){
gen();
}
public void gen(){
c = new Chunk();
if(c.chunkChanged == false){
for(int i = 0; i < worldSize; i++){
newChunk();
}
}
}
The line c = new Chunk()
is causing the stack overflow error., I realize why (because its creating new instances of it and taking up more and more memory), but as soon as I initialize it in the constructor or just do Chunk c = new Chunk()
, at the top below public class GenChunk {
I get another stack overflow error! If I don't initalize the class, I get a null pointer (obviously). What is wrong with my code? I must be overlooking something, but I can't figure it out!
Here is my Chunk class, it has a few empty methods:
public class Chunk {
public boolean chunkChanged = false;
boolean enteredGame = true;
public int chunkID = 0;
Player player;
float var1;
float var2;
float var3;
public Chunk(){
update();
}
private void update(){
setUp();
}
private void setUp(){
if(enteredGame){
new GenChunk();
}
}
private void checkChunkRebuild(int id){
}
private void rebuildOnPlayerChange(int id){
if(enteredGame && chunkChanged == true){
}
}
private float getPlayerX(){
return player.var1;
}
private float getPlayerZ(){
return player.var2;
}
private float getPlayerY(){
return player.var3;
}
}
Your Chunk class is creating a new instance of GenChunk, which in turn is creating a new instance of the Chunk class, creating an infinite loop until the stack overflow occurs.
private void setUp(){
if(enteredGame){
new GenChunk();
}
}
is where it starts, and it continues at
public void gen(){
c = new Chunk();
if(c.chunkChanged == false){
for(int i = 0; i < worldSize; i++){
newChunk();
}
}
}
which is called in the GenChunk constructor.