Just a snippet of code of AppState

Here is the code snippet. dist() method is being called from update() method which is being called when button is pressed. But lines inside dist() keeps giving exceptions.

[java]
public class HudScreen extends AbstractAppState implements ScreenController{
public void dist(int flag){

	Spatial card1_player = this.assetManager.loadModel("/Object/Card/card.blend");
	Spatial card2_player = this.assetManager.loadModel("/Object/Card/card.blend");
	Spatial card1_bot = this.assetManager.loadModel("/Object/Card/card.blend");
	Spatial card2_bot = this.assetManager.loadModel("/Object/Card/card.blend");
	
	if(flag==1){
	
	card1_player.setLocalTranslation(1f, -2.2f, -4f);
	card1_player.scale(1f);
	Material card_mat = new Material(this.assetManager, "/Common/MatDefs/Misc/Unshaded.j3md");
	card_mat.setTexture("ColorMap", this.assetManager.loadTexture("/CardImages/card_01.png"));
	card1_player.setMaterial(card_mat);
	localRootNode.attachChild(card1_player);
	
	
	card2_player.setLocalTranslation(-1f, -2.2f, -4f);
	card2_player.scale(1f);
	//Material card_mat1 = new Material(assetManager, "/Common/MatDefs/Misc/Unshaded.j3md");
	card2_player.setMaterial(card_mat);
	localRootNode.attachChild(card2_player);
	
	
	card1_bot.setLocalTranslation(1f, -2.5f, 3.5f);
	card1_bot.scale(1f);
	//Material card_mat = new Material(assetManager, "/Common/MatDefs/Misc/Unshaded.j3md");
	card1_bot.setMaterial(card_mat);
	localRootNode.attachChild(card1_bot);
	
	
	card2_bot.setLocalTranslation(-1f, -2.5f, 3.5f);
	card2_bot.scale(1f);

// Material card_mat = new Material(assetManager, “/Common/MatDefs/Misc/Unshaded.j3md”);
card2_bot.setMaterial(card_mat);
localRootNode.attachChild(card2_bot);
}
}

public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app=(SimpleApplication)app;

	    this.viewPort      = app.getViewPort();
	    this.assetManager  = app.getAssetManager();
	    
}
 
 public void update(){
	 System.out.println("update");
	 dist(flag);
 }

}
[/java]
Also one more thing, when I create constructor below , the button which nifty element just stops working. If I remove the constructor it works. Is constructor not allowed?
[java]

public HudScreen(SimpleApplication app){
this.rootNode = app.getRootNode();
this.viewPort = app.getViewPort();
this.guiNode = app.getGuiNode();
this.assetManager = app.getAssetManager();
}

[/java]

If nifty creates the controller then you must have a public no-argument constructor so that nifty can create it.

If you create the controller yourself and then register it with nifty you can do whatever you like :slight_smile:

I’m surprised your app state compiles since it is referencing a bunch of non-existent fields. Or did you leave code out?

Most likely you are getting null pointer exceptions because your app state was not attached to the state manager… thus initialize was never called.

Basic debugging will confirm this. Either add some printlns or place a break point and run it in the debugger.

Here is the complete code for class HudScreen.
Please go through it.
Thanks for your time.
[java]
package main;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class HudScreen extends AbstractAppState implements ScreenController{

private ViewPort viewPort;
private Node rootNode;
private Node guiNode;
private AssetManager assetManager;
private AppStateManager stateManager;
private Nifty nifty;
private Screen screen;
private SimpleApplication app;
private static int flag=1;
public static int change=1;
Player player = new Player();
Bot bot = new Bot();
static String msg="fuck you";
private PokerMain    physics;
private Node localRootNode = new Node("Hud Screen RootNode"); 
//Element popupElement = nifty.createPopup("PopupRaise");

// public HudScreen(SimpleApplication app){
// this.rootNode = app.getRootNode();
// this.viewPort = app.getViewPort();
// this.guiNode = app.getGuiNode();
// this.assetManager = app.getAssetManager();
// }
@Override
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}

public void startGame(String nextScreen) {
    
	System.out.println("Came");
	String swap="swap";
	if(nextScreen.equals(swap)){
		System.out.println("swap worked");
		if(change==1){

// stateManager.detach(bot);
// stateManager.attach(player);
getStateName(1);
change=0;
System.out.println(“bot is here”);
}
else if(change==0){
// stateManager.detach(player);
// stateManager.attach(bot);
getStateName(0);
change=1;
System.out.println(“player is here”);
}
// nifty.gotoScreen(“hud”);
}
else{
System.out.println(“next screen is here”);
nifty.gotoScreen(nextScreen);
}
}
public void PopupRaise(){

	 Element popupElement = nifty.createPopup("PopupRaise");
	 
	 nifty.showPopup(nifty.getCurrentScreen(), popupElement.getId(), null);

}

