Skip to content
Advertisement

preparedstatement stopped working after inserting into database

I wanted to insert into table called accounts from user input but this is the error. It used to work but suddenly i dont know what happened and it doesnt work anymore

Error code:

Exception in thread “main” java.lang.NullPointerException

at pl.marcinek.sql.sql.register(sql.java:28)

at pl.marcinek.accounts.Register.Register(Register.java:32)

at pl.marcinek.Main.main(Main.java:19)

Sql class

public class sql {

    private static Connection conn = null;
    private static Staenter code heretement st = null;
    private static PreparedStatement preparedStmt = null;

    public static void sql() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "");
        st = conn.createStatement();

    }

    public static void register() throws SQLException, ClassNotFoundException {
        sql.sql();
        String query = " insert into accounts (username, password, name, surname, balance, authcode)" + " values (?, ?, ?, ?, ?, ?)";
        conn.prepareStatement(query);

        preparedStmt.setString(1, Register.username); //error
        preparedStmt.setString(2, Register.password);
        preparedStmt.setString(3, Register.name);
        preparedStmt.setString(4, Register.surname);
        preparedStmt.setString(5, Register.balance);
        preparedStmt.setString(6, Authenticator.authenticator());
        preparedStmt.executeUpdate();
    }

Register class

public class Register {
    public static String username;
    public static String password;
    public static String name;
    public static String surname;
    public static String balance;


    public static void Register() throws InterruptedException, SQLException, ClassNotFoundException {
        Scanner reg = new Scanner(System.in);
        System.out.println("register in this order...");
        TimeUnit.SECONDS.sleep(2);
        System.out.println("username, password, name, surname, balance");

        username = reg.nextLine();
        password = reg.nextLine();
        name = reg.nextLine();
        surname = reg.nextLine();
        balance = reg.nextLine();


        System.out.println("you were correctly registered");
        System.out.println("your authenticating code is: ");
        sql.register(); //error
        TimeUnit.SECONDS.sleep(1);
        sql.id();
        System.out.println(" ");
        System.out.println("dont show your to anyone");
    }
}

Main class

public class Main {

    public static void main(String[] args) throws InterruptedException, SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("choose one");
        System.out.println("1:log in");
        System.out.println("2:register");
        String opcja = scanner.nextLine();

        if(opcja.equals("1")){
            AccountManager.accoutmanager();
        }
        if(opcja.equals("2")){
            Register.Register(); // Error
        }
    }
}

Advertisement

Answer

You forgot to assign result of call to Connection#prepareStatement() into variable:

conn.prepareStatement(query);
preparedStmt.setString(1, Register.username); //error

should be

preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, Register.username);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement