这是我提交表格时收到的错误。 HTTP状态500错误。
public class SupplierController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String ADD = "/supplier_add.jsp";
private static String VIEW = "/supplier_view.jsp";
private static String EDIT = "/supplier_edit.jsp";
private static String LIST = "/supplier.jsp";
String forward="";
private SupplierDAO dao;
/**
* @see HttpServlet#HttpServlet()
*/
public SupplierController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equalsIgnoreCase("SupplierList")){
forward = LIST;
try {
request.setAttribute("suppliers", dao.getAllSupplier());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (action.equalsIgnoreCase("SupplierView")){
forward = VIEW;
int supplierid = Integer.parseInt(request.getParameter("supplierid"));
SupplierBean supplier = dao.getSupplierById(supplierid);
request.setAttribute("supplier", supplier);
}
else if (action.equalsIgnoreCase("SupplierDelete")){
int supplierid = Integer.parseInt(request.getParameter("supplierid"));
dao.delete(supplierid);
request.setAttribute("suppliers", dao.getAllSupplier());
}
else if (action.equalsIgnoreCase("SupplierEdit")){
forward = EDIT;
int supplierid = Integer.parseInt(request.getParameter("supplierid"));
SupplierBean supplier = dao.getSupplierById(supplierid);
request.setAttribute("supplier", supplier);
}
else {
forward = ADD;
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
SupplierBean supplier = new SupplierBean();
supplier.setSupplier_name(request.getParameter("supplier_name"));
supplier.setSupplier_address(request.getParameter("supplier_description"));
supplier.setSupplier_contact_num(request.getParameter("supplier_contact_num"));
supplier.setSupplier_description(request.getParameter("supplier_description"));
String supplierid = request.getParameter("supplier_id");
if(supplierid == null || supplierid.isEmpty())
{
try {
dao.add(supplier);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
supplier.setSupplier_id(Integer.parseInt(supplierid));
try {
dao.edit(supplier);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
forward = LIST;
RequestDispatcher view = request.getRequestDispatcher(forward);
try {
request.setAttribute("supplier", dao.getAllSupplier());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
view.forward(request, response);
}
每当我提交表格时。它说行dao.add(供应商)中的java.lang.NullPointerException错误。我在文本框中检查所有输入,并返回除supplierid之外的值,因为我想提交表单。我该如何解决这个问题?
public class ProductDAO {
static Connection currentCon = null;
static Statement stmt=null;
static ResultSet rs = null;
static PreparedStatement ps=null;
static String product_name, product_description, product_ori_price, product_sell_price, product_profit, product_quantity, product_supplier;
static int product_id;
//add new product (register)
public void add(ProductBean bean) throws NoSuchAlgorithmException{
product_name = bean.getProduct_name();
product_description = bean.getProduct_description();
product_ori_price = bean.getProduct_ori_price();
product_sell_price = bean.getProduct_sell_price();
product_profit = bean.getProduct_profit();
product_quantity = bean.getProduct_quantity();
product_supplier = bean.getProduct_supplier();
try {
currentCon = ConnectionManager.getConnection();
ps=currentCon.prepareStatement("insert into products(product_name, product_description, product_ori_price, product_sell_price, product_profit, product_quantity, product_supplier)values(?,?,?,?,?,?,?)");
ps.setString(1,product_name);
ps.setString(2,product_description);
ps.setString(3,product_ori_price);
ps.setString(4,product_sell_price);
ps.setString(5,product_profit);
ps.setString(6,product_quantity);
ps.setString(7,product_supplier);
ps.execute();
System.out.println("Your product name is " + product_name);
System.out.println("Your product price is " + product_ori_price);
}
catch (Exception ex) {
System.out.println("failed: An Exception has occurred! " + ex);
}
finally {
if (ps != null) {
try {
ps.close();
} catch (Exception e) {
}
ps = null;
}
if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}
currentCon = null;
}
}
}
public void edit(ProductBean bean) throws NoSuchAlgorithmException {
product_name = bean.getProduct_name();
product_description = bean.getProduct_description();
product_ori_price = bean.getProduct_ori_price();
product_sell_price = bean.getProduct_sell_price();
product_profit = bean.getProduct_profit();
product_quantity = bean.getProduct_quantity();
product_supplier = bean.getProduct_supplier();
String searchQuery = "UPDATE products SET product_name='" + product_name + "', product_description='" + product_description + "', product_ori_price='" + product_ori_price + "', product_sell_price= '"+ product_sell_price + "' , product_profit='" +product_profit+ "' , product_quantity='" +product_quantity+ "' , product_supplier= '"+product_supplier+"' WHERE product_id = '" + product_id + "'";
try {
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
stmt.executeUpdate(searchQuery);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int product_id) {
try {
PreparedStatement preparedStatement = currentCon.prepareStatement("delete from products where product_id=?");
// Parameters start with 1
preparedStatement.setInt(1, product_id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
//list order
public List<ProductBean> getAllProduct(){
List<ProductBean> products = new ArrayList<ProductBean>();
try {
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
String sql = "select * from products";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
ProductBean product = new ProductBean();
product.setProduct_id(rs.getInt("product_id"));
product.setProduct_name(rs.getString("product_name"));
product.setProduct_ori_price(rs.getString("product__ori_price"));
product.setProduct_sell_price(rs.getString("product_sell_price"));
product.setProduct_profit(rs.getString("product_profit"));
product.setProduct_quantity(rs.getString("product_quantity"));
products.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
}
return products;
}
//get product by productid
public ProductBean getProductById(int productid) {
ProductBean product = new ProductBean();
try {
currentCon = ConnectionManager.getConnection();
ps=currentCon.prepareStatement("select * from products where product_id=?");
ps.setInt(1, productid);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
product.setProduct_id(rs.getInt("product_id"));
product.setProduct_name(rs.getString("product_name"));
product.setProduct_description(rs.getString("product_description"));
product.setProduct_ori_price(rs.getString("product__ori_price"));
product.setProduct_sell_price(rs.getString("product_sell_price"));
product.setProduct_profit(rs.getString("product_profit"));
product.setProduct_quantity(rs.getString("product_quantity"));
product.setProduct_supplier(rs.getString("product_supplier"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return product;
}
}
}
**修订版
抱歉,这是我的ProductDAO页面。添加有错误的方法。我已将product_id设置在数据库中以进行自动增量。
Moq
答案 0 :(得分:0)
你的问题来自于此:
private SupplierDAO dao;
删除该行并在doGet中实例化它:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SupplierDAO dao = new SupplierDAO();
...
答案 1 :(得分:0)
SupplierDAO
是一个班级?然后使用至少下面的代码。
private SupplierDAO dao= new SupplierDAO();
如果是界面,则在实施类上使用new
。