Lỗi NullPointerException khi chuyển qua lại các jFrame

Cho em hỏi sao lúc em chuyển qua lại giữa các frame lại bị lỗi như vậy với, em đang bắt đầu làm quen với java swing nên có j sai sót mong mọi người bổ sung với luôn.

Frame này lúc mình đăng xuất thì nó báo lỗi như ở dưới

    public class Home extends JFrame {

    	private JPanel contentPane;
    	static Home frame;

    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					frame = new Home();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}

    	/**
    	 * Create the frame.
    	 */
    	public Home() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		
    		JMenuBar menuBar = new JMenuBar();
    		setJMenuBar(menuBar);
    		
    		JMenu mnHThng = new JMenu("H\u1EC7 Th\u1ED1ng");
    		menuBar.add(mnHThng);
    		
    		JMenuItem mntmngNhp = new JMenuItem("\u0110\u0103ng nh\u1EADp");
    		mnHThng.add(mntmngNhp);
    		
    		JMenuItem mntmngXut = new JMenuItem("\u0110\u0103ng xu\u1EA5t");
    		mntmngXut.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
   			frame.setVisible(false);
    				
    			}
    		});
    		mnHThng.add(mntmngXut);
    		
    		JMenuItem mntmThot = new JMenuItem("Tho\u00E1t");
    		mnHThng.add(mntmThot);
    		
    		JMenu mniuKhin = new JMenu("\u0110i\u1EC1u khi\u1EC3n");
    		menuBar.add(mniuKhin);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    	}

    	}

Đây là frame chính, lúc mình login thành công nó sẽ ẩn đi và hiện cái trang chủ phía trên


JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(txtUser.equals("")) {
					JOptionPane.showMessageDialog(null, "Tên người dùng không được để trống");
				} else if(txtPass.equals("")) {
					JOptionPane.showMessageDialog(null,"Mật khẩu không được để trống");
				} else {
					try {
						Connection conn = ConnectionsUtls.getMySqlConnection();
						String sql = "select * from Admin where userName = ? and userPass = ?";
						try {
							PreparedStatement ps = conn.prepareStatement(sql);
							ps.setString(1, txtUser.getText());
							ps.setString(2, txtPass.getText());
							ResultSet rs = ps.executeQuery();
							while(rs.next()) {
								Home home = new Home();
								home.setVisible(true);
								frame.setVisible(false);
								
							}
							
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					} catch (SQLServerException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
			}
		});

Làm gì phải cực khổ thế, dùng biến tĩnh, gán rồi “quên” gán giá trị mới cho nó sau khi bị hủy.

Thay

Bằng

public void actionPerformed(ActionEvent arg0) { 
    Home.this.setVisible(false);
    // Home.this.dispose();
}
3 Likes

Cảm ơn bạn, mình sửa lại được rồi, mấy hôm nay cứ gg mà k biết sửa thế nào, may có b giúp :slight_smile:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?