0

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

2 回答 2

1

您的调用LoginModel login = new LoginModel();会产生 aStackOverflowError因为它是递归的。当您尝试创建一个LoginModel时,它会LoginModellogin实例变量创建另一个,这会LoginModel为其实例变量创建另一个 login依此类推。

login实例变量的目的是什么?如果只是删除NullPointerException,那么它不起作用。它只是掩盖它。删除它并尝试其他方法来调试NullPointerException.

的原因NullPointerException可能是因为这些实例变量都没有在您的代码中设置。

  • jTextField1
  • jTextField2
于 2013-05-02T18:39:06.543 回答
1
LoginModel login = new LoginModel();

login在这种情况下是一个成员变量,这意味着它将为每个类型的新对象创建LoginModel

由于创建一个LoginModel对象会创建一个成员变量logina LoginModel,这会创建一个成员变量logina LoginModel... 你有问题。

于 2013-05-02T18:39:41.117 回答