Custome Field took a nullpointexception in the labefiled and 7.1 paint method only you have this problem! Pls help me?

KIM I create a custom entry field as below:

public class AddEditField extends HorizontalFieldManager {
    private LabelField labelField;
    private EditField contentField;
    private RichTextField wrapaddField;
    HorizontalFieldManager textMgr;
    HorizontalFieldManager wrapMgr;
    private static final int HPADDING = Display.getWidth() <= 320 ? 6 : 8;
    Font font;

    public AddEditField(String label, Font font, int maxSpace) {
        this(label, 0, font, maxSpace);
    }

    public AddEditField(String label, long style, Font cfont, int maxSpace) {
        super(NO_HORIZONTAL_SCROLL);
        this.font = cfont;
        textMgr = new HorizontalFieldManager(HORIZONTAL_SCROLL | FIELD_VCENTER);
        wrapMgr = new HorizontalFieldManager(FIELD_VCENTER);
        wrapaddField = new RichTextField(RichTextField.TEXT_ALIGN_LEFT);
        long cstyle = 0;
        if (style == 0) {
            cstyle = EditField.NO_NEWLINE | EditField.FOCUSABLE
                    | EditField.EDITABLE | FIELD_VCENTER;
        } else {
            cstyle = EditField.NO_NEWLINE | EditField.FOCUSABLE
                    | EditField.EDITABLE | style | FIELD_VCENTER;
        }
        contentField = new EditField(cstyle) {
            protected void paint(Graphics graphics) {

                // Font font = getFont().derive(Font.PLAIN, fontSize);
                graphics.setFont(font);
                graphics.setColor(0x666666);
                super.paint(graphics);
            }

            protected void onFocus(int direction) {
                super.onFocus(direction);
                invalidate();
            }

            protected void onUnfocus() {
                super.onUnfocus();
                // if (VirtualKeyboard.isSupported()) {
                // VirtualKeyboard vKeyboard =getVirtualKeyboard();
                // vKeyboard.setVisibility(VirtualKeyboard.HIDE);
                // }

            }
        };
        labelField = new LabelField(label, FIELD_LEFT | FIELD_VCENTER) {
            protected void paint(Graphics graphics) {
                graphics.setColor(0x006699);
                graphics.setFont(font);
                super.paint(graphics);
            }
        };
        LabelField spaceF = new LabelField() {

            protected void layout(int width, int height) {
                // TODO Auto-generated method stub
                //super.layout(HPADDING, font.getHeight() + 10);
                setExtent(HPADDING, font.getHeight() + 10);
            }
        };
        add(spaceF);
        add(labelField);
        final int space = maxSpace - font.getAdvance(labelField.getText());
        LabelField spaceF2 = new LabelField() {

            protected void layout(int width, int height) {
                // TODO Auto-generated method stub
                // super.layout(space, font.getHeight() + 10);
                setExtent(space, font.getHeight() + 10);
            }
        };
        add(spaceF2);
        textMgr.add(contentField);
        wrapMgr.add(wrapaddField);
        add(textMgr);
    }

    public String getText() {
        return contentField.getText();
    }

    public void setText(String text) {
        synchronized (UiApplication.getEventLock()) {
            wrapaddField.setText(text);
            contentField.setText(text);
        }
    }

    public void setEditMode(boolean editable) {
        String text = wrapaddField.getText();
        synchronized (UiApplication.getEventLock()) {
            if (editable) {
                contentField.setText(text);
                if (wrapMgr.getManager() == this) {
                    delete(wrapMgr);
                }
                if (textMgr.getManager() != this) {
                    add(textMgr);
                }
            } else {
                if (textMgr.getManager() == this) {
                    delete(textMgr);
                }
                if (wrapMgr.getManager() != this) {
                    add(wrapMgr);
                }
            }
        }
    }

}

Then I create: new AddEditField ("address:", cFont, maxSpace);  and add it to a screen.

When I try to open this form. It is nullpointexception: the eclipse debugger show the info such as:

AddEditField$ 4labelfield.paint line 385.

Please help me to check the code. why it can run in 0 s 7.0 and before, only 7.1 has had this problem?

Thank you

Hi Flybrid,

I checked at the time of your code,

I am able to run your code by deleting page layout override methods in two fields of the label.

The problem with the substitution of the method of layout in the spaceF and spaceF2 fields.

But I can't explain why.

I think that you can achieve the same thing by replacing getPrefferedWidth() and getPrefferedHeight() above the fields instead of page layout.

Thank you.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for