我正在尝试通过使用xampp for ATM应用程序从数据库中获取卡号和PIN来进行登录,方法是提供输入卡号和PIN,然后显示错误为:
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)
我的jButton1代码用于登录并获取卡号和密码如下所示。我一直在用Java swing编程,需要帮助。
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();
}