0

The problem is that NullPointException occurs at st = con.createStatement(); rs = st.executeQuery("select * from noit"); rs.next(); but they are declared to be null. The first code is for connection to MySQL and the second is to give a function to a button.

Here is his code:

public class database {
public void con(){
Connection con = null;
Statement st = null;
ResultSet rs = null;
String nqkuvsi = null;
try{
    con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/userlogin","root","1234");       
    if(con!=null){
        System.out.println("successfull");
       }}catch(Exception e){
    System.out.println("no connection");
    e.printStackTrace();
}
}
}

Second code:

public void actionPerformed(ActionEvent a){
db.con();    
    ResultSet res = null;       
    int resul = 0;
    Object s = a.getSource();           
    if(s==jbut){
     Random rand = new Random();
        Random rand1 = new Random();
             int rnd = rand.nextInt(2)+1;           
             int rnd1 = rand1.nextInt(2) + 2;
         String nqkuv = null;
         String nqkuv1 = null,nqkuv2 = null,nqkuv3 = null;
         if(rnd==1){                 
             try{               
             st = con.createStatement();
             rs = st.executeQuery("select * from noit");
             rs.next();
             nqkuv = rs.getString(rnd1);
         }catch(SQLException e){
             e.printStackTrace();
         }

Any help appreciated!

tnw
  • 13,521
  • 15
  • 70
  • 111
shashnatiq
  • 31
  • 2

0 Answers0