Problems with getEnv(), help!

5 replies [Last post]
Offline
Joined: 10/24/2011

I posted this on the Moodle boards too but I'll repost this here in hopes of getting Jason's attention

 

I'm doing my Assignment 1 and I want my player character to shoot a projectile when the mouse is clicked. I did it like in the Scene Creator animation lab and as far as I can tell things match up so I don't see why it doesn't work...
One of the only differences I can see is that GameObject extends EnvObject instead of EnvNode but does that make a difference?

here's Doty (spoiler alert for my game, igloo projectiles >.>)

import env3d.EnvObject;
import java.util.ArrayList;
import org.lwjgl.input.Keyboard;

/**
* Doty is the character the Player controls. He can throw igloos.
*
* @author Aaron Cheung
* @version Feb 17/2012
*/
public class Doty extends GameObject
{

/**
* Constructor for objects of class Doty
*/
public Doty(double x, double y, double z)
{
// initialise instance variables
setX(x);
setY(y);
setZ(z);
setTexture("textures/doty.gif");
setScale(1);
}

/**
* Overriding move method
* Left Click = shoot
*/
public void move()
{
// HERE I GET java.lang.NullPointerException: null
// sad sad
if (getEnv().getMouseButtonClicked() == 0) {
shoot();
}
}

/**
* Doty can chuck Igloos from his position to stun foxes
*/
public void shoot()
{
Igloo i = new Igloo(getX(), getY()+1, getZ());
i.setRotateY(getRotateY());
i.setRotateXDir((int)(Math.random()));
i.setRotateYDir((int)(Math.random()));
i.setRotateZDir((int)(Math.random()));

// IF I COMMENT OUT getMouseButtonClicked above, this line gets the error instead:

// :( :(
env.addObject(i);
}
}

Offline
Joined: 12/13/2010
The university email system
The university email system is down for the weekend. So I couldn't get to my mail box. Sorry. Anyway, EnvNode is simply a high performance replacement for EnvObject. That is not your problem. The Null Pointer error is triggered by getEnv() returning null, sam error happens if you call the shoot() method and try to access the env object. You need to make sure the you call the setEnv() method so that the env field is set to reference the proper env object.
Offline
Joined: 10/24/2011
thanks

Thanks for that tip. It really helped.

I got this weird error when I tried to upload my game to the website. It was like NoSuchMethodFound for all the times I put env.getObjects(GameObject.class) and similar.

To get around this I rewrote a bunch of stuff so it would work in applet form, but I handed in the original code because the rewritten code is superfluous and awful and etc.

The two versions work in exactly the same way.

These problems led me to hand my files in a few minutes after midnight, and the applet even later. I hope that's ok since I now have a working applet >_>

Offline
Joined: 10/29/2013
I will use it like you said

I will use it like you said via a cloud app