openGeeksLab / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

Error in Obtaining a List from Web Service #927

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm having trouble in creating a list that is obtained from a web service. The 
following error appears the first time we press the action button but once we 
press ok to the error and re-press the action button it will create the list 
properly.

The following error: 

java.lang.NullPointerException
    at java.util.ArrayList.<init>(Unknown Source)
    at com.codename1.ui.list.DefaultListModel.<init> (DefaultListModel.java:60)
    at userclasses.StateMachine.initListModelList(StateMachine.java:199)
    at generated.StateMachineBase.setListModel(StateMachineBase.java:1316)
    at com.codename1.ui.util.UIBuilder.createComponent(UIBuilder.java:1422)
    at com.codename1.ui.util.UIBuilder.createComponent(UIBuilder.java:1363)
    at com.codename1.ui.util.UIBuilder.createContainer(UIBuilder.java:409)
    at com.codename1.ui.util.UIBuilder.createContainer(UIBuilder.java:394)
    at com.codename1.ui.util.UIBuilder.showContainerImpl(UIBuilder.java:2279)
    at com.codename1.ui.util.UIBuilder.showContainer(UIBuilder.java:2161)
    at com.codename1.ui.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2772)
    at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:383)
    at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:326)
    at com.codename1.ui.Form.actionCommandImplNoRecurseComponent(Form.java:1228)
    at com.codename1.ui.Button.fireActionEvent(Button.java:372)
    at com.codename1.ui.Button.released(Button.java:407)
    at com.codename1.ui.Button.pointerReleased(Button.java:495)
    at com.codename1.ui.Form.pointerReleased(Form.java:2194)
    at com.codename1.ui.Form.pointerReleased(Form.java:2140)
    at com.codename1.ui.Component.pointerReleased(Component.java:2319)
    at com.codename1.ui.Display.handleEvent(Display.java:1766)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:970)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:899)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:119)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.lang.NullPointerException
    at com.codename1.ui.Container.insertComponentAt(Container.java:423)
    at com.codename1.ui.Container.addComponent(Container.java:403)
    at com.codename1.ui.Form.addComponent(Form.java:836)
    at com.codename1.ui.util.UIBuilder.showContainerImpl(UIBuilder.java:2285)
    at com.codename1.ui.util.UIBuilder.showContainer(UIBuilder.java:2161)
    at com.codename1.ui.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2772)
    at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:383)
    at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:326)
    at com.codename1.ui.Form.actionCommandImplNoRecurseComponent(Form.java:1228)
    at com.codename1.ui.Button.fireActionEvent(Button.java:372)
    at com.codename1.ui.Button.released(Button.java:407)
    at com.codename1.ui.Button.pointerReleased(Button.java:495)
    at com.codename1.ui.Form.pointerReleased(Form.java:2194)
    at com.codename1.ui.Form.pointerReleased(Form.java:2140)
    at com.codename1.ui.Component.pointerReleased(Component.java:2319)
    at com.codename1.ui.Display.handleEvent(Display.java:1766)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:970)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:899)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:119)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176) 

The code is as follow:

  @Override
    protected void onMenu_CuentasButtonAction(Component c, ActionEvent event) {

        ConnectionRequest r = new ConnectionRequest() {         
            String h = "";

            @Override
            protected void readResponse(InputStream input) throws IOException {
                XMLParser xmlp = new XMLParser();
                h = "" + xmlp.parse(new InputStreamReader(input));
                System.out.println(h);
            }

            @Override
            protected void postResponse() {
                String error = "X";
                Result result = Result.fromContent(h, Result.XML);
                error = result.getAsString("//consultarSaldos/errorcode");

                if (!error.trim().equals("'0'")) {
                    Dialog.show("Error", "Informacion Invalida", "OK", null);
                } else {
                    String accountAvailable[] = result.getAsStringArray("//accountavailable");
                    String accountCurrency[] = result.getAsStringArray("//accountcurrency");
                    String accountNumber[] = result.getAsStringArray("//accountnumber");
                    String accountType[] = result.getAsStringArray("//accounttype");

                    lsaccounts = new Vector();
                    int numel = accountAvailable.length;
                    for (int i = 0; i < numel; i++) {

                        String accountAvailable1 = accountAvailable[i];
                        String accountCurrency1 = accountCurrency[i];
                        String accountNumber1 = accountNumber[i];
                        String accountType1 = accountType[i];

                        accountAvailable1 = accountAvailable1.trim().substring(1, accountAvailable1.trim().length() - 1);
                        accountCurrency1 = accountCurrency1.trim().substring(1, accountCurrency1.trim().length() - 1);
                        accountNumber1 = accountNumber1.trim().substring(1, accountNumber1.trim().length() - 1);
                        accountType1 = accountType1.trim().substring(1, accountType1.trim().length() - 1);

                        dbAccount accbean = new dbAccount();
                        String accnumt = "";
                        if (accountNumber1.length() > 4) {
                            accnumt = accountNumber1.substring( accountNumber1.length() - 4, accountNumber1.length());
                        } else {
                            accnumt = accountNumber1;
                        }

                        String accnumMask = accountType1 + " No. **** " + accnumt + " " + accountCurrency1 + " "+ accountAvailable1;
                        accbean.setaccNum(accnumMask);
                        accbean.setaccAvai(accountCurrency1 + " "+ accountAvailable1);
                        accbean.setaccTyp(accountType1);
                        lsaccounts.add(accnumMask);
                    }
                }
            }
        };

        r.setUrl(URL);
        r.setPost(false);
        r.addArgument("PB", "2");
        r.addArgument("USERID", USERID);
        r.addArgument("USERPA", USERPA);
        InfiniteProgress prog = new InfiniteProgress();
        Dialog dlg = prog.showInifiniteBlocking();
        r.setDisposeOnCompletion(dlg);
        NetworkManager.getInstance().addToQueue(r);
        r.getResponseData();

    }

    @Override
    protected boolean initListModelList(List cmp) {

        cmp.setModel(new com.codename1.ui.list.DefaultListModel(lsaccounts));
        return true;
    }

P.S. I used the rendering list method in the theme.

The expected output is to have different accounts listed in the first press of 
the button. 

Thanks.

Original issue reported on code.google.com by ABuitrag...@gmail.com on 23 Oct 2013 at 9:24

GoogleCodeExporter commented 9 years ago
This issue tracker is meant to track issues in Codename One. Please refer to 
the discussion forum for help: http://www.codenameone.com/discussion-forum.html

Original comment by shai.almog on 24 Oct 2013 at 4:29