Null pointer Exception help :o?

Started by qcrist, August 22, 2011, 05:14:11 AM

Previous topic - Next topic

qcrist

Don't really get why it says nullpointer exception. Nothing I gave it is null.

Any idea what is wrong?

Exception in thread "main" java.lang.NullPointerException
at com.threed.jpct.Object3D.checkForCollisionSpherical(Unknown Source)
at Game3D$Gravity.doGravity(Game3D.java:266)
at Game3D.loop(Game3D.java:201)
at Game3D.<init>(Game3D.java:62)
at Game3D.main(Game3D.java:41)


btw: Line 266 is the last line besides the "}"s.

import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.util.ArrayList;
import java.util.List;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Loader;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.SkyBox;

public class Game3D {
private World world;
private SkyBox skybox;
private FrameBuffer buffer;
private Object3D box;
private Camera camera;
private Robot robot;
private int displayWidth,displayHeight;
private final String IMAGES = "img/";
private final String MESHES = "mesh/";
private boolean done = false;
private Gravity gravity = new Gravity();
private float a;

public static void main(String[] args) throws Exception {
new Game3D();
}

public void addTexture(String name,String file)
{
TextureManager.getInstance().addTexture(name, new Texture(file));
}

public Game3D() throws Exception
{

world = new World();
camera = world.getCamera();
DisplayMode dm = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode();
displayWidth=dm.getWidth();
displayHeight=dm.getHeight();
robot = new Robot();
buffer = new FrameBuffer(displayWidth,displayHeight, FrameBuffer.SAMPLINGMODE_NORMAL);
makeSky(IMAGES+"sky/",".jpg");
makeObjects();
generalConfig();
loop();
buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
buffer.dispose();
System.exit(0);
}

public int roundToNearest2Pow(int num)
{
double p;
for (int i=1;i>0;i++)
{
p = Math.pow(2, i);
if (p>num)
return (int)Math.pow(2,i-1);
}
return 256;
}

public void generalConfig() throws LWJGLException
{

camera.setPosition(0,-20,0);
SimpleVector look = camera.getPosition();
look.add(new SimpleVector(0,0,0));
camera.lookAt(look);
world.setAmbientLight(128,128,128);
buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
buffer.enableRenderer(IRenderer.RENDERER_OPENGL, IRenderer.MODE_OPENGL);
Display.setFullscreen(true);
Keyboard.create();
Mouse.create();
Mouse.setGrabbed(true);
int numberOfProcs = Runtime.getRuntime().availableProcessors();
Config.useMultipleThreads = numberOfProcs > 1;
Config.useMultiThreadedBlitting = numberOfProcs > 1;
Config.loadBalancingStrategy = 1;
Config.maxNumberOfCores = numberOfProcs;
}

public void makeSky(String dir,String extention)
{
System.out.println("Loading SkyBox from "+dir);
if (extention.charAt(0)!='.')
extention = "."+extention;
String sides[] = {"left","front","right","back","up","down"};
for (String side:sides)
addTexture(side,dir+side+extention);
skybox = new SkyBox(1000f);
skybox.compile();
}

public int getY(int x,int z)
{
if (x==2)
return 1;
if (z==2)
return 1;
if (x==4)
return 1;
if (z==4)
return 1;
return 0;
}

public void makeObjects()
{
Object3D t;
Object3D boxMesh = loadModel(MESHES+"cube.3ds",IMAGES+"box.png");
boxMesh.setOrigin(new SimpleVector(0,0,0));
boxMesh.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS | Object3D.COLLISION_CHECK_SELF);
int v[] = {-4,-2,0,2,4,6,8,10};
for (int x:v)
for (int z:v)
{
t = boxMesh.cloneObject();
t.translate(x,getY(x,z),z);
t.build();
world.addObject(t);
}
box = boxMesh.cloneObject();
box.translate(0,-10,0);
box.build();
gravity.addObject(boxMesh, 2);
world.addObject(box);
}

public void mouseEvents()
{
Mouse.poll();
Matrix rot = world.getCamera().getBack();
int dx = Mouse.getDX();
int dy = Mouse.getDY();
float ts = 0.2f;
float tsy;
ts = dx / 500f;
tsy = dy / 500f;
if (a+tsy<1.5 && a+tsy>-1.5)
{
rot.rotateX(tsy);
a+=tsy;
}
rot.rotateAxis(rot.getYAxis(), ts);
robot.mouseMove((displayWidth/2), (displayHeight/2));
}

public void render()
{
buffer.clear(java.awt.Color.BLACK);
skybox.render(world, buffer);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
}

public void keyboardEvents()
{
Keyboard.poll();
if (Keyboard.isKeyDown(Keyboard.KEY_UP))
{
camera.moveCamera(Camera.CAMERA_MOVEIN, 0.1f);
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
{
camera.moveCamera(Camera.CAMERA_MOVEOUT, 0.1f);
}
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
world.getCamera().moveCamera(Camera.CAMERA_MOVELEFT, 0.1f);
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
world.getCamera().moveCamera(Camera.CAMERA_MOVERIGHT, 0.1f);
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
done=true;
}

public void loop() throws InterruptedException
{
while (!done)
{
gravity.doGravity();
render();
mouseEvents();
keyboardEvents();
Thread.sleep(15);
buffer.display(null);
}
}

public Object3D loadModel(String filename, String image) {
if (!TextureManager.getInstance().containsTexture(image))
TextureManager.getInstance().addTexture(image, new Texture(image));
Object3D[] model = Loader.load3DS(filename, 1f);
Object3D o3d = new Object3D(0);
Object3D temp = null;
for (int i = 0; i < model.length; i++) {
temp = model[i];
temp.setCenter(SimpleVector.ORIGIN);
temp.rotateX((float)( -.5*Math.PI));
temp.rotateMesh();
temp.setRotationMatrix(new Matrix());
o3d = Object3D.mergeObjects(o3d, temp);
o3d.build();
}
o3d.setTexture(image);
o3d.build();
return o3d;
}

public class Gravity {

private final float GRAVITY = 0.3f;
private final float LINE_OF_DEATH = 1000;

List<Object3D> objects = new ArrayList<Object3D>();
List<Integer> radius = new ArrayList<Integer>();
List<Integer> fallDistance = new ArrayList<Integer>();

public void addObject(Object3D obj,int r)
{
objects.add(obj);
radius.add(r);
fallDistance.add(0);
}

public void removeObject(Object3D obj)
{
fallDistance.remove(objects.lastIndexOf(obj));
radius.remove(objects.lastIndexOf(obj));
objects.remove(objects.lastIndexOf(obj));
}

public void doGravity()
{
for (int i=0;i<objects.size();i++)
{
fallDistance.set(i, fallDistance.get(i)+1);
Object3D obj = objects.get(i);
if (obj.getTranslation().y>LINE_OF_DEATH)
{
removeObject(obj);
continue;
}
for (int f=0;f<fallDistance.get(i);f++)
{
obj.translate(obj.checkForCollisionSpherical(new SimpleVector(0,GRAVITY,0), radius.get(i)));
}
}
}
}
}


Gravity class is not finished yet. I am aware that it probably won't work as of now. I just cant test. :[




Reason for editing: Wrong error message? (Wonder how I did that :o)

EgonOlsen

That's because you don't add boxMesh to the world. Your can't do a collision detection between objects that don't belong to a world, because they don't know each other. I agree that a null pointer isn't a good error message in that case. I'll add a check and a proper error message in the next version.

qcrist

Oh ,that isnt supposed to be boxmesh :D

Thanks for the help  :D