Problem creating a Ray

I’m trying to make a ray upon the user left clicking. I’m using the code from the “Hello Pick” tutorials as a template to start from. Here is my code. Line 4 is where I hit my error and it crashes. I’m a newbie so you’ll have to bare with me.



[snippet id=“29”]

It sure helps if you tell the forum what the error actually is. It might be that “interactable” is null?

Good point, my bad. xD



Here is the error:



SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

[java]Main cc = new Main();[/java]



dont do that

@wezrule said:
[java]Main cc = new Main();[/java]

dont do that


Okay, what might be a right course of action? Just using "Ray ray = new Ray(cam.getLocation(), cam.getDirection());" was throwing the same error in the same spot. I thought it had something to do with the fact this code was outside my main class so that's why I made an object of my main class and called a camera from it. I'm just trying to put everything that handles my interactions in a different class form my main because my main class is already getting pretty clustered.

This is a java related error, i think you should learn all the programming constructs of java first. You need to either pass a reference of the SimpleApplication instance to your new class’ constructor or create a static variable referencing your SimpleApplication instance; there are also other ways as well



There are a number of posts similar to this around these forums

@wezrule said:
This is a java related error, i think you should learn all the programming constructs of java first. You need to either pass a reference of the SimpleApplication instance to your new class' constructor or create a static variable referencing your SimpleApplication instance; there are also other ways as well

There are a number of posts similar to this around these forums



Okay, thanks a ton. I'm a 'learn on the fly' kind of person that learns best by doing. I can only retain so much by reading books and browsing forums, some times you just need to jump in and go at it ya know?

yh i know, but it makes sense to learn the basics first, rather than advanced. It will make it a lot more enjoyable and easier to debug errors in the long run

1 Like

Okay, I have a feeling I’m trying to do what you suggest all wrong. Could you perhaps give me a link to the oracle site or something that tells me about what your taking about?

If the error is indeed in this line:

[java]

Ray ray = new Ray(cc.getCamera().getLocation(), cc.getCamera().getDirection())

[/java]

Then since cc is clearly not null ( you just created it ), the null object MUST be the camera returned from cc. Your code bit gives few clues, but my guess is that cc is a JME application object? If so, you will need to run cc.start(), in order to initialize all objects, including the camera, explaining why the returned camera is null…

I used the 'Hello Collision" tutorial to set up the ‘player’ like so.



Main.java

[snippet id=“30”]



Now I’m trying to make it so the character can ‘attack’, and for that I will need a ray for a line of sight right? That’s why I made the above code in a different class (Called Interaction) to prevent clustering up my main class.

the way i would do it is:



Main.java

[java]

private static Main myApp;



public void simpleInit() {

myApp = this;

}



public static Main getApp() {

return myApp;

}

[/java]



AnotherClass.java

[java]

private Main myApp = Main.getApp();



//then you can do stuff like

private Cam cam = myApp.getCamera();

private Node rootNode = myApp.getRootNode();



public void interAction(){

CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

interactable.collideWith(ray, results);

CollisionResult closest = results.getClosestCollision();

float dist = closest.getDistance();

Vector3f pt = closest.getContactPoint();

String hit = closest.getGeometry().getName();

float dis2 = results.getCollision(2).getDistance();

Vector3f pt2 = results.getCollision(2).getContactPoint();

String hit2 = results.getCollision(2).getGeometry().getName();



if (results.size() > 0) {



mark.setLocalTranslation(closest.getContactPoint());

rootNode.attachChild(mark);

} else {

// No hits? Then remove the red mark.

rootNode.detachChild(mark);



}



[/java]

Ohhhh, okay! That makes sense. Thanks for the information. This will help me a lot in days to come. xD