Android nullpointerexception on TextField

17 views
Skip to first unread message

timg...@gmail.com

unread,
Nov 15, 2013, 6:25:04 AM11/15/13
to codenameone...@googlegroups.com
I have a form with a button and a textfield.  Prior to the form being shown the following is invoked:

    @Override
    protected void beforeConfirmBooking(Form f) {                findBookingConfirmationDescriptionTextField().setText(booking.getCollection().getWaypointDescription());
         }

And the text is set without an issue.

Upon clicking the button the following is invoked:

    @Override
    protected void onConfirmBooking_ButtonAction(Component c, ActionEvent event) {
        showProgressDialog(RoadTripOne.localization.get("saving").toString() + "...");
        String bookingDescription = findBookingConfirmationDescriptionTextField().getText();
    }

And findBookingConfirmationDescriptionTextField() returns a nullpointerexception. 

This occurs only in Android (on both the emulator and a real device) and not on the iOS emulator.

What is happening?

timg...@gmail.com

unread,
Nov 15, 2013, 6:31:37 AM11/15/13
to codenameone...@googlegroups.com, timg...@gmail.com
To answer my own question, the issue is the first line:

showProgressDialog(RoadTripOne.localization.get("saving").toString() + "...");

    public static void showProgressDialog(String text) {
        progressDialog = new com.codename1.ui.Dialog(text);
        com.codename1.components.InfiniteProgress loadingProgress = new com.codename1.components.InfiniteProgress();
        progressDialog.addComponent(loadingProgress);
        progressDialog.showModeless();       
    }  

In creating the progress dialog it seems that I no longer have access to the form's fields.  Strangely enough this is only an issue in Android.

Is this a bug, or design?

Shai Almog

unread,
Nov 15, 2013, 3:56:58 PM11/15/13
to codenameone...@googlegroups.com, timg...@gmail.com
The findBookingConfirmationDescriptionTextField() method is invoked on the current form which is the one prior to the confirm booking form.
If you have a progress dialog open then it might return as the current form (due to race conditions etc.) hence this will behave badly.

The solution is never to use findBookingConfirmationDescriptionTextField() or find methods that don't take arguments unless you are sure about the current form (e.g. button event handler).
Use findBookingConfirmationDescriptionTextField(f) with the form as an argument.

If you don't have the form add an action listener to the text field and save its value to a class member.
Reply all
Reply to author
Forward
0 new messages