0

I am having a problem trying to access data stored in the database using a model java class, i tried retrieving data on a jsp page using the class but the compiler gives an error saying : java.lang.NullPointerException

what i want to do this display all videos on the jsp page using a the java class, db controller and the jsp page.

Error line:

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /doctorallvideo.jsp at line 159

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

Stacktrace:

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)

root cause:

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)

this is my model class:

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;
}

}

and this is the db controller:

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;

}

the servlet file:

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);
    }

}

and finally this i added on the jsp page:

<%@ 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>

and this on the 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>
  • How nice to not have error stack – Worthless Aug 11 '18 at 19:46
  • You need to provide error message and stack trace, as well as on which line exactly it throws exception, you can't just throw code and expect someone to find cause of NPE. Also classes in Java should start with capital letter by convention. – FilipRistic Aug 11 '18 at 21:06
  • sorry im new to this, i updated with the full error i got. –  Aug 11 '18 at 22:44
  • How exactly are you invoking the servlet do you think? Servlets are really not "magically" invoked, they can only be explicitly invoked. This can easily be enforced by preventing the user from being able to directly access solely the JSP file by a web browser. This has already been chewed out countless times, among others in the abovelinked duplicate. – BalusC Aug 12 '18 at 13:40

1 Answers1

0

I see that the error exists because you are trying to access the vidlist which is not specified inside the code fragment i mean inside this part of your code <% code fragment %>

Why not try this in your code and check.

            <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>
anand s
  • 9
  • 3