1. ホーム
  2. 例外事項

スレッド "AWT-EventQueue-0" で例外発生 java.lang.NullPointerException

2022-02-21 09:54:44

数日前、生徒のクラス選択システムの作業中にヌルポインタの例外に遭遇しました。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.yfl.service.LoginService.loginValidate(LoginService.java:17)
	at org.yfl.view.Login.actionPerformed(Login.java:125)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6297)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
	at java.awt.Component.processEvent(Component.java:6062)
	at java.awt.Container.processEvent(Container.java:2039)
	at java.awt.Component.dispatchEventImpl(Component.java:4660)
	at java.awt.Container.dispatchEventImpl(Container.java:2097)
	at java.awt.Component.dispatchEvent(Component.java:4488)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
	at java.awt.Container.dispatchEventImpl(Container.java:2083)
	at java.awt.Window.dispatchEventImpl(Window.java:2489)
	at java.awt.Component.dispatchEvent(Component.java:4488)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
	at java.awt.EventQueue.access$400(EventQueue.java:81)
	at java.awt.EventQueue$2.run(EventQueue.java:633)
	at java.awt.EventQueue$2.run(EventQueue.java:631)
	at java.security.AccessController.doPrivileged(Native Method)
	doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$3.run(EventQueue.java:647)
	at java.awt.EventQueue$3.run(EventQueue.java:645)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


長い間この問題を調べていましたが、やっと見つけました。以下は、この例外を発生させるコードです。

if (e.getSource() == login) {
	String userIdStr = inputUserId.getText().trim();
	// Here if you are using the JPasswordField control to enter the password should not be written like this because then the password will be a very strange string, which is the ultimate cause of the null pointer exception
	String passwdStr = inputPassword.getPassword().toString().trim();
			
	if(0 == userIdStr.length() || 0 == passwdStr.length()){
		JOptionPane.showMessageDialog(this, "User ID or password is empty!!!! ");
	}else{
		org.yfl.domain.Login login = new org.yfl.domain.Login();
		login.setLogin_id(userIdStr);
		login.setPasswd(passwdStr);
		new LoginService().loginValidate(login);
		System.out.println("**********"+login.getGrade());
		}
			
	} else if (e.getSource() == cancel) {
		System.exit(0);
	}

<スパン 以下は、修正したコードです(OK問題解決)。

if (e.getSource() == login) {
	String userIdStr = inputUserId.getText().trim();
	// Here if you are using the JPasswordField control to enter the password, you should write it like this and there will be no problem
	String passwdStr = new String(inputPassword.getPassword()).trim();
			
	if(0 == userIdStr.length() || 0 == passwdStr.length()){
		JOptionPane.showMessageDialog(this, "User ID or password is empty!!!! ");
	}else{
		org.yfl.domain.Login login = new org.yfl.domain.Login();
		login.setLogin_id(userIdStr);
		login.setPasswd(passwdStr);
		new LoginService().loginValidate(login);
		System.out.println("**********"+login.getGrade());
		}
			
	} else if (e.getSource() == cancel) {
		System.exit(0);
	}






もうひとつ、データベースへの問い合わせは、結果がなくても空のResultSetを返さないので、resultSet.getMetaData().getColumnCount()を使うと、このテーブルの列数も返ってくるので、戻り値としてオブジェクトの配列を使う場合は注意しましょう、、、、、。

resultSet = statement.executeQuery();
int columns = resultSet.getMetaData().getColumnCount();
// the columns here must be greater than 0
if(columns > 0){
	objects = new Object[columns];
}
while (resultSet.next()) {
	
	for (int i = 0; i < objects.length; i++) {
		objects[i] = resultSet.getObject(i + 1);
	}
}