java.lang.NullPointerException 和 ResultSet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15794369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NullPointerException and ResultSet
提问by JudgementFlame
This is my first post so please be patient. I am working on a java project and that connects to a sql database. What I am currently trying to do is when I click a button, will display the name of the item and the price of the item with the specified ID in the text area from my database. However, when I click the button an error such as this appears:
这是我的第一篇文章,所以请耐心等待。我正在开发一个连接到 sql 数据库的 java 项目。我目前正在尝试做的是,当我单击一个按钮时,将在我的数据库的文本区域中显示具有指定 ID 的商品名称和商品价格。但是,当我单击按钮时,会出现如下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at OrderFrame.waterButtonActionPerformed(OrderFrame.java:494)
at OrderFrame.accessprivate void waterButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
String query;
query = "SELECT itemName, itemPrice FROM item WHERE itemID = '11111'";
String itemName = " ",itemPrice =" ";
***ResultSet rs = st.executeQuery(query);***
if(rs != null){
while(rs.next())
{
itemName = rs.getString(1);
itemPrice = rs.getString(2);
}
orderTextArea.setText(itemName);
orderTextArea.setText(itemPrice);
}
} catch (SQLException ex) {}
}
0(OrderFrame.java:11)
Here is the code for my waterButtonActionPerformed:
这是我的 waterButtonActionPerformed 的代码:
waterButton.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Statement st = connectionObject.createStatement();
// might want to use placeholders here as well
String query= "SELECT itemName, itemPrice FROM item WHERE itemID = '11111'";
String itemName = " ",itemPrice =" ";
ResultSet rs = st.executeQuery(query);
if(rs != null){
do {
itemName = rs.getString(1);
itemPrice = rs.getString(2);
} while (rs.next());
orderTextArea.setText(itemName);
orderTextArea.setText(itemPrice);
}
} catch (SQLException ex) {
System.err.println(new java.util.Date()+" : "+ex.getMessage();
}
});
The line 494 is the one with the declaration of the ResultSet. I am am hoping someone can help me fix this issue. Thank you in advance.
第 494 行是带有 ResultSet 声明的那一行。我希望有人能帮我解决这个问题。先感谢您。
采纳答案by hd1
Rewrite the handler, like this:
重写处理程序,如下所示:
con.prepareStatement(query);
回答by durron597
st
is null
. You need something like
st
是null
。你需要类似的东西
conn.prepareStatement(query)
Where con
is your connection to the SQL database. You don't show how you connect to the database here. Here's a good tutorial on how to do that.
con
您与 SQL 数据库的连接在哪里。您没有在此处显示如何连接到数据库。这是一个关于如何做到这一点的好教程。
回答by Biswajit
It look like that st is null .please check your st otherwise write the code
看起来 st 为空。请检查您的 st 否则编写代码
##代码##回答by Alex Blakemore
It looks like st is null. Your example doesn't show where st is declared, much less initialized.
看起来 st 为空。您的示例没有显示 st 的声明位置,更不用说初始化了。
You probably need to create a Statement from your query (which is just a string)
您可能需要从您的查询中创建一个 Statement (这只是一个字符串)