Hi,
First post here and pretty new to Java, so thanks in advance for your help in located what I'm sure is going to be a simple mistake. I am trying to write a basic function that loads a .txt file and turns it into objects with coordinates on the map. I have tested nearly all of the function and it appears to function properly, but when I add the "final piece" the switch statement that assigns objects my draw level function keeps getting a null pointer error which has to mean that my switch statement isn't setting all the object array properly.
// set the number of objects back to zero numberofobjects = 0; // reads the text in the file based on what level the player is in file = Gdx.files.internal("levels/level" + level + ".txt"); // Use a string to store the file information to prevent overhead of streaming the file multiple times during this function String fileinfo = file.readString(); // searches through the string to find out the number of objects needed for(int objectcount = 0; objectcount < fileinfo.length(); objectcount++){ if(fileinfo.codePointAt(objectcount) != 0) numberofobjects++; // if its an object then count up. } starttime = System.currentTimeMillis(); acceleration = new Vector2(0, 0); if(level == 1){ // set X and Y to the top left of the screen int x = 0; int y = Gdx.graphics.getHeight()-64; // resize the objects array to the number of objects in the current level objects = new Object[numberofobjects]; // integer to track how many objects have been set so far int currentobject = 0; // loop through the entire string which contains all object information for the level for(int loop = 0; loop < fileinfo.length(); loop++){ char temp = fileinfo.charAt(loop); switch(temp){ case 0: break; // blank space case 1: objects[currentobject] = new Object(0, new Vector2(x, y)); currentobject++; break; // tree case 2: break; case 3: objects[currentobject] = new Object(1, new Vector2(x, y)); currentobject++; break; // rock case 4: objects[currentobject] = new Object(3, new Vector2(x, y)); currentobject++; break; // water case 'X': position = new Vector2(x, y); break; // smiley case 'O': objects[currentobject] = new Object(2, new Vector2(x, y)); currentobject++; break; // star } x += 64; if(x == Gdx.graphics.getWidth() || x > Gdx.graphics.getWidth()-64){ x = 0; y -= 64; } }
Exact error is java.lang.NullPointerException and it occurs at
public void drawlevel(){ drawbackground(grasstile); currenttime = System.currentTimeMillis(); batch.begin(); for(int loopcontrol = 0; loopcontrol < numberofobjects; loopcontrol++){ switch(objects[loopcontrol].type){ case 0: batch.draw(treetile, objects[loopcontrol].position.x, objects[loopcontrol].position.y); break; case 1: batch.draw(rocktile, objects[loopcontrol].position.x, objects[loopcontrol].position.y); break; case 2: batch.draw(startile, objects[loopcontrol].position.x, objects[loopcontrol].position.y); break; case 3: batch.draw(watertile, objects[loopcontrol].position.x, objects[loopcontrol].position.y); break; } } font.draw(batch, ""+((currenttime - starttime)/1000f)+" seconds", 10, Gdx.graphics.getHeight()-10); batch.end(); }
So the error would probably be in the assignment (my first code) since this draw function is looping through all objects but finds a nullpointer or what I'm assuming is an object with no value.
Thanks again to anyone who can spot my error of my ways.