0

I have initialized all my classes correctly although not shown in the code. I have tested if the source of the error was in my textfield but it wasn't. the "s" in the MainForm class is the initialization of the Sqlstatement class. There is one error here which is shown in the code below

my new_user class

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

There is one here too which is shown in the code.

my main_form class

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

my sqlstatement class

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

my stacktrace

java.lang.NullPointerException

at MainForm.search_for_dupe_user(MainForm.java:3151)

at new_user.save_user_btnActionPerformed(new_user.java:129)

Ram
  • 3,092
  • 10
  • 40
  • 56
fanboime12
  • 21
  • 2
  • 10

0 Answers0