-1

これは私の単純なコードです。

問題があります。nullポインタ例外が発生します。

「NULLポインター例外」というエラーが表示される理由を誰かに教えてもらえますか?

理解できません。

それについてのあなたの考えを答えてください。

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);
        }

    }
}
4

1 に答える 1

0

Eclipseで、F11キーを押してアプリケーションをデバッグします。NullPointerExceptionがスローされた正確な行を確認できるはずです。

于 2012-06-22T16:49:53.797 に答える