Multiple classes

Hey, i’m new to JME3 as well as Java.I get a nullpointerexception when i try run a method from one class in the main class.



[java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import Gui.Menus;

import com.jme3.scene.Spatial;



public class Main extends SimpleApplication {



public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

Menus menu = new Menus();



menu.Main(); // says something is wrong with this?

}



@Override

public void simpleUpdate(float tpf) {

//TODO: add update code

}



@Override

public void simpleRender(RenderManager rm) {

//TODO: add render code

}

}[/java]

[java]package Gui;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Spatial;

import com.jme3.scene.shape.Box;

import mygame.Main;



public class Menus extends SimpleApplication{



public void Main(){ // I want to use this method in main.java

Box box = new Box(Vector3f.ZERO, 2.5f,2.5f,1.0f);

Spatial wall = new Geometry(“Box”, box );

Material mat_brick = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”); // says something is wrong with this?

wall.setMaterial(mat_brick);

wall.setLocalTranslation(2.0f,-2.5f,0.0f);

rootNode.attachChild(wall);



}

public void simpleInitApp(){



}

}[/java]



error



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

java.lang.NullPointerException

at com.jme3.material.Material.(Material.java:116)

at Gui.Menus.Main(Menus.java:17)

at mygame.Main.simpleInitApp(Main.java:28)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:228)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)

at java.lang.Thread.run(Thread.java:722)


How can I get this to work?

I know I can use the niftyGui but I wanted to make a more 3d menu.

You should be setting stuff up in simpleinitapp not in main().



The null pointer is because asset manager is not initialized yet.



NPE are very easy to track down though…you should be able to follow it just from the stack trace…

Thanks for the reply. I thought that if we called the method Main() in the simpleInitApp () that it would run like normal.

I have to make dinner for my kids, so this will have to be short.



There is no real need to have 2 separate game threads running to do what you’re trying to do. The GUI node accessible through the rootNode is the same as any other node … it renders differently, but still allows you to load 3d objects into it. You may have to scale them different, etc, etc… however… I think your initial approach here is flawed… and may be why no one has been able to answer you so far.



Sorry I don’t have the time to show an example… but… mom = must cook.

XD Thanks, I was more going to have a class file with all the code for all the menus then grab the bits I needed when the time comes.

Take a step back and try to think logically. How many applications will you need in your application? Or to use an analogy, how many cars do you need in your car?



Answer: 1



So if you find yourself making more than one application in your application then you are heading in the wrong direction. You can have many classes, though… and you will have to provide them what they need to do their job.



I don’t want to sound discouraging but there are few things as hard as learning to program 3D games. And there are few things as hard as learning to program object oriented from scratch. Combining them makes it almost impossible. Maybe pick a simple project to learn Java first and then return to the next hard part. I usually recommend a simple text adventure. A decent Java programmer could write one over a weekend and nothing you learn is wasted when it comes time to write a game.

Ok,I will do that. I appreciate your help.