Trouble in obtaining a list that comes from a web service

42 views
Skip to first unread message

abuitr...@gmail.com

unread,
Oct 23, 2013, 5:53:47 PM10/23/13
to codenameone...@googlegroups.com
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:
private String USERID = ""; private String USERPA = ""; private String URL = "http://134.177.251.210:9080/DIBS_SERVER_MOVIL_DEMO/servlet/com.datapro.dibs.servlets.JSServicesList"; // private String URL = // "http://localhost:9081/DIBS_SERVER_MOVIL_DEMO/servlet/com.datapro.dibs.servlets.JSServicesList"; private Vector lsaccounts = null; private Vector lsretailaccounts = null;
        private Vector lsbenes = null;  

@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 as one first presses the button. Thanks.

Shai Almog

unread,
Oct 24, 2013, 12:55:47 AM10/24/13
to codenameone...@googlegroups.com, abuitr...@gmail.com
lsaccounts is null initially so you obviously get a null pointer exception. After you fetch it, then it is no longer null and it starts working.

protected boolean initListModelList(List cmp) {
   
if(lsaccounts == null) {
          cmp
.setModel(new com.codename1.ui.list.DefaultListModel());
   
} else {
          cmp
.setModel(new com.codename1.ui.list.DefaultListModel(lsaccounts));
   
}        
   
return true;    
}


abuitr...@gmail.com

unread,
Oct 24, 2013, 4:00:47 PM10/24/13
to codenameone...@googlegroups.com, abuitr...@gmail.com
Thank you for your help, but with this code it will generate first a blank list and then actual list will load. How does one prevent the blank list?
</code
...

Shai Almog

unread,
Oct 25, 2013, 1:47:32 AM10/25/13
to codenameone...@googlegroups.com, abuitr...@gmail.com
addToQueue() returns immediately so the getData() following it is meaningless.
You can change it to addToQueueAndWait() which might be what you are after.
Reply all
Reply to author
Forward
0 new messages