这是我的简单代码。
我有一个问题。它给了我空指针异常。
谁能告诉我为什么它给我错误“空指针异常”?
我无法理解。
请回复您的想法。
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);
}
}
}