2013-10-09 3 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이거나 뷰가 null입니까? 우리가 시작하면 어떻게 볼 수 있습니다 null LoginFrame보기;

이 버그를 해결할 수 있도록 도와 주셔서 감사합니다.

+0

나는이 오류가 발생하면 btnLogin 속성이 null이라고 생각합니다. 그러나 나는 그것이 아직 null 일 수있는 방법을 밝히지 않았다. –

답변

1

좋아 ... 알았어. 당신이

가의 Runnable에 초기화되기 때문에 발생
view.buttonActionListeners(this); 

에 전화를 걸 때 btnLogin 속성이 null, 즉 비동기, 아마도 아직 실행하지 않았다. 코드의이 부분에서 비동기 적으로 호출 할 필요가 없습니다. 따라서 속성을 동 기적으로 설정하는 것이 좋습니다. 따라서 :

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

관련 문제