如何解决此错误....线程中的异常“AWT-eventqueue-0”显示java.lang.NullPointerException

如何解决此错误....线程中的异常“AWT-eventqueue-0”显示java.lang.NullPointerException

问题描述:

这是我的代码......



包MyProject;



导入java。 awt.HeadlessException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

import java.lang。*;



/ ** >
*

* @author hp

* /

公共类User1扩展javax.swing.JFrame {



连接con = null;

PreparedStatement ps;

ResultSet rs;

/ **

*创建新表单User1

* /

public User1(){

initComponents();

}



/ **

*从构造函数中调用此方法来初始化表单。

*警告:不要修改此代码。表单编辑器重新生成此方法的内容

*

* /

@SuppressWarnings(unchecked)

//< editor-fold defaultstate =collapseddesc =Generated Code>

private void initComponents(){



label1 = new javax.swing.JLabel();

Back1 = new javax.swing.JButton();

label2 = new javax.swing.JLabel();

username = new javax.swing.JTextField();

password = new javax.swing.JTextField();

Submit1 = new javax.swing.JButton();

label3 = new javax.swing .JLabel();



setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);



label1。 setText(Username:);



Back1.setText(Back);

Back1.addActionListener(new java.awt。 event.ActionListener(){

public void actionPerformed(java.awt.event.ActionEvent evt){

Back1ActionPerformed(evt);

}

});



label2.setText(密码:);



username.addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(java.awt.event.ActionEvent evt){

usernameActionPerformed(evt);

}

});



密码。 addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(java.awt.event.ActionEvent evt){

passwordActionPerformed(evt);

}

});



Submit1.setText(提交);

Submit1.addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(java.awt.event.ActionEvent evt){

Submit1ActionPerformed(evt) ;

}

});



javax.swing.GroupLayout layout = new javax.swing.GroupLayout (getContentPa ne());

getContentPane()。setLayout(layout);

layout.setHorizo​​ntalGroup(

layout.createParallelGroup(javax.swing。 GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(15,15,15)

.addComponent(Back1))

.addGroup(layout.createSequentialGroup()

.addGap(51,51,51)

.addComponent(label1)

.addGap (103,103,103)

.addComponent(username,javax.swing.GroupLayout.PREFERRED_SIZE,121,javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup( layout.createSeq uentialGroup()

.addGap(51,51,51)

.addComponent(label2)

.addGap(107,107,107)

.addComponent(密码,javax.swing.GroupLayout.PREFERRED_SIZE,121,javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(145,145,145)

.addComponent(Submit1))

.addGroup(layout.createSequentialGroup()

.addGap(121,121,121)

.addComponent(label3,javax.swing.GroupLayout.PREFERRED_SIZE,146,javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(161,Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing) .GroupLayout。 Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(16,16,16)

.addComponent(Back1)

.addGap(34,34,34)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(3,3,3)

.addComponent(label1))

.addComponent( username,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(47,47,47)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(3,3, 3)

.addComponent(label2))

.addComponent(密码,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(31,31,31)

.addComponent(Submit1)

.addGap(63,63,63)

。 addComponent(label3,javax.swing.GroupLayout.PREFERRED_SIZE,90,javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(184,Short.MAX_VALUE))

);



pack();

} //



private void Back1ActionPerformed (java.awt.event.ActionEvent evt){

this.dispose();

}



private void usernameActionPerformed(java.awt.event.ActionEvent evt){

// TODO在这里添加你的处理代码:

}



private void passwordActionPerformed(java.awt。 event.ActionEvent evt){

// TODO在这里添加你的处理代码:

}



private void Submit1ActionPerformed(java.awt.event.ActionEvent evt){









con = MysqlConnection.ConnectionDB();



String Query =select * from users where Username =?和密码=?;



尝试{

ps = con.prepareStatement(查询);



ps.setString(1,username.getText());

ps.setString(2,password.getText());



rs = ps.executeQuery(查询);

if(rs.next())

{



JOptionPane.showMessageDialog(null,invalid,invalid,JOptionPane.ERROR_MESSAGE);



}









else

{





this.dispose();

}



}






catch(SQLException | H eadlessException ex){

}















}



/ ** >
* @param args命令行参数

* /

public static void main(String args []){

/ *设置Nimbus外观* /

//< editor-fold defaultstate =collapseddesc =外观设置代码(可选)>

/ *如果Nimbus(在Java SE 6中引入)不可用,请使用默认的外观。

*有关详细信息,请参阅http://download.oracle.com/javase/tutorial /uiswing/lookandfeel/plaf.html

* /

尝试{

for(javax.swing.UIManager.LookAndFeelInfo info:javax.swing .UIManager.getInstalledLookAndFeels()){

if(Nimbus.equals(info.getName())){

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;
}

}

} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex){

java.util.logging.Logger.getLogger(User1.class.getName())。log(java.util.logging.Level.SEVERE,null,ex );

}

//



//



/ *创建并显示表格* /

java.awt.EventQueue.invokeLater(() - > {

new User1()。setVisible (true);

});

}



//变量声明 - 不要修改

private javax.swing.JButton Back1;

private javax.swing.JButton Submit1;

private javax.swing.JLabel label1;

private javax.swing.JLabel label2;

private javax.swing.JLabel label3;

private javax.swing.JTextField password;

private javax.swing.JTextField username;

//变量结束声明

}



我的尝试:



here'a MysqlConnection文件



/ *

*要更改此许可证标题,请在项目属性中选择许可证标题。 />
*要更改此模板文件,请选择工具|模板

*并在编辑器中打开模板。

* /

包MyProject;



import java.awt.HeadlessException;



import java.sql。*;

import javax.swing。*;





/ **

*

* @author hp

* /

公共类MysqlConnection {

连接con = null;

公共静态连接ConnectionDB(){

尝试{

System.out.println(1);

Class.forName(com.jdbc.mysql.Driver);

System.out.println(1);

连接con = DriverManager.getConnection(jdbc:mysql:// localhost:3306 / airlines?autoReconnect = true& amp; ; useSSL = false,root,riyard);

JOptionPane.showMessageDialog(null,连接到数据库);

return con;



} $ / $
catch(ClassNotFoundException | SQLException | HeadlessException e){

JOptionPane.showMessageDialog(null,e);

返回null;



}




}







}

donno如何解决这个问题..请帮帮我...

Here's my code......

package MyProject;

import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.lang.*;

/**
*
* @author hp
*/
public class User1 extends javax.swing.JFrame {

Connection con = null;
PreparedStatement ps;
ResultSet rs;
/**
* Creates new form User1
*/
public User1() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

label1 = new javax.swing.JLabel();
Back1 = new javax.swing.JButton();
label2 = new javax.swing.JLabel();
username = new javax.swing.JTextField();
password = new javax.swing.JTextField();
Submit1 = new javax.swing.JButton();
label3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

label1.setText("Username:");

Back1.setText("Back");
Back1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Back1ActionPerformed(evt);
}
});

label2.setText("Password:");

username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
usernameActionPerformed(evt);
}
});

