Archived

This topic is now archived and is closed to further replies.

KiwiMelon

Turning TextField Uneditable on and off

Recommended Posts

KiwiMelon    122
I’m just getting started with J2ME so I thought I would play around with some of the high level UI components. What I’m trying to do is have a ChoiceGroup control whether a TextField is editable or not. However for some reason a NullPointerException gets thrown and I don’t know why. This is the code I have:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class EditableTextFieldMIDlet extends MIDlet
{
	private boolean startup;
	Display display;

	EditableTextFieldForm editableTextFieldForm;

	public EditableTextFieldMIDlet()
	{
		startup = true;
	}

	public void startApp()
	{
		if(startup)
		{
			display = Display.getDisplay(this);
			editableTextFieldForm = new EditableTextFieldForm(this);
			startup = false;
		}

		display.setCurrent(editableTextFieldForm);
	}

	public void pauseApp()
	{
	}

	public void destroyApp(boolean unconditional)
	{
	}

	public void exitRequested()
	{
		destroyApp(false);
		notifyDestroyed();
	}
}

class EditableTextFieldForm extends Form implements CommandListener, ItemStateListener
{
	EditableTextFieldMIDlet midlet;

	ChoiceGroup allowEditingChoiceGroup;
	TextField textField;

	Command exitCommand;

	public EditableTextFieldForm(EditableTextFieldMIDlet main)
	{
		super("Editable Text Field");

		midlet = main;

		allowEditingChoiceGroup = new ChoiceGroup("Allow Editing", Choice.EXCLUSIVE);
		allowEditingChoiceGroup.append("Off", null);
		allowEditingChoiceGroup.append("On", null);
		append(allowEditingChoiceGroup);

		textField = new TextField("String:", null, 10, TextField.UNEDITABLE);
		append(textField);

		setItemStateListener(this);

		exitCommand = new Command("Exit", Command.EXIT, 1);
		addCommand(exitCommand);
		setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d)
	{
		if(c == exitCommand)
		{
			midlet.exitRequested();
		}
	}

	public void itemStateChanged(Item item)
	{
		if(item == allowEditingChoiceGroup)
		{
			if(allowEditingChoiceGroup.getSelectedIndex() == 1)
			{
				// On

				textField.setConstraints(TextField.ANY);
			}
			else
			{
				textField.setConstraints(TextField.UNEDITABLE);
			}
		}
	}
}
You can select between the two choices without any problem but when you give focus to the TextField and then change the value in the ChoiceGroup the following stack trace for the exception comes up:
java.lang.NullPointerException 
	at com.sun.midp.lcdui.DefaultInputMethodHandler.setConstraints(+63) 
	at javax.microedition.lcdui.TextField$InputMethodClientImpl.setConstraints(+20) 
	at javax.microedition.lcdui.TextField.setConstraints(+37) 
	at EditableTextFieldForm.itemStateChanged(+27) 
	at javax.microedition.lcdui.Form.callItemStateChanged(+27) 
	at javax.microedition.lcdui.Display.callItemStateChanged(+14) 
	at javax.microedition.lcdui.Display.access$900(+5) 
	at javax.microedition.lcdui.Display$DisplayAccessor.callItemStateChanged(+8) 
	at javax.microedition.lcdui.Display$DisplayManagerImpl.callItemStateChanged(+10) 
	at com.sun.midp.lcdui.DefaultEventHandler.itemStateChangedEvent(+17) 
	at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+835) 
Any insights would be greatly appreciated. Cheers.

Share this post


Link to post
Share on other sites