欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java空指针不知道错哪 急救!!!

程序员文章站 2022-04-10 09:51:02
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerExceptionat wt.views.LoginView.loginUserActionPerformed(LoginView.java:156)at wt.views.LoginView.access$0(LoginView.java:154)at wt.views.LoginView2.actionPerformed(LoginView.java:86)atjavax.swi...

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at wt.views.LoginView.loginUserActionPerformed(LoginView.java:156)
at wt.views.LoginView.access$0(LoginView.java:154)
at wt.views.LoginView2.actionPerformed(LoginView.java:86)atjavax.swing.AbstractButton.fireActionPerformed(UnknownSource)atjavax.swing.AbstractButton2.actionPerformed(LoginView.java:86) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButtonHandler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue3.run(UnknownSource)atjava.security.AccessController.doPrivileged(NativeMethod)atjava.security.ProtectionDomain3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomainJavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue4.run(UnknownSource)atjava.security.AccessController.doPrivileged(NativeMethod)atjava.security.ProtectionDomain4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomainJavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

package wt.views;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import wt.dao.daouser;
import wt.model.User;
import wt.tool.tool;
import wt.tool.toolstring;

@SuppressWarnings("serial")
public class LoginView extends JFrame {


	
	private tool dbUtil = new tool();
	private daouser userDao = new daouser();

	private JPanel contentPane;
	private JTextField text_userName;
	private JPasswordField text_password;
	private JLabel text_name;
	private JPasswordField text_pass;

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

	/**
	 * Create the frame.
	 */
	public LoginView() {
		setTitle("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF-\u7528\u6237\u767B\u5F55");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 564, 452);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 19));
		
		text_userName = new JTextField();
		text_userName.setFont(new Font("宋体", Font.PLAIN, 19));
		text_userName.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel(" \u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF");
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 40));
		lblNewLabel_1.setIcon(new ImageIcon(LoginView.class.getResource("/picture/logo.png")));
		
		JLabel lblNewLabel_2 = new JLabel("\u5BC6  \u7801\uFF1A");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 19));
		
		text_password = new JPasswordField();
		text_password.setFont(new Font("宋体", Font.PLAIN, 19));
		
		JButton btnNewButton = new JButton("\u767B\u5F55");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginUserActionPerformed(e);
			}


		});
		btnNewButton.setIcon(new ImageIcon(LoginView.class.getResource("/picture/login.png")));
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 19));
		
		JButton btnNewButton_1 = new JButton("\u6CE8\u518C");
		btnNewButton_1.setIcon(new ImageIcon(LoginView.class.getResource("/picture/addUser.png")));
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 19));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(120)
							.addComponent(lblNewLabel)
							.addGap(15)
							.addComponent(text_userName, GroupLayout.PREFERRED_SIZE, 177, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(121)
							.addComponent(lblNewLabel_2)
							.addGap(13)
							.addComponent(text_password, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(119)
							.addComponent(btnNewButton)
							.addGap(98)
							.addComponent(btnNewButton_1))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(73)
							.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 382, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(81, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(41)
					.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(40)
							.addComponent(lblNewLabel)
							.addPreferredGap(ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
							.addComponent(lblNewLabel_2)
							.addGap(135))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addGap(35)
							.addComponent(text_userName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(35)
							.addComponent(text_password, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
							.addGap(53)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE))
							.addGap(40))))
		);
		contentPane.setLayout(gl_contentPane);
		//设置窗口剧中显示
		this.setLocationRelativeTo(null);
	}
	/**
	 * 登录按钮的方法
	 * @param e
	 */
	private void loginUserActionPerformed(ActionEvent evt) {

		String userName = text_name.getText();
		String password = new String(text_pass.getPassword());
		
		if(toolstring.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空");
			return;
		}
		if(toolstring.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		User user = new User(userName,password);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			User currentUser = userDao.login( con, user);
			if(currentUser != null){
				dispose();
				new MainView().setVisible(true);
			}else{
				JOptionPane.showMessageDialog(null, "用户名或密码错误");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

本文地址:https://blog.csdn.net/weixin_46894841/article/details/107272369