从另一个类调用方法时出现NullPointerException

时间:2015-02-20 12:49:58

标签: java netbeans

虽然代码中没有显示,但我已正确初始化了所有类。我已经测试过错误的来源是否在我的textfield中,但它不是。 " s"在MainForm类中是Sqlstatement类的初始化。这里有一个错误,显示在下面的代码中

我的new_user

public new_user() {
    initComponents();

    try{

        mf = new MainForm();

    }catch(Exception e){
        e.printStackTrace();
    }
}private void save_user_btnActionPerformed(java.awt.event.ActionEvent evt) {                                              

    try{
        user = uname_input_txt.getText();
        pass = pass_input_txt.getText();

        //ERROR HERE
        if(mf.search_for_dupe_user(user) == true){
            JOptionPane.showMessageDialog(null, "Username not available");
        }else{
            mf.new_user(user, pass);
            JOptionPane.showMessageDialog(null, "New user added.");
            this.dispose();
        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e.getMessage());
        e.printStackTrace();
    }
}

这里也有一个显示在代码中。

我的main_form

public void new_user(String user, String pass)throws SQLException{
    try{
        s.new_user(user, pass);
    }catch(Exception e){
        e.printStackTrace();
    }
}

public boolean search_for_dupe_user(String user)throws SQLException{

   boolean result = false;

   try{ 
        //ERROR HERE
        if(s.search_for_dupe_user(user) == true){
            result = true;
        }else{
            result = false;
        }
   }catch(Exception e){
       e.printStackTrace();
   }

   return result;
}

我的sqlstatement

public void new_user(String user, String pass)throws SQLException{
    try{
        stmt = conn.prepareStatement("insert into users (user_num, username, password) values(null,'"+user+"','"+pass+"')");
        stmt.execute();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public boolean search_for_dupe_user(String user)throws SQLException{

   boolean result= false;

   ResultSet rs = null;
   try{
        rs = statement.executeQuery("select username from users where username='"+user+"'");

        if(rs.next())
        {
           result = true;
        }

        else
        {
           result = false;
        }
   }catch(Exception e){
       e.printStackTrace();
   }

   return result;
}

我的stacktrace

  

java.lang.NullPointerException

     

在MainForm.search_for_dupe_user(MainForm.java:3151)

     

at new_user.save_user_btnActionPerformed(new_user.java:129)

0 个答案:

没有答案