Using classes

Hello all,

I don’t know why, but I am having trouble using classes in JMonkey.

For example, I created a class called Cannons (extends the main class). And in this class I have ONLY a function called make_cannons().

Then I go to the main class (that has the main() function), (extends “SimpleApplication”). And in the simpleInitApp() function I write the following code:
[java]Cannons cannon = new Cannons();
cannon.make_cannons();[/java]

The code gives me no error before running. But when I run the application, and press the button that triggers the above code, JMonkey gives me a “NullPointerException”.

What am I doing wrong?

@M-ASH said: Hello all,

I don’t know why, but I am having trouble using classes in JMonkey.

For example, I created a class called Cannons (extends the main class). And in this class I have ONLY a function called make_cannons().

Then I go to the main class (that has the main() function), (extends “SimpleApplication”). And in the simpleInitApp() function I write the following code:
[java]Cannons cannon = new Cannons();
cannon.make_cannons();[/java]

The code gives me no error before running. But when I run the application, and press the button that triggers the above code, JMonkey gives me a “NullPointerException”.

What am I doing wrong?

You are trying to write a Java game without having learned Java. This is not really possible.

You probably want to run through some Java tutorials until you are familiar enough in the language to write programs… then come back to a more complicated subject like 3D gaming.

@pspeed, I don’t think I really would have gotten into something as 3D gaming without learning Java. I do know that this is supposed to be a very simple topic.

I have called and used classes before using other programs. And I tried so many times to get it right on JMonkey. But I am really having trouble with calling classes here. I just can’t get what I am missing here.

So if you could help, your help would be greatly appreciated.

Thanks. :slight_smile:

You haven’t provided any of the information necessary for us to help you. However, you have stated that you have “cannons that extends the main class” which implies that you are having multiple applications in your application. I can’t see the code so I can’t be sure.

NPEs are the single easiest exceptions to track down. I automatically assume that anyone who can’t do it must be very new to Java. Since you didn’t even provide is the line that the crash happens that indicates that you don’t really understand what an NPE is and I sort of stand by my original guess that you don’t know Java very well.

This is the call in the main class that triggers make_cannons():
[java]if (name.equals(“shoot”) && !keyPressed ) {
Cannon cannon = new Cannon();
cannon.make_cannons();
}[/java]

And this is the function in the class Cannon:
[java]
public void make_cannons() {
Material stone_mat;
RigidBodyControl ball_phy;

    stone_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //THE ERROR HAPPENS HERE!
    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key2.setGenerateMips(true);
    Texture tex2 = assetManager.loadTexture(key2);
    stone_mat.setTexture("ColorMap", tex2);

/** Create a cannon ball geometry and attach to scene graph. */
    Geometry ball_geo = new Geometry("cannon ball", sphere);
    ball_geo.setMaterial(stone_mat);
    ball_geo.scale(2);
    rootNode.attachChild(ball_geo);
/** Position the cannon ball  */
    ball_geo.setLocalTranslation(Ship.getLocalTranslation());
/** Make the ball physcial with a mass > 0.0f */
    ball_phy = new RigidBodyControl(1f);
/** Add physical ball to physics space. */
    ball_geo.addControl(ball_phy);
    bulletAppState.getPhysicsSpace().add(ball_phy);
/** Accelerate the physical ball to shoot it. */
    ball_phy.setLinearVelocity(Ship.getLocalTranslation().mult(0.5f).add(0f, 0f, 0.5f));

    ////

    rootNode.attachChild(explosion);
    explosion.setLocalTranslation(Ship.getLocalTranslation().mult(2).add(0f, 0f, 0.5f));
    
    Material explosionMat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
    explosionMat.setTexture("Texture", assetManager.loadTexture("Effects/Smoke/Smoke.png"));
    explosion.setMaterial(explosionMat);
    explosion.setImagesX(1);
    explosion.setImagesY(1);
    explosion.setLocalTranslation(Ship.getLocalTranslation());
    rootNode.attachChild(explosion); // attach one or more emitters to any node

    explosion.setStartSize(50);
    explosion.emitAllParticles();
    explosion.killAllParticles();

} [/java]

The error first happens at line#5. What is the problem?

re: stone_mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”); //THE ERROR HAPPENS HERE!

No, it doesn’t. But that’s probably enough to guess where. assetManager is probably null and the NPE is probably happening inside of Material or deeper.

Just because your cannons extend application doesn’t make it an application. Your application shouldn’t have 500 different applications in it. Just one. Your cannon, despite extending your main class (ugh) is not an application. It doesn’t get initialized like an application. It was never started, etc… So all of its fields are still null.

This is a fundamental Java misunderstanding. You claim to know Java well but any of us can see better and you do yourself no favors by pretending.

We’ve been around this block about a millions times now. Let me see if I can guess your thought process from having interacted with 500+ other Java-beginners trying to do this:
me: “Why does your cannon class extend your main class? How many applications do you want in your application?”
you: “So it can access the asset manager and stuff.”
me: “But… why not just pass those things you need to the other class instead of having many applications in your application?”
you: “How do I do that?”

QED

What’s the exception? What lines in the stack trace correspond to what lines in the above code?

This really is very basic debugging…

Thank you for your time. I appreciate it.

It worked. Using the class constructor was far out of my head for some reason. Thanks again.

1 Like