0

I am trying but I cannot find the errors in my code.

public static List<Gaming> getGamingByName(String TenSanPham) 
    {
        List<Gaming> gaming = new LinkedList<Gaming>();
        Connection connection = JDBC.JDBCConnection.getJDBCConnection();
        
        String sql ="SELECT * FROM GAMING WHERE TENSANPHAM = ? ";
        
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, TenSanPham);
            ResultSet rs = preparedStatement.executeQuery();
            
            while (rs.next())
            {
                Gaming gaming1 = new Gaming();
                
                gaming1.setTENSANPHAM(rs.getString("TENSANPHAM"));
                gaming1.setSOLUONG(rs.getString("SOLUONG"));
                gaming1.setGIATIEN(rs.getString("GIATIEN"));
                gaming1.setNGAYSANXUAT(rs.getString("NGAYSANXUAT"));
                gaming1.setXUATXU(rs.getString("XUATXU"));
                gaming1.setABOUT(rs.getString("ABOUT"));
                
                gaming.add(gaming1);
            }
           
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;

This is structure of Edit Edit Gaming

<%
String TenSanPham = request.getParameter("TenSanPham");

List<Gaming> gamings = new GamingService().getGamingByName(TenSanPham);

for (Gaming gaming : gamings) {
%>
<
<form action="WebJava/EditPost.jsp" method="post">

<p>Ten San Pham: </p>
<input type="text" name="TenSanPham" value="<%=gaming.getTENSANPHAM()%>" readonly="readonly"><br>
<p>So Luong: </p>
<input type="text" name="SoLuong" value="<%=gaming.getSOLUONG()%>"><br>
<p>Gia Tien: </p>
<input type="text" name="GiaTien" value="<%=gaming.getGIATIEN()%>"><br>
<p>Ngay San Xuat: </p>
<input type="text" name="NgaySanXuat" value="<%=gaming.getNGAYSANXUAT()%>"><br>
<p>Xuat Xu: </p>
<input type="text" name="XuatXu" value="<%=gaming.getXUATXU()%>"><br>
<p>About: </p>
<input type="text" name="About" value="<%=gaming.getABOUT()%>"><br>

<input type="submit" value="Edit"><br>
</form>

Then, EditPostName.Jsp

 package QuanLi;
 import java.io.IOException;
 import QuanLi.GameDao;
 import Service.GamingService;
 import jakarta.servlet.RequestDispatcher;
 import jakarta.servlet.ServletException;
 import jakarta.servlet.annotation.WebServlet;
 import jakarta.servlet.http.HttpServlet;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 @WebServlet(urlPatterns = {"/EditPost123"})
 public class EditPostName extends HttpServlet{
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 String TenSanPham = request.getParameter("TenSanPham");
        request.setAttribute("getGamingByName",GameDao.getGamingByName(TenSanPham));

 RequestDispatcher rd = request.getRequestDispatcher("/WebJava/EditGaming.jsp");
        rd.forward(request, response);
        
    }
 @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

} Finally, EditPost.jsp

<%
GamingService gamingService = new GamingService();
Gaming gaming = new Gaming();

gaming.setSOLUONG(request.getParameter("SoLuong"));
gaming.setGIATIEN(request.getParameter("GiaTien"));
gaming.setNGAYSANXUAT(request.getParameter("NgaySanXuat"));
gaming.setXUATXU(request.getParameter("XuatXu"));
gaming.setABOUT(request.getParameter("About"));
gaming.setTENSANPHAM(request.getParameter("TenSanPham"));

/*
String SoLuong = request.getParameter("SoLuong");
String GiaTien = request.getParameter("GiaTien");
String NgaySanXuat = request.getParameter("NgaySanXuat");
String XuatXu = request.getParameter("XuatXu");
String About = request.getParameter("About");
String TenSanPham = request.getParameter("TenSanPham");
*/  
gamingService.updateGaming(gaming);

response.sendRedirect("/ChuyenDeThucTap/WebJava/ListGaming.jsp");

%>

I am a junior university student, I still have many shortcomings, I hope to receive everyone's help. I'm so impressed, I know arrays can't traverse 1 object. Furthermore, when SELECT*FROM GAMING WHERE TENSANPHAM =? will just be an object because, TENSANPHAM is the KEY enter image description here

David
  • 1
  • 1
  • 1
    The Exception's stack trace tells you exactly where to look. – f1sh Sep 02 '21 at 09:32
  • And ... unless you show us the stacktrace, it is very difficult for us to help you. In future always include the complete stacktrace in any question that asks for our debugging assistance. But for now, just read the dup link which explains how to debug NullPointerException errors. – Stephen C Sep 02 '21 at 10:06
  • HI F1sh. Thank You for helping. I received this errors. It's always NULLPOINT and I don't understand this problem I add Link image above – David Sep 03 '21 at 02:50

0 Answers0