7 Replies - 1292 Views - Last Post: 12 January 2012 - 03:12 AM Rate Topic: -----

#1 RosyS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 12

java.lang.NullPointerException

Posted 11 January 2012 - 12:25 PM

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
if(count<3)
   {  
       try
       {   
           conn = DbConnection.ConnectDB();
          ps=conn.prepareStatement("select * from LOGIN_TBL where USERNAME='"+value1+"' and PASSWORD='"+value2+"'");              	  
          ps.setString(1,jTextField1.getText());
          ps.setString(2,jPasswordField1.getText());
          ResultSet rs=ps.executeQuery();
       
          if(rs.next())
          { 
              uname=rs.getString("USERNAME");
              pass=rs.getString("PASSWORD");
          }
          if(value1.equals(uname) && value2.equals(pass))
          {
           String str = "Dear " + jTextField1.getText() + ".\nWelcome ";
           JOptionPane.showMessageDialog(null,str, "Welcome", JOptionPane.INFORMATION_MESSAGE );
           d.setVisible(true);
          }
          else if(value1.equals("") || value2.equals(""))
          {
              JOptionPane.showMessageDialog(null,"UserId OR Password cannot be empty!","Error",JOptionPane.ERROR_MESSAGE);             
              jTextField1.requestFocus();     
           }
          else if (!value1.equals(uname) || !value2.equals(pass))    
          {          
               JOptionPane.showMessageDialog(null,"Please enter correct UserId or Password!","Error",JOptionPane.ERROR_MESSAGE);
               jTextField1.setText("");
               jPasswordField1.setText("");
               jTextField1.requestFocus();
          }
         }
                         
        catch(Exception e)
        {    
          System.out.println(e);
         }
         count++;  
         
     }  

}   

this is my login form                                


public class DbConnection {
     static Connection conn=DbConnection.ConnectDB();
        static Statement st;
        static Statement rs;
    public static Connection ConnectDB(){ 
    try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection ("jdbc:odbc:INVENTORY_MNGT");
     JOptionPane.showMessageDialog(null, "Connected");
     return conn;     
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
        return null;
    } 
}
}


this is connection class
but when i run the login form, it the form opens but when i click on login button gives an null pointer exception.....just wanted to know what my error is

Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.NullPointerException

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: java.lang.NullPointerException

Posted 11 January 2012 - 12:43 PM

*Moved to Java*

A NullPointerException means you are invoking a method, referencing a variable or constant, etc., from a non-primitive variable that is null. Without the stack trace, we can't help you. Also, we do not support the NetBeans GUI Builder b/c it produces such horrible code. Write your code by hand and we will be happy to help.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java.lang.NullPointerException

Posted 11 January 2012 - 06:31 PM

If it is a PreparedStatement

          ps=conn.prepareStatement("select * from LOGIN_TBL where USERNAME=? and PASSWORD=?");                  
          ps.setString(1,jTextField1.getText());
          ps.setString(2,jPasswordField1.getText());



and if there is a rs.next() it means you find the record


         if(rs.next())
          { 
            // we have a match
              // uname=rs.getString("USERNAME");
              // pass=rs.getString("PASSWORD");
          }


Was This Post Helpful? 4
  • +
  • -

#4 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: java.lang.NullPointerException

Posted 11 January 2012 - 06:34 PM

On another note, this looks like it's vulnerable to SQL injection. I'm not sure if it is or not, but I didn't notice any code to prevent it. Be careful if this is a public application, people could easily manipulate your DB.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: java.lang.NullPointerException

Posted 11 January 2012 - 06:36 PM

One more reason then to use a PreparedStatement as pbl suggested, since they are immune to SQL Injection attacks. :)
Was This Post Helpful? 1
  • +
  • -

#6 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: java.lang.NullPointerException

Posted 11 January 2012 - 06:44 PM

View Postmacosxnerd101, on 11 January 2012 - 08:36 PM, said:

One more reason then to use a PreparedStatement as pbl suggested, since they are immune to SQL Injection attacks. :)


Oh! Thanks for the tip!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java.lang.NullPointerException

Posted 11 January 2012 - 06:57 PM

View PostMATTtheSEAHAWK, on 11 January 2012 - 08:34 PM, said:

On another note, this looks like it's vulnerable to SQL injection.

No, it is a PreparedStatement, badly used but still a PreparedStatement
Was This Post Helpful? 1
  • +
  • -

#8 RosyS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 12

Re: java.lang.NullPointerException

Posted 12 January 2012 - 03:12 AM

View Postpbl, on 11 January 2012 - 06:57 PM, said:

View PostMATTtheSEAHAWK, on 11 January 2012 - 08:34 PM, said:

On another note, this looks like it's vulnerable to SQL injection.

No, it is a PreparedStatement, badly used but still a PreparedStatement


View Postpbl, on 11 January 2012 - 06:31 PM, said:

If it is a PreparedStatement

          ps=conn.prepareStatement("select * from LOGIN_TBL where USERNAME=? and PASSWORD=?");                  
          ps.setString(1,jTextField1.getText());
          ps.setString(2,jPasswordField1.getText());



and if there is a rs.next() it means you find the record


         if(rs.next())
          { 
            // we have a match
              // uname=rs.getString("USERNAME");
              // pass=rs.getString("PASSWORD");
          }


Thank you sooooooo much..my problem resolved....thank you once again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1