password.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passwordActionPerformed(evt);
}
});

Submit1.setText("Submit");
Submit1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Submit1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(Back1))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(label1)
.addGap(103, 103, 103)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(label2)
.addGap(107, 107, 107)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(145, 145, 145)
.addComponent(Submit1))
.addGroup(layout.createSequentialGroup()
.addGap(121, 121, 121)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(161, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(Back1)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(label1))
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(label2))
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addComponent(Submit1)
.addGap(63, 63, 63)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(184, Short.MAX_VALUE))
);

pack();
}//

private void Back1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}

private void usernameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void passwordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void Submit1ActionPerformed(java.awt.event.ActionEvent evt) {




con=MysqlConnection.ConnectionDB();

String Query="select * from users where Username=? AND Password=?";

try{
ps = con.prepareStatement(Query);

ps.setString(1,username.getText());
ps.setString(2,password.getText());

rs = ps.executeQuery(Query);
if(rs.next())
{

JOptionPane.showMessageDialog(null,"invalid","invalid",JOptionPane.ERROR_MESSAGE);

}




else
{


this.dispose();
}

}



catch (SQLException | HeadlessException ex) {
}







}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(User1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

//

/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new User1().setVisible(true);
});
}

// Variables declaration - do not modify
private javax.swing.JButton Back1;
private javax.swing.JButton Submit1;
private javax.swing.JLabel label1;
private javax.swing.JLabel label2;
private javax.swing.JLabel label3;
private javax.swing.JTextField password;
private javax.swing.JTextField username;
// End of variables declaration
}

What I have tried:

here'a MysqlConnection file

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyProject;

import java.awt.HeadlessException;

import java.sql.*;
import javax.swing.*;


/**
*
* @author hp
*/
public class MysqlConnection {
Connection con=null;
public static Connection ConnectionDB(){
try{
System.out.println("1");
Class.forName("com.jdbc.mysql.Driver");
System.out.println("1");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/airlines?autoReconnect=true&useSSL=false", "root","riyard");
JOptionPane.showMessageDialog(null,"Connected to Database");
return con;

}
catch(ClassNotFoundException | SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null,e);
return null;

}


}



}
donno how to fix this.. Please help me...

这是其中之一我们得到最常见的问题,也是我们最不能回答的问题,但你最有能力回答自己。



我来解释一下错误意味着:您已尝试使用变量,属性或方法返回值,但它包含null - 这意味着变量中没有类的实例。

它有点像口袋:你的衬衫里有一个口袋,用来握笔。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了!



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行您的程序,当它失败时,VS将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,VS会在错误发生前停止,让你通过查看代码来查看你的价值来检查发生了什么。



但我们做不到那 - 我们没有您的代码,如果我们拥有它,我们不知道如何使用它,我们没有您的数据。所以试试吧 - 看看你能找到多少信息!
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!