将数据库中的数据存储到 java 类并使用 JSP 进行访问



我在尝试使用模型java类访问存储在数据库中的数据时遇到问题,我尝试使用该类在jsp页面上检索数据,但编译器给出错误说:java.lang.NullPointerException

我想做什么 使用 Java 类、数据库控制器和 JSP 页面显示 JSP 页面上的所有视频。

错误行:

exception

org.apache.jasper.JasperException:处理 JSP 页面/doctorallvideo.jsp 第 159 行时发生异常

</tr>
</thead>
<tbody>
error line--->    <% for(int i=0; i<vidlist.size(); i++){
video vid = vidlist.get(i);
%>
<tr>

堆栈跟踪:

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:579)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根源:

java.lang.NullPointerException
org.apache.jsp.doctorallvideo_jsp._jspService(doctorallvideo_jsp.java:271)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的模型类:

public class video {
private int videoId;
private String videoName;
private String description;
private String category;
private String fileName;
private String filepath;
public video(int videoId, String videoName, String description,String category, String fileName, String filepath) {
super();
this.videoId = videoId;
this.videoName = videoName;
this.description = description;
this.category = category;
this.fileName = fileName;
this.filepath = filepath;
}

public video(String videoName, String description,String category, String fileName, String filepath) {
this.videoName = videoName;
this.description = description;
this.category = category;
this.fileName = fileName;
this.filepath = filepath;   
}

public video() {
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getFilepath() {
return filepath;
}

public void setFilepath(String filepath) {
this.filepath = filepath;
}

public int getVideoId() {
return videoId;
}

public void setVideoId(int videoId) {
this.videoId = videoId;
}

public String getVideoName() {
return videoName;
}

public void setVideoName(String videoName) {
this.videoName = videoName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}

这是数据库控制器:

public ArrayList<video> getVideos() {
try {
// get person from database
PreparedStatement ps = connection.prepareStatement("select * from video");
ResultSet rs = ps.executeQuery();
ArrayList<video> vidlist = new ArrayList<>();
while (rs.next()) {
// get doctor from database
video vid = new video(rs.getInt("videoId"), rs.getString("videoName"), rs.getString("description"),rs.getString("category"), rs.getString("fileName")
,rs.getString("filepath"));
vidlist.add(vid);
}
return vidlist;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

servlet 文件:

public doctorallvideo() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());

try {
int personId = (int) session.getAttribute("UserID");
// get doctor details
dbcontroller dbcon = new dbcontroller();
ArrayList<video> vids = dbcon.getVideos();
// add user id in the future 
if (vids == null) {
}
RequestDispatcher rs = request.getRequestDispatcher("doctorallvideo.jsp");
request.setAttribute("video", vids);
rs.forward(request, response);
return;
} catch (Exception e) {
// redirect to login
redirectToVideoUpload(request, response);
}
}

最后,我在JSP页面上添加了这个:

<%@ page
import="multimedia.video, java.util.ArrayList"%>   
<%  
ArrayList<video> vidlist = (ArrayList<video>)request.getAttribute("video");
%>
<div class="col-sm-9 col-sm-offset-3 main">enter code here
<h3 class="page-header">All Videos</h3>

这在 JSP 上:

<div class="col-sm-9 col-sm-offset-3 main" id="patients" style="display: none;">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Id</th>
<th>username</th>
<th>Blood Group</th>
</tr>
</thead>
<tbody>
<% for(int i=0; i<vidlist.size(); i++){
video vid = vidlist.get(i);
%>
<tr>
<td><%= vid.getVideoId() %></td>
<td><%= vid.getVideoName() %></td>
</tr>
<%} %>
</tbody>
</table>
</div>

我看到错误存在,因为您正在尝试访问代码片段中未指定的vidlist,我的意思是在您的代码片段的这一部分内<% 代码片段 %>

为什么不在代码中尝试一下并检查。

<tbody>
<% 
ArrayList<video> vidlist = (ArrayList<video>)request.getAttribute("video");
for(int i=0; i<vidlist.size(); i++){
video vid = vidlist.get(i);
%>
<tr>
<td><%= vid.getVideoId() %></td>
<td><%= vid.getVideoName() %></td>
</tr>
<%} %>
</tbody>

最新更新