berryandcherry 0 Newbie Poster

Below is the code i am using to create dropdown using listfields.In this i am able to create dropdown with hardcoded data binded to listfields with no Problem,but when i bind data from webservice when i clicked on horizontalfield(dropdown),first it shows the null pointer exception(White screen),by clicking middle button of simulator i am able to go back to dropdown design screen, where datas get binded in listfields.My question is why it shows null pointer exception even when the datas are binded to listfield.

    int index = -1;
    Object choice[];
    Vector Sales = new Vector();
    Vector SalesID=new Vector();
    String SalesData;
    ListField salechoiceFieldterm;
    ListField salechoicelistterm;
    Object[] salechoiceterm;
    String saletxtterm = "-select-";
    VerticalFieldManager saledropdown;

salerentField = new HorizontalFieldManager(DrawStyle.HCENTER
                    | DrawStyle.VCENTER) {
                public void sublayout(int width, int height) {
                    // width = Display.getWidth() ;
                    height = dropdownheight;
                    super.sublayout(width, height);
                    setExtent(width, height);
                }

            };
salerentField.setBackground(BackgroundFactory.createBitmapBackground(
                    bmpstour1, Background.POSITION_X_RIGHT,
                    Background.POSITION_Y_TOP, Background.REPEAT_NONE));
            salerentField.setMargin(10, 0, 0, 0);
            // final Object choice1[] = {""};
            // this.salechoiceterm = new Object[Sales.size()];
            // this.salechoiceterm = (Object[]) Sales.elementAt(0);
            salechoiceFieldterm = new ListField(DrawStyle.VCENTER
                    | ListField.MULTI_SELECT) {
                protected boolean navigationClick(int status, int time) {

                    Field focus = UiApplication.getUiApplication()
                            .getActiveScreen().getLeafFieldWithFocus();
                    if (Sales.size() > 0) {
                        if (focus instanceof ListField) {
                            saledropdown.deleteAll();
                            ChoicePopupScreenfilter popup = new ChoicePopupScreenfilter(
                                    30, 10, salechoiceterm, 0);
                            popup.setChoice(salechoiceterm);

                        }
                    }
                    return super.navigationClick(status, time);
                }

                protected void drawFocus(Graphics g, boolean on) {
                    // Dialog.alert(""+Display.getHeight());
                    int btnfocusX = 0, btnfocusY = 0, btnfocusWidth = 0, btnfocusHeight = 0;

                    XYRect myExtent = getExtent();
                    int alpha = g.getGlobalAlpha();
                    g.setGlobalAlpha(0xBF);
                    g.setColor(CloudTemplate.getDescptnColor());

                    // g.fillRect(18, -2, getWidth()-45, getHeight()+50);
                    g.fillRect(btnfocusX, btnfocusY,
                            getWidth() - btnfocusWidth, getHeight()
                                    + btnfocusHeight);

                    g.setColor(0XAE1E16);
                    g.setGlobalAlpha(alpha);
                }

                protected void paint(Graphics g) {

                    g.setColor(0x3A3E44);
                    super.paint(g);

                }

            };
                salechoiceFieldterm.setMargin(5, 0, 0, 5);
salechoiceFieldterm.setSize(1);
            salechoiceFieldterm.setCallback(new DropdownListCallbackfilter(0));

            salerentField.add(salechoiceFieldterm);

            saledropdown = new VerticalFieldManager() {
    protected void paint(Graphics g) {

                    g.setColor(0x5d5d64);
                    g.fillRoundRect(0, 0, getWidth(), getHeight(), 0, 0);

                    // draw the border
                    g.setColor(0x2accff);
                    g.drawRoundRect(0, 0, getWidth(), getHeight(), 0, 0);

                    g.setColor(0xffffff);
                    // g.setColor(Color.WHITE);

                    super.paint(g);

                }

            };
            saledropdown.setBackground(BackgroundFactory
                    .createSolidBackground(0x5d5d64));
add(salerentField);
add(saledropdown);








public class ChoicePopupScreenfilter {
        Object[] salechoiceterm = null;
        private int _posX = 0;
        private int _posY = 0;

