1

I am developing s60 using j2me with LWUIT in Eclipse.

I am writing this method to draw list item and try to create List manually rather than using Lwuit list. Because as i posted in my last question here is LinK.. don't know why but it decreases performance.

So In below method i trying to create in which i'm adding adding two labels to layoutX Container and adding that Conatiner to layoutY Container and Adding that layoutY to BaseContainer so output is looks like list.

Method is here ...

private void drawAgendasListItem(Vector vector) {

        Container containerX[] = new Container[vector.size()];
        Container containerY[] = new Container[vector.size()];

        if (featuredeventsForm.contains(baseContainer)) {
            baseContainer.removeAll();
            featuredeventsForm.removeComponent(baseContainer);
            System.out.println("base Container is removed ");
        }

        BoxLayout layoutX = new BoxLayout(BoxLayout.X_AXIS);
            BoxLayout layoutY = new BoxLayout(BoxLayout.Y_AXIS);

        for (int i = 0; i < vector.size(); i++) {

        try {
                containerX[i].setLayout(layoutX);
                containerY[i].setLayout(layoutY);

                Label startTime = new Label();
                Label description = new Label();

                startTime.getStyle().setBgTransparency(0);
                startTime.setText("start 10:20 Am");
                startTime.getStyle().setMargin(0, 0, 0, 5);

                description.getStyle().setBgTransparency(0);
                description.setText("decriptionString");

                containerX[i].getStyle().setPadding(0, 0, 2, 2);
                containerX[i].addComponent(startTime);
                containerX[i].addComponent(description);

                containerY[i].addComponent(containerX[i]);
                baseContainer.addComponent(i, containerX[i]);

                System.out.println("Component added to base Container @ " + i);

            } catch (Exception e) {
                System.out.println("Exception in drawAgendaListItem " + e);
            }
        }
        featuredeventsForm.addComponent(baseContainer);
        featuredeventsForm.invalidate();
        featuredeventsForm.repaint();
        System.out.println("All elements added and form repainted");

    }

In above method when i try to assign layout to Container it fires an NullPointerException at line containerX[i].setLayout(layoutX);.

I don't understand why it's happening, I was also try to comment that lines then it fires NullPointerException at line containerX[i].getStyle().setPadding(0, 0, 2, 2);.

please help ....

Community
  • 1
  • 1
MobileEvangelist
  • 2,583
  • 1
  • 25
  • 36

1 Answers1

1

Based on the source code, my guess is that you think instantiating an array also populates it. That is not the case in Java.

In other word, if you think that containerX looks like:
[new Container, new Container,..., new Container]
in memory, that is incorrect. It actually looks like:
[null,null,...,null]

I think you need to add

containerX[i] = new Container();
containerY[i] = new Container();

at the beginning of the loop.

(Maybe you want to instantiate the contents of the arrays as subclasses of Container)

michael aubert
  • 6,836
  • 1
  • 16
  • 32
  • i added these lines, containerX[i] = new Container(layoutX); containerY[i] = new Container(new BoxLayout(BoxLayout.Y_AXIS)); but it still not working . the same error fires – MobileEvangelist Jul 13 '12 at 10:25
  • i posted in my last question problem with List rendering [here is LinK..](http://stackoverflow.com/questions/11414861/in-listcellrenderer-getlistcellrenderercomponent-method-called-more-than-once) – MobileEvangelist Jul 13 '12 at 10:27
  • please help me.., i want to add to-do item to my s60 phones programmatically i'm using lwuit, i surfed a lot but not single result was helpful.. – MobileEvangelist Jul 16 '12 at 14:09