user2344339
user2344339

Reputation: 1

How to solve java.lang.StackOverflowError in MVC design pattern (plus mySQL)

I'm using MVC design pattern, and I'm a complete beginner in design pattern and java. If I didn't put the LoginModel login = new LoginModel()); java.lang.nullPointerException will pop up. But when I put it, this java.lang.StackOverflowError happens. I'm not sure if I give enough details, but if you need more, just mention, I'll provide more details..

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;
    }
}

Upvotes: 0

Views: 152

Answers (2)

rgettman
rgettman

Reputation: 178303

Your call LoginModel login = new LoginModel(); produces a StackOverflowError because it's recursive. When you attempt to create a LoginModel, it creates another LoginModel for the login instance variable, which creates another LoginModel for its login instance variable, and so on.

What is the purpose of the login instance variable? If it's just to remove the NullPointerException, then it's not working. It's just covering it up. Remove it and try something else to debug the NullPointerException.

The cause of the NullPointerException is probably because neither of these instance variables are ever set in your code.

  • jTextField1
  • jTextField2

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 181037

LoginModel login = new LoginModel();

login is a member variable in this case, which means that it'll be created for every new object of the type LoginModel.

Since creating a LoginModel object creates the member variable login which is a LoginModel, which creates a member variable login which is a LoginModel... you have a problem.

Upvotes: 1

Related Questions