        public ChoicePopupScreenfilter(int posx, int posy,
                final Object[] choice, int value) {
            /* calling super class constructor */

            /* Setting position for popup screen */
            _posX = posx;
            _posY = posy;
            int maxWidth = Display.getWidth();
            int maxHeight = 300;
            if (value == 0) {

                this.salechoiceterm = new Object[Sales.size()];
                this.salechoiceterm = choice;
                /* list field customized to display as choice picker */
                salechoicelistterm = new ListField(ListField.MULTI_SELECT) {
                    /* list field event handling using navigation click */
                                    protected boolean navigationClick(int status, int time) {
    setSelIndex(this.getSelectedIndex(), salechoiceterm,
                                salechoicelistterm, 0);
                        //
                        this.invalidate();
                        saledropdown.deleteAll();
                        return true;
                    }

                };

                salechoicelistterm.setSize(Sales.size());
                salechoicelistterm.setCallback(new PopUpListCallbackfilter(value));
                saledropdown.add(salechoicelistterm);
    protected void setChoice(Object[] choice) {
            this.salechoiceterm = new Object[choice.length];
            this.salechoiceterm = choice;
        }

        protected void sublayout(int width, int height) {
            // super.sublayout(width, height);
            /* setting the position for the popup screen */
            setPosition(_posX, _posY);
        }
    }

    public void setSelIndex(int index, Object[] choice, ListField field, int id) {
        this.index = index;
            if (id == 0) {
    this.saletxtterm = Sales.elementAt(index).toString(); // listElementsterm.elementAt(index).toString();

            SalesData = saletxtterm;
            SaleID=((SearchID) SalesID.elementAt(index)).getmakeID();
            }
        field.invalidate();
    }

    public class PopUpListCallbackfilter implements ListFieldCallback {
        int popupval;

        PopUpListCallbackfilter(int val) {
            popupval = val;
        }

        public void drawListRow(ListField list, Graphics g, int index, int y,
                int w) {
                if (popupval == 0) {
            String textcolor = Sales.elementAt(index).toString();// listElementsterm.elementAt(index).toString();
                g.drawText(textcolor, 10, y, 0, w);
                }
if (index == 0) {

            } else {
                g.drawLine(0, y - 15, Display.getWidth(), y - 15);
            }
        }

        public Object get(ListField listField, int index) {
            // TODO Auto-generated method stub
            return null;
        }

        public int getPreferredWidth(ListField listField) {
            // TODO Auto-generated method stub
            return 0;
        }

        public int indexOfList(ListField listField, String prefix, int start) {
            // TODO Auto-generated method stub
            return 0;
        }

    }

    final class DropdownListCallbackfilter implements ListFieldCallback {
        private int val;

        DropdownListCallbackfilter(int value) {
            val = value;
        }


public void drawListRow(ListField list, Graphics g, int index, int y,
                int w) {
            Bitmap bitmap;
            bitmap = Bitmap.getBitmapResource("dropdwnimage.png");
        int height;

            height = list.getRowHeight();

            int posY = (height - bitmap.getHeight()) / 2;
            g.setColor(Color.WHITE);
            int txtpadding, topheiht;

        txtpadding = 350;
                y = y + 5;
                posY = posY + 5;

                topheiht = 12;
                    g.drawText(saletxtterm, 20, y - topheiht, 0, w);
public Object get(ListField listField, int index) {
            if (Sales.size() > index) {
                return salechoiceterm[index].toString();
            }
            return null;
        }

        public int getPreferredWidth(ListField listField) {

            return 0;
        }

        public int indexOfList(ListField listField, String prefix, int start) {
            return listField.indexOfList(prefix, start);
        }
    }

this is the code to bind webservice data

     public void binddata() {
        ConnectionClass cls = new ConnectionClass();
        String Data = "";
        String URL = "";
        URL = "my webservice url here";
        try {

            Data = cls.CallWebService(URL);
            try {
                JSONArray jarr = new JSONArray(Data);
                        if (Application.isEventDispatchThread()) {
               for (int i = 0; i < data.length(); i++) {
                try {
                JSONObject jsonobj = new JSONObject(data.getString(i)
                            .toString());
                    Sales.addElement(new String(jsonobj.getString("DDLValue")));
                   SalesID.addElement(new SearchID(jsonobj
                            .getString("ID")));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            } else {
            Application.getApplication().invokeLater(new Runnable() {
                public void run() {
                    // updateField(data);
                }

            }

            );
        }

    }
    } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

please guide me i am new to blackberry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.