简体   繁体   English

Java MVC DAO实现收到NullPointerException错误

[英]Java MVC DAO implementation getting NullPointerException error

I'm trying to learn how to do MVC and DAO but my implementation is failing right now and I can't get around it. 我正在尝试学习如何做MVC和DAO,但是我的实现现在失败了,我无法解决。

Here is my view, or the relevant parts of it: 这是我的观点,或它的相关部分:

package gui;

import javax.swing.JFrame;

public class LoginFrame{

    private JPanel contentPane;
    private final JLabel credentialsLabel = new JLabel("Credentials");
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton btnLogin;

    /**
     * Create the frame.
     */
    public LoginFrame() {
        createAndShowGUI();

    }

    private void createAndShowGUI()
    {
            EventQueue.invokeLater(new Runnable() {
            public void run() {
            try {
                    JFrame frame = new JFrame();
                    frame.setTitle("Login");
                    frame.setContentPane(createContentPane());
                    frame.setBounds(100, 100, 480, 237);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } 
        });

    }


    //A function that sets up the basic structure of the GUI.
    private JPanel createContentPane() {

        contentPane = new JPanel();
        contentPane.setForeground(Color.LIGHT_GRAY);
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{106, 0, 0, 153, 0};
        gbl_contentPane.rowHeights = new int[]{65, 27, 39, 36, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);



        //login button
        btnLogin = new JButton("login");
        GridBagConstraints gbc_btnLogin = new GridBagConstraints();
        gbc_btnLogin.anchor = GridBagConstraints.WEST;
        gbc_btnLogin.insets = new Insets(0, 0, 5, 5);
        gbc_btnLogin.gridx = 2;
        gbc_btnLogin.gridy = 3;
        contentPane.add(btnLogin, gbc_btnLogin);

        //register button 
        JButton btnRegister = new JButton("register");
        btnRegister.setFont(new Font("Helvetica", Font.PLAIN, 13));
        GridBagConstraints gbc_btnRegister = new GridBagConstraints();
        gbc_btnRegister.anchor = GridBagConstraints.WEST;
        gbc_btnRegister.insets = new Insets(0, 0, 5, 0);
        gbc_btnRegister.gridx = 3;
        gbc_btnRegister.gridy = 3;
        contentPane.add(btnRegister, gbc_btnRegister);
        return contentPane; 
    }

    //the Action Listener for the Login Button passed by controller
    public void buttonActionListeners(ActionListener al) {
        btnLogin.setActionCommand("login");
        btnLogin.addActionListener(al); 
    }


}

My Model: 我的模特:

package functions;

import database.LoginDAO;

public class LoginModel {
    LoginDAO logindao; 
    public LoginModel(LoginDAO logindao){
        this.logindao = logindao;
    }

    public void attemptLogin(String username, char[] password) {
        System.out.println("Testing login attempt"); 
        logindao.attemptLogin(username, password); 
    }


}

My controller: 我的控制器:

package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import functions.LoginModel;
import gui.LoginFrame;

public class LoginControl implements ActionListener {
    LoginModel model; 
    LoginFrame view; 

    public LoginControl(LoginModel model, LoginFrame view){
        this.model = model; 
        this.view = view;

        //add action listener from control to view. 
        view.buttonActionListeners(this); 
    }

    //action performed by view
    public void actionPerformed(ActionEvent ae)
    {
        System.out.println("Testing Action Performed");
        String action = ae.getActionCommand(); 
        if(action.equals("login")){
            System.out.println("Testing Action Performed");
            model.attemptLogin(view.getUsername(),view.getPassword()); 
        }
    }
}

Main: 主要:

import functions.LoginModel;
import java.io.IOException;
import java.sql.SQLException;
import controller.LoginControl;
import database.LoginDAO;
import gui.LoginFrame;

public class Main {

    public static void main(String[] args) throws IOException, SQLException {
        LoginFrame frame = new LoginFrame(); 
        LoginDAO loginDao = new LoginDAO(); 
        LoginModel model = new LoginModel(loginDao);
        LoginControl controller = new LoginControl(model, frame); 
    }

}

The Error: 错误:

Exception in thread "main" java.lang.NullPointerException
    at gui.LoginFrame.buttonActionListeners(LoginFrame.java:160)
    at controller.LoginControl.<init>(LoginControl.java:16)
    at Main.main(Main.java:14)

The lines that the error references: 错误所引用的行:

btnLogin.setActionCommand("login"); //in View
view.buttonActionListeners(this);  //in Control
LoginControl controller = new LoginControl(model, frame); //in main.

So from what I've tried to understand: view.buttonActionListeners(this); 因此,根据我试图理解的内容:view.buttonActionListeners(this); somewhere in here I'm passing a null, so either this is null or view is null? 我在这里的某个地方传递了null,所以这是null还是view是null? how can view be null if we initiated LoginFrame view; 如果我们启动了LoginFrame视图,视图如何为null;

Thanks for helping me solve this bug. 感谢您帮助我解决此错误。

Ok... got it. 好的,我知道了。 your btnLogin property is null when you make the call to 当您呼叫时,您的btnLogin属性为null

view.buttonActionListeners(this);

It happens because it is initialized on a Runnable, that is asynchronous, and probably didn't run yet. 发生这种情况是因为它是在Runnable上初始化的,它是异步的,并且可能尚未运行。 From this part of the code I don't see the need to it be called asynchronously. 从这部分代码中,我看不到需要异步调用它。 So I would suggest to set the properties synchronously. 因此,我建议同步设置属性。 So it should be: 因此应该是:

 private void createAndShowGUI()
{

                JFrame frame = new JFrame();
                frame.setTitle("Login");
                frame.setContentPane(createContentPane());
                frame.setBounds(100, 100, 480, 237);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM