运行程序时出现java.lang.NullPointerException
错误。似乎错误似乎在try块内:
rs = st.executeQuery(query);
以下Java代码中的方法regcustomers
。请查看我的代码并帮助我了解我做错了什么。
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vastika.serlvet.dao.ConnectionDb;
public class RegistrationServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Read the data
String name = req.getParameter("name");
String email = req.getParameter("email");
String line1 = req.getParameter("addressline1");
String city = req.getParameter("city");
String state = req.getParameter("state");
String country = req.getParameter("country");
String userid = req.getParameter("username");
String password = req.getParameter("password");
insertToDatabase(name, email, line1, city, state, country, userid,
password);
// Forward to Shopping page
req.getRequestDispatcher("shop.html").forward(req, res);
}
private void insertToDatabase(String name, String email, String line1,
String city, String state, String country, String userid,
String password) {
ConnectionDb con = new ConnectionDb();
Statement st = con.makeConnection();
ResultSet rs = null;
String query = "insert into regcustomers (Name, Email, Address, City, State, Country, Username, Password) values("+"'"+name+"'"+","+"'"+email+"'"+","+"'"+line1+"'"+","+"'"+city+"'"+","+"'"+state+"'"+","+"'"+country+"'"+","+"'"+userid+"'"+","+"'"+password+"'"+");";
System.out.println(query);
try {
rs = st.executeQuery(query);
System.out.println(rs.getRow());
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
和
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class ConnectionDb {
public Statement makeConnection() {
Connection conn = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/onlineshopping", "root",
"test");
st = conn.createStatement();
} catch (Exception e) {
e.getStackTrace();
}
return st;
}
}
答案 0 :(得分:3)
发生的事情是您在以下代码中获得异常
public Statement makeConnection() {
Connection conn = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/onlineshopping", "root",
"test");
st = conn.createStatement();
} catch (Exception e) {
e.getStackTrace();
}
return st;
}
然后返回st
null
,因为代码永远不会到conn.createStatement()
。
顺便说一句,你实际上并没有打印Exception
。使用e.printStackTrace()
。
此外,您应该实际处理异常。
答案 1 :(得分:0)
无需输入分号;
String query = "insert into regcustomers (Name, Email, Address, City, State, Country, Username, Password) values("+"'"+name+"'"+","+"'"+email+"'"+","+"'"+line1+"'"+","+"'"+city+"'"+","+"'"+state+"'"+","+"'"+country+"'"+","+"'"+userid+"'"+","+"'"+password+"'"+")";
当您点击ResultSet
查询
SELECT
删除ResultSet
声明..
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASENAME", "root", "");
Statement st = con.createStatement();
使用int count = st.executeUpdate();
答案 2 :(得分:0)
你可以尝试这样做:
private void insertToDatabase(String name, String email, String line1,
String city, String state, String country, String userid,
String password) {
ConnectionDb con = new ConnectionDb();
Statement st = con.makeConnection();
ResultSet rs = null;
String query = "insert into regcustomers (Name, Email, Address, City, State, Country, Username, Password) values(?,?,?,?,?,?,?,?)";
st.setString(1,name);
st.setString(2,email);
st.setString(3,line1);
st.setString(4,street);
st.setString(5,state);
st.setString(6,country);
st.setString(7,userid);
st.setString(8,passwoname);
try {
int flag = st.executeUpdate(query);
}
catch (SQLException e) {
e.printStackTrace();
}
}
我认为你想做的不是查询,而是更新。所以:
rs = st.executeQuery(query);
你可以这样做:
int flag = st.executeUpdate(query);
请参阅this以获取更多信息。
Att:你有一个NullPointer异常,因为(可能)rs = st.executeQuery(query);
没有检索任何内容。
希望它有所帮助^^
答案 3 :(得分:0)
在try catch块中执行SQL查询时触发java.lang.NullPointerException错误有一种奇怪的情况。 查询更新了几个JCombobox中的选定索引。 这些JCobobox最初链接到根据shema手动更新手动索引:
private void PriorityLoad6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
NewSelectedIndex = PriorityLoad6.getSelectedIndex(); SetNewIndexes();
}
在这种情况下,用简单的
执行mysql更新时PriorityLoad6.setSelectedIndex(rs.getInt("PriorityLoad_6"));
触发上述错误。 在这种情况下,一种解决方案是使用在查询期间不活动的if“隔离”内部代码。例如:
private void PriorityLoad6ActionPerformed(java.awt.event.ActionEvent evt) {
if (SaveSettings.hasFocus() == false){NewSelectedIndex = PriorityLoad6.getSelectedIndex(); SetNewIndexes();}}
这一行动当然应该在充分放置之前进行:
SaveSettings.requestFocus();
在调用mysql查询之前。