2013-05-02 49 views
0

我正在使用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; 
    } 
} 
+0

遞歸'新的LoginModel()'。你有一個實例字段'LoginModel login'。當你創建一個新的LoginModel時,它會創建一個新的LoginModel,令人生厭。 – 2013-05-02 18:37:20

回答

1

您的通話LoginModel login = new LoginModel();產生StackOverflowError,因爲它是遞歸的。當您嘗試創建LoginModel時,它會爲login實例變量創建另一個LoginModel,該實例變量將爲另一個LoginModel創建login實例變量,依此類推。

login實例變量的用途是什麼?如果只是刪除NullPointerException,那麼它不起作用。這只是掩蓋它。刪除它並嘗試其他方法來調試NullPointerException

NullPointerException的原因可能是因爲您的代碼中沒有設置這些實例變量。

  • jTextField1
  • jTextField2中
1
LoginModel login = new LoginModel(); 

login是在這種情況下,這意味着,它會爲類型LoginModel的每一個新的對象被創建的成員變量。

由於製作LoginModel對象創建成員變量login這是一個LoginModel,它創建了一個成員變量login這是一個LoginModel ......你有問題。