Skip to main content

Update TextField text value error in a keyPressed

1 reply [Last post]
renard
Offline
Joined: 2008-07-16
Points: 0

I would like create a Spinner Objet which adds 1 to the current TextField value when a key is pressed. I wrote this :

import com.sun.lwuit.TextField;

public class Spinner extends TextField{

public Spinner(int value) {
super(Integer.toString(value), 2);
}

public void keyPressed(int keyCode) {
int value = Integer.parseInt(this.getText());
value = value + 1;
this.setText(Integer.toString(value));

}
}

This code changes the displayed value of the TextField but just after there is a message with an InternalError : NullPointerException :

java.lang.NullPointerException
at com.sun.lwuit.Form.updateFocus(+68)
at com.sun.lwuit.Form.keyPressed(+133)
at com.sun.lwuit.Implementation.handleEvent(+56)
at com.sun.lwuit.Implementation.edtLoopImpl(+143)
at com.sun.lwuit.Implementation.mainEDTLoop(+144)
at com.sun.lwuit.Implementation.run(+4)

Do you know why I have this error ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Shai Almog

Hi,
first off I assume that what you want to do is really very different
than what you actually did. The text field is a remarkably elaborate
component that won't work with the spinner functionality well...
I suggest you start off from the creating custom component sample in
the Developer Guide and work from there.
I got number format exceptions but I didn't get the
NullPointerException mentioned.

You should generally ignore navigation keys in such cases and use
setHandlesFocus(true/false) when you wish to handle input form the
user rather than let navigation "steal" it.

Thanks,
Shai.

> I would like create a Spinner Objet which adds 1 to the current
> TextField value when a key is pressed. I wrote this :
>
> [i]import com.sun.lwuit.TextField;
>
> public class Spinner extends TextField{
>
> public Spinner(int value) {
> super(Integer.toString(value), 2);
> }
>
> public void keyPressed(int keyCode) {
> int value = Integer.parseInt(this.getText());
> value = value + 1;
> this.setText(Integer.toString(value));
>
> }
> }[/i]
>
> This code changes the displayed value of the TextField but just
> after there is a message with an InternalError :
> NullPointerException :
>
> [i]java.lang.NullPointerException
> at com.sun.lwuit.Form.updateFocus(+68)
> at com.sun.lwuit.Form.keyPressed(+133)
> at com.sun.lwuit.Implementation.handleEvent(+56)
> at com.sun.lwuit.Implementation.edtLoopImpl(+143)
> at com.sun.lwuit.Implementation.mainEDTLoop(+144)
> at com.sun.lwuit.Implementation.run(+4)[/i]
>
> Do you know why I have this error ?
> [Message sent by forum member 'renard' (renard)]
>
> http://forums.java.net/jive/thread.jspa?messageID=287055
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

Shai Almog
http://lwuit.blogspot.com/

[att1.html]