黑莓代码出错(空指针异常)

时间:2012-06-22 11:02:51

标签: java blackberry nullpointerexception blackberry-jde

这是我的简单代码。

我遇到了问题。它给我空指针异常。

任何人都可以告诉我为什么它会给我错误“空指针异常”?

我无法理解。

请回复你的想法。

public final class MyScreen extends MainScreen implements FieldChangeListener
{
    /**
     * Creates a new MyScreen object
     */
    MyTextField textfield1,textfield2,textfield3;
    FontFamily alphaSansFamily;
    ButtonField buttonNew,buttonFind;
    public MyScreen()
    {        
        System.out.println("In field Change Method");
        setTitle("Offline Dictionary");
        int width = Display.getWidth();
        HorizontalFieldManager hrm=new HorizontalFieldManager();

        MyTextField textfield1=new MyTextField(width-150, 50);
        textfield1.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
        textfield1.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),Border.STYLE_SOLID));
        try {
            alphaSansFamily = FontFamily.forName("BBAlpha Serif");
            Font appFont = alphaSansFamily.getFont(Font.PLAIN, 9, Ui.UNITS_pt);
            textfield1.setFont(appFont);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        textfield1.setText("");
        hrm.add(textfield1);

        buttonFind=new ButtonField("Search",FIELD_RIGHT|FIELD_VCENTER);
        buttonFind.setChangeListener(this);
        hrm.add(buttonFind);

        add(hrm);

        HorizontalFieldManager hrm1=new HorizontalFieldManager();

        MyTextField textfield2=new MyTextField(width-150, 50);
        textfield2.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
        textfield2.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),Border.STYLE_SOLID));
        try {
            alphaSansFamily = FontFamily.forName("BBAlpha Serif");
            Font appFont = alphaSansFamily.getFont(Font.PLAIN, 9, Ui.UNITS_pt);
            textfield1.setFont(appFont);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        textfield1.setText("");
        hrm1.add(textfield2);
        buttonNew=new ButtonField("New",FIELD_LEFT|FIELD_VCENTER);
        buttonNew.setChangeListener(this);
        hrm1.add(buttonNew);

        add(hrm1);


        textfield3=new MyTextField(width-60, 50);
        textfield3.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
        textfield3.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),Border.STYLE_SOLID));
        try {
            alphaSansFamily = FontFamily.forName("BBAlpha Serif");
            Font appFont = alphaSansFamily.getFont(Font.PLAIN, 9, Ui.UNITS_pt);
            textfield1.setFont(appFont);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        textfield3.setText("");
        add(textfield3);

    }
    public void fieldChanged(Field field, int context) {
        System.out.println("In field Change Method");
        if (field==buttonFind)
        {
            Dialog.inform("Find Button Pressed!");
            System.out.println("In Button Find");
            buttonNew.setEnabled(false);
            textfield2.setEditable(false);
            textfield3.setEditable(false);
        }
        else 
        {
            Dialog.inform("New Button Pressed!");
            buttonFind.setEnabled(false);
            textfield1.setEditable(false);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

在Eclipse中按F11调试应用程序。您应该能够看到抛出NullPointerException的确切行。