public void getStateName(int state){

// AppState state;
// state=stateManager.getState(AppState Player);

	if(state==0){
		 msg="bot's turn";
		 returnStateName();
	}
	else if(state==1){
		msg="your turn";
		returnStateName();
	}

	Element niftyElement = nifty.getCurrentScreen().findElementByName("play_turn");
	// swap old with new text
	 niftyElement.getRenderer(TextRenderer.class).setText(msg);

} 

public String returnStateName(){
	System.out.println(msg);
	return msg;
}
public void PopupClose(){
	//Element popupElement = nifty.createPopup("PopNewUser");
	
    nifty.closePopup(screen.getTopMostPopup().getId());
    
 }

public void distribute(){

// if(msg==“yes”){
if(flag==0)
flag=1;
else if(flag==1)
flag=0;
// }

}

public static int returnDist(){
	return flag;
}

public void dist(int flag){

	Spatial card1_player = this.assetManager.loadModel("/Object/Card/card.blend");
	Spatial card2_player = this.assetManager.loadModel("/Object/Card/card.blend");
	Spatial card1_bot = this.assetManager.loadModel("/Object/Card/card.blend");
	Spatial card2_bot = this.assetManager.loadModel("/Object/Card/card.blend");
	
	if(flag==1){
	
	card1_player.setLocalTranslation(1f, -2.2f, -4f);
	card1_player.scale(1f);
	Material card_mat = new Material(this.assetManager, "/Common/MatDefs/Misc/Unshaded.j3md");
	card_mat.setTexture("ColorMap", this.assetManager.loadTexture("/CardImages/card_01.png"));
	card1_player.setMaterial(card_mat);
	localRootNode.attachChild(card1_player);
	
	
	card2_player.setLocalTranslation(-1f, -2.2f, -4f);
	card2_player.scale(1f);
	//Material card_mat1 = new Material(assetManager, "/Common/MatDefs/Misc/Unshaded.j3md");
	card2_player.setMaterial(card_mat);
	localRootNode.attachChild(card2_player);
	
	
	card1_bot.setLocalTranslation(1f, -2.5f, 3.5f);
	card1_bot.scale(1f);
	//Material card_mat = new Material(assetManager, "/Common/MatDefs/Misc/Unshaded.j3md");
	card1_bot.setMaterial(card_mat);
	localRootNode.attachChild(card1_bot);
	
	
	card2_bot.setLocalTranslation(-1f, -2.5f, 3.5f);
	card2_bot.scale(1f);

// Material card_mat = new Material(assetManager, “/Common/MatDefs/Misc/Unshaded.j3md”);
card2_bot.setMaterial(card_mat);
localRootNode.attachChild(card2_bot);
}
else if(flag==0){
// localRootNode.detachChild(card1_bot);
}
}

@Override
public void onEndScreen() {
	// TODO Auto-generated method stub
	
}

@Override
public void onStartScreen() {
	// TODO Auto-generated method stub
	
}

 public void initialize(AppStateManager stateManager, Application app) {
	    super.initialize(stateManager, app);
	    this.app=(SimpleApplication)app;
	    
	    System.out.println("initialize");

// this.rootNode = app.getRootNode();
// this.viewPort = app.getViewPort();
// this.guiNode = app.getGuiNode();
// this.assetManager = app.getAssetManager();

}
 
 public void update(){

	 System.out.println("update");
	 dist(flag);
	 

 }

}
[/java]

Do you ever see the output from this?
System.out.println(“initialize”);

Do you ever stateManager.attach() this app state?

I didn’t attach it. I just registered it in nifty xml. So it is triggered by nifty button. Everything works except that dist() method and I didn’t see the “initialize” msg.

@tsogtoo said: I didn't attach it. I just registered it in nifty xml. So it is triggered by nifty button. Everything works except that dist() method and I didn't see the "initialize" msg.

I can only say this about five different ways…

You are getting null pointer exceptions in dist. Why?

Because your app state is not initialized. Why?

Because it was never attached to the state manager.

I even kind of gave you the clues needed to track this down yourself. I mean this to be helpful: 3D game programming and nifty are extremely complicated. You might try a few simpler projects before tackling the “big things” until your debugging skills get better.

2 Likes

Thanks @pspeed. Though there is less time, I will work hard on it.