我正在使用MVC設計模式,並且我是設計模式和java的完整初學者。 如果我沒有把LoginModel login = new LoginModel()); java.lang.nullPointerException將彈出。但是當我把它,這個java.lang.StackOverflowError發生。 我不知道,如果我給足夠的細節,但如果你需要更多的,只是提了,我會提供更多的細節..如何解決MVC設計模式中的java.lang.StackOverflowError(加上mySQL)
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;
}
}
遞歸'新的LoginModel()'。你有一個實例字段'LoginModel login'。當你創建一個新的LoginModel時,它會創建一個新的LoginModel,令人生厭。 – 2013-05-02 18:37:20