Java gettext()空指针异常

Java gettext()空指针异常,java,Java,考虑以下代码 public class sqldetails extends JFrame implements ActionListener{ static String username=null; static String password=null; JButton button; JTextField utext,ptext; public static void main(String[] args){ new sqldetails(); } sqldetails() {

考虑以下代码

public class sqldetails extends JFrame implements ActionListener{

static String username=null;
static String password=null;
JButton button;
JTextField utext,ptext;

public static void main(String[] args){
new sqldetails();
}

sqldetails() {
    this.setTitle("Sql details");
    JPanel panel=new JPanel();
    panel.setLayout(new GridBagLayout());

    JLabel ulabel=new JLabel("Enter your Sql username");
    panel.add(ulabel);
    addItem(panel,ulabel,0,0,1,1,GridBagConstraints.WEST);

    JTextField utext=new JTextField(9);
    panel.add(utext);
    addItem(panel,utext,1,0,1,1,GridBagConstraints.WEST);

    JLabel plabel=new JLabel("Enter your Sql password");
    panel.add(plabel);
    addItem(panel,plabel,0,1,1,1,GridBagConstraints.WEST);

    JTextField ptext=new JTextField(9);
    panel.add(ptext);
    addItem(panel,ptext,1,1,1,1,GridBagConstraints.WEST);

    button =new JButton("enter");
    button.addActionListener(this);
    panel.add(button);
    addItem(panel,button,0,2,1,1,GridBagConstraints.CENTER);
    this.add(panel);
    this.pack();
    this.setVisible(true);
}

private void addItem(JPanel panel, JComponent c, int i, int j,
        int k, int l, int align) {
    GridBagConstraints calc=new GridBagConstraints();
    calc.gridx=i;
    calc.gridy=j;
    calc.gridwidth=k;
    calc.gridheight=l;
    calc.weightx=100.0;
    calc.weighty=100.0;
    calc.insets=new Insets(5,5,5,5);
    calc.anchor=align;
    calc.fill=GridBagConstraints.NONE;
    panel.add(c,calc);      

}


public void actionPerformed(ActionEvent arg0) {
    if(arg0.getSource()==button){
        username +=utext.getText();
        utext.setText(null);
        password +=ptext.getText();
        ptext.setText(null);
        new sqlconnection();
        new gui();
        new first();
    }
}
不管我如何编译它,我都会在gettext()行中得到java.lang.NullPointerException。
非常感谢您的帮助:)

这是因为构造函数sqldetails()中的变量utext和ptext隐藏了同名的实例变量

sqldetails() {
    .....

    // JTextField utext=new JTextField(9); //====> shadows the instance variable utext
    this.utext=new JTextField(9); // This is the correct use
    panel.add(utext);
    addItem(panel,utext,1,0,1,1,GridBagConstraints.WEST);

    .......

    // JTextField ptext=new JTextField(9); //=====> shadows the instance variable ptext
    this.ptext = new JtextField();  // This is the correct use

    .......
}

这是因为构造函数sqldetails()中的变量utext和ptext隐藏了同名的实例变量

sqldetails() {
    .....

    // JTextField utext=new JTextField(9); //====> shadows the instance variable utext
    this.utext=new JTextField(9); // This is the correct use
    panel.add(utext);
    addItem(panel,utext,1,0,1,1,GridBagConstraints.WEST);

    .......

    // JTextField ptext=new JTextField(9); //=====> shadows the instance variable ptext
    this.ptext = new JtextField();  // This is the correct use

    .......
}