1

I'm trying to make a login screen by fetching Card No and PIN from database using xampp for ATM app by giving input as Card No and PIN then it is showing error as:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Atmcardno.jButton1MouseClicked(Atmcardno.java:228)
at Atmcardno.access$300(Atmcardno.java:15)
at Atmcardno$4.mouseClicked(Atmcardno.java:128)

My jButton1 code for login and fetching the Card No and Pin is shown below. I've been programming in java swing and need help.

    System.out.println("Starting...");

          if(((cardno.getText()).equals("")) && (pwd.getPassword().equals("")))
          {
           JOptionPane.showMessageDialog(this,"Please enter ATM Card No and PIN No.","Warning!!!",JOptionPane.WARNING_MESSAGE);
       }
          try{
              System.out.print("Starting1...");
                int foundrec=0;
                Class.forName("com.mysql.jdbc.Driver");
                Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","");
                System.out.println("Connected to database.");
      PreparedStatement ps= con.prepareStatement("select* from accountdetail where atmno='"+cardno.getText()+"' and pinno='"+pwd.getPassword()+"'");
                ResultSet rs= ps.executeQuery();
                while(rs.next())
                {
                    atno=rs.getInt(1);
                    System.out.println(atno);
                    acno=rs.getInt(2);
                    System.out.println(acno);
                    pno= rs.getInt(3);
                    System.out.println(pno);
                    String cardname= rs.getString(5);
                    System.out.println(cardname);
                    String expdate= rs.getString(7);

                    try
                    {
                        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-mm-dd");
                        Date d1= sdf.parse(curdate);
                        Date d2= sdf.parse(expdate);

                        System.out.println("Expiry date of ATM card is "+sdf.format(d2));
                        System.out.println("Today's date is "+sdf.format(d1));

                        if(d1.compareTo(d2)>=0)
                        {
                            JOptionPane.showMessageDialog(null, "Hello "+cardname);
                      //      new AccountType().setVisible(true);
                        }
                        else if(d1.compareTo(d2)<0)
                        {
                            JOptionPane.showMessageDialog(this, "Your ATM card is out of expiry date, please visit nearest branch for more information.","Warning",JOptionPane.WARNING_MESSAGE);
                            new Welcome().setVisible(true);
                          this.setVisible(false);  
                        }
                     }
                    catch(ParseException pe)
                         {
                              System.out.println("Exception in date format. "+pe);
                              pe.printStackTrace();
                         }
                    foundrec=1;
                }

                if(foundrec==0)
                {
                    JOptionPane.showMessageDialog(null, "Invalid ATM card no. or PIN no. ","Warning",JOptionPane.WARNING_MESSAGE);

                }

            }
            catch(Exception e) {
                e.printStackTrace();
            } 
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
  • You tell us. We don't know what line 228 is. We don't know what variable is null on that line. We don't know how you define your variables. Do some basic debugging to determine which variable is null and why. – camickr Nov 09 '18 at 16:52
  • Please mention the line `Atmcardno.java:228` in your code snippet. – Roshana Pitigala Nov 09 '18 at 16:53
  • See also: [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Nov 09 '18 at 16:55
  • Also, you are using a PreparedStatement incorrectly. The point of using the PreparedStatement is to specify tokens in the statement and then replace those tokens with appropriate values. See: https://stackoverflow.com/questions/24081941/jtable-fill-data-with-jcombobox/24083888#24083888 for the most basic example. – camickr Nov 09 '18 at 16:58

0 Answers0