2013-10-09 70 views
0

我正試圖學習如何做MVC和DAO,但我的實現現在失敗了,我無法繞過它。Java MVC DAO實現獲取NullPointerException錯誤

這是我的看法,或它的相關部分:

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


} 

我的模型:

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


} 

我的控制器:

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

主營:

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

} 

的錯誤:

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) 

該行的錯誤引用:

btnLogin.setActionCommand("login"); //in View 
view.buttonActionListeners(this); //in Control 
LoginControl controller = new LoginControl(model, frame); //in main. 
從我已經試過

因此,要了解: view.buttonActionListeners(本);在這裏的某個地方,我傳遞了一個null, ,所以要麼是null或view是null? 如果我們啓動了 LoginFrame視圖,如何查看;

感謝您幫助我解決這個錯誤。

+0

我認爲你btnLogin屬性爲空,你會得到這個錯誤。但我沒有確定它如何可以是空的。 –

回答

1

好的...明白了。 您btnLogin屬性爲null,當你撥打電話到

view.buttonActionListeners(this); 

它發生,因爲它是在一個Runnable初始化,即是異步的,而且可能還不運行。 從這部分代碼中我看不到它需要異步調用。所以我會建議同步設置屬性。所以它應該是:

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); 
} 
+0

你說得對。現在我需要研究何時需要在GUI上使用Runnable。 – Harrison