external update of textfield reports null

Anything libgdx related goes here!

external update of textfield reports null

Postby Ickyrus » Sat Feb 01, 2020 12:56 am

I am trying to update the test of a textfield by an external function. They should reflect the contents of an external array list, when I am not actively focused in one of them. The code I am using is as follows:

-----------------------------------------------------------
private static TextButton indexButton;
private static TextField methodField;
---------------------------------------------------------------------------------------------------------------------------------

final TextField methodField = new TextField("", LottopgGame.mainSkin, "default");

methodField.setText(Integer.toString(World.testline.get(scriptIndex).method)); //retrieve value in update
methodField.setPosition(272, Gdx.graphics.getHeight()/2 + 256f);
methodField.setWidth(96);
methodField.setHeight(32);
methodField.setAlignment(Align.center);
methodField.setTextFieldFilter(new TextField.TextFieldFilter.DigitsOnlyFilter());
methodField.setVisible(false);
methodField.setFocusTraversal(false);

methodField.addListener(new FocusListener(){
public void keyboardFocusChanged(FocusListener.FocusEvent event, Actor actor, boolean focused) {
if (focused == true) {
/** change text to "" */

} else if (focused == false) {
//update after leaving if it happens no where else
methodField.setText(Integer.toString(World.testline.get(scriptIndex).method));

}
}
});

methodField.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
//get a number, test it against allowable range and adjust if needed
int all = methodField.getText().isEmpty() ? (0)
: Integer.parseInt(methodField.getText());
//range check
if(all < 0 || all > 6){
all = 0;
}


World.testline.get(scriptIndex).method = all;
//update is automatic, but not if we fixed it..
methodField.setText(Integer.toString(World.testline.get(scriptIndex).method));

//try writing result to the scrollpane
World.scrollableResults.clear();
World.scrollableResults.add("You set the value of method to " + Integer.toString(all) + " and the text value of the field is " + methodField.getText() + " and testline is " + World.testline.get(scriptIndex).method );
resultsList.setItems(World.scrollableResults);
scrollPane.setVisible(true);

}
});



methodField.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
chartButton.setText(" Methods are 0: do nothing 1: Hits 2: Skips 3: Follow 4: Adjacent 5: Skip Chance 6: Skip Group Chance ");
return true;
}
}

);

mainStage.addActor(methodField);

---------------------------------------------------------------------------------------------------------------------
en example of calling the function from a button that adds to the scripting array

addButton = new ImageButton(game.mainSkin, "ImageButtonplus");
addButton.setPosition(32.0f,Gdx.graphics.getHeight()/2 + 288.0f,3);
addButton.setVisible(false);
addButton.addListener(new InputListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

//if editing date through picker, just return
ScriptLine thisscript = new ScriptLine();
World.testline.add(thisscript);
scriptIndex += 1; //move to the newly created script entry
//todo add script view update
updateChooseFilters(); // ************* <-------- called here
}

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
}
);
mainStage.addActor(addButton);

--------------------------------------------------------------------------------------
****** the called function :

public void updateChooseFilters(){
indexButton.setText(String.valueOf(scriptIndex)); //this updates as expected on the same page as the textfield below
int testme = World.testline.get(scriptIndex).method; //this retrieves an integer of value "0" correctly
methodField.setText(Integer.toString(testme)); //this crashes with nullpointer exception, debugger says methodField is null?
}


-----------------------------------------------------------------------------------------

I can execute the contents of this function in the listeners of the add button example above and they work fine. But the external call does not. I fired up the debugger, and sure enough, it reports that the textfield itself is null when I enter the update function. It will update the index button on the same page with no issues..

I'm using 1.9.9, not latest, but this does not appear in the closed or open issues as far as I could see.

Please help :)
Ickyrus
 
Posts: 18
Joined: Wed Jan 18, 2012 10:03 pm

Re: external update of textfield reports null *answered

Postby Ickyrus » Sat Feb 01, 2020 6:16 pm

My bad, declaring the textfield in the Class , and then FINAL in the constructor meant there were two of them, and the declared but uninitialized version was accessed by the function external to the constructor, and was indeed null. Removing Final cured it.
Ickyrus
 
Posts: 18
Joined: Wed Jan 18, 2012 10:03 pm


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot] and 1 guest