I wanted to make it so that when I select a specific TextField in a Form, I get a specific list of Commands,
for example
TextField: OK, Cancel
TextField2: OK, Back, Help
And I succeeded by using addCommand() on each of TextFields but I can’t tell it what to happen when when I activate one of these commands since there is no setCommandListener method for them.
It throws a NullPointerException when I click it.
I also tried to get the commands from their Form but it is receiving only the commands that it contains, not the commands that TextFields have.
How to get around this?
TextField is an
Itemobject, and to use commands with it you need ItemCommandListener:Most likely reason for
NullPointerExceptionyou mentioned is that you did not set appropriate item command listener (it’s null by default, thus when MIDP attempts to invoke command action, there is NPE).To get what you want, besides creating and adding commands, you need to create item command listener, implement code to handle commands in it, then associate your text field with this listener: