我正在使用 MVC 设计模式,我是设计模式和 java 的完全初学者。如果我没有把 LoginModel login = new LoginModel()); 会弹出 java.lang.nullPointerException。但是当我说的时候,这个 java.lang.StackOverflowError 发生了。我不确定我是否提供了足够的详细信息,但是如果您需要更多信息,请提一下,我会提供更多详细信息..
public class LoginModel {
private String userName;
private int id;
private String email;
private long startTime;
private long endTime;
Timer timer;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
LoginModel login = new LoginModel(); //this is where the error happens
public LoginModel(){}
public LoginModel(int id,String userName,String email){
this.id=id;
this.userName=userName;
this.email=email;
}
public int getID(){
return id;
}
public String getUserName(){
return userName;
}
public String getEmail(){
return email;
}
public void setStartTime(long time){
startTime=time;
}
public void setEndTime(long time){
endTime = time;
}
public long getUsedTime(){
return (endTime-startTime);
}
public LoginModel(int seconds) {
timer = new Timer();
timer.schedule(new LoginModel.RemindTask(), seconds*1000);
}
public void setTimer(int seconds){
timer = new Timer();
timer.schedule(new LoginModel.RemindTask(), seconds*1000);
}
class RemindTask extends TimerTask {
public void run() {
JOptionPane.showMessageDialog(null, "60 seconds left!");
timer.cancel();
}
}
public void RetrieveDbase (Connection conn){
String sql = "SELECT * FROM customer_info WHERE username = ? and password = ?";
String sql2 = "SELECT * FROM admin_info WHERE username = ? and password = ?";
String sql11 = "SELECT * FROM customer_info WHERE username = ? and password = ? and paidRemainingTime != 0";
try{
pst = conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Welcome "+jTextField1.getText());
try{
pst = conn.prepareStatement(sql11);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
rs = pst.executeQuery();
if (rs.next()){
new dummyHomepage().setVisible(true);
//this.setVisible(false);
JOptionPane.showMessageDialog(null,"Your Paid Time Remains "
+ rs.getInt("paidRemainingTime")+"Minutes");
login.setTimer(rs.getInt("paidRemainingTime"));
long startTime = System.currentTimeMillis( );
login.setStartTime(startTime);
}
else{
new PurchaseInterface().setVisible(true);
//this.setVisible(false);
}
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
else
try{
pst = conn.prepareStatement(sql2);
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Welcome ADMIN "+jTextField1.getText());
new ManageUserInterface().setVisible(true);
//this.setVisible(false);
}
else
JOptionPane.showMessageDialog(null, "Invalid username and password");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
public Connection getDbase(){
return conn;
}
}