title: Not able to fetch values from servlet in jsp
body: <p>I try to fetch values from servlet into my JSP, but it throws a <code>NullPointerException</code> or some other error.</p>
  
  <p>This is the servlet which gets values from JSP:</p>
  
  <p><code>buildingprofilerequest.java</code></p>
  
  <pre><code>package asset.management.arms.buildingprofilemodule;
  
  import java.io.IOException;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import com.sun.xml.internal.ws.client.SenderException;
  /**
   * Servlet implementation class buildingprofilerequest
   */
  public class buildingprofilerequest extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          try{
  
              buildingservice building = new buildingservice();
              building.setBuilding_name(request.getParameter("combobox"));
  
              building = BuildingDAO.build(building);
  
              request.setAttribute("abcd", building);      
  
  
                   RequestDispatcher dispatcher =                  
                   getServletContext().getRequestDispatcher("/building_profile_details.jsp");
  
              dispatcher.include(request, response);
  
          }
      catch (Throwable theException)      
          {
               System.out.println(theException); 
          }
  
      }
  </code></pre>
  
  <p>My bean looks in brief like this:</p>
  
  <p><code>buildingservice.java</code></p>
  
  <pre><code>package asset.management.arms.buildingprofilemodule;
  
  public class buildingservice {
  
          private String building_name;
  
                  public String getBuilding_name() {
              return building_name;
          }
  
          public void setBuilding_name(String newbuilding_name) {
              this.building_name = newbuilding_name;
          }
  
                //and has many more parameters and there getters and setters
          }
  </code></pre>
  
  <p>My other class is <code>BuildingDAO.java</code>, here all the calculations are done:</p>
  
  <pre><code>package asset.management.arms.buildingprofilemodule;
  
  import java.sql.*;
  import asset.management.arms.loginmodule.ConnectionManager;
  
  public class BuildingDAO {
      static Connection currentCon = null;
      static ResultSet rs = null;  
  
      public static buildingservice build(buildingservice bean) {
  
             String building_name = bean.getBuilding_name(); 
  
             String searchQuery = "select * from buildings";
           try{
              //connect to DB 
              currentCon = ConnectionManager.getConnection();
               stmt=currentCon.createStatement();
               rs = stmt.executeQuery(searchQuery);           
  
               while(rs.next()){
  
              //retreiving building parameters from database   
               String buildingname = rs.getString("building_name");
               String buildingnumber = rs.getString("building_number");
               int buildarea = rs.getInt("build_area");
  
                    //setting building parameters
                   bean.setBuilding_name(buildingname);
                   bean.setBuilding_number(buildingnumber);
                   bean.setBuild_area(buildarea);
                   bean.setBuilt_year(builtyear);
              catch (Exception ex) 
            {
               System.out.println(" " + ex);
            } 
  
              finally 
            {
               if (rs != null)    {
                  try {
                     rs.close();
                  } catch (Exception e) {}
                     rs = null;
                  }
  
               if (stmt != null) {
                  try {
                     stmt.close();
                  } catch (Exception e) {}
                     stmt = null;
                  }
  
               if (currentCon != null) {
                  try {
                     currentCon.close();
                  } catch (Exception e) {
                  }
  
                  currentCon = null;
               }
            }
  
          return bean;
      }
  
  }
  </code></pre>
  
  <p>My JSP is this:</p>
  
  <p><code>building_profile_details.jsp</code></p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=iso-8859-1"
      pageEncoding="ISO-8859-1" import="asset.management.arms.buildingprofilemodule.buildingservice"%&gt;
  
  &lt;%  buildingservice hello = (buildingservice) request.getAttribute("abcd"); %&gt;
  
  &lt;table width="1150" height="176" border="1" align="center" bordercolor="lightslategray"&gt;
        &lt;tr&gt;
          &lt;td width="107"&gt;&lt;div align="center"&gt;&lt;b&gt;Building Number&lt;/b&gt; &lt;/div&gt;&lt;/td&gt;
          &lt;td width="325"&gt;&lt;div align="center"&gt;&lt;b&gt;Building Name &lt;/b&gt;&lt;/div&gt;&lt;/td&gt;
          &lt;td width="70"&gt;&lt;div align="center"&gt;&lt;b&gt;area&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;
          &lt;td width="146"&gt;&lt;div align="center"&gt;&lt;b&gt;built year&lt;/b&gt;&lt;/div&gt;&lt;/td&gt; 
            &lt;/tr&gt;
  
  &lt;tr&gt;
      &lt;td&gt;&lt;div align="center"&gt;&lt;%=hello.getBuilding_number()%&gt;&lt;/div&gt;&lt;/td&gt;
      &lt;td&gt;&lt;div align="center"&gt;&lt;%= hello.getBuilding_name()%&gt;&lt;/div&gt;&lt;/td&gt;
  &lt;td&gt;&lt;div align="center"&gt;&lt;%=hello.getBuild_area()%&gt;&lt;/div&gt;&lt;/td&gt;
   &lt;td&gt;&lt;div align="center"&gt;&lt;%= hello.getBuilt_year()%&gt;&lt;/div&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>In the JSP I even tried other ways like:</p>
  
  <pre><code>&lt;jsp:useBean id="hello" class="asset.management.arms.buildingprofilemodule.buildingservice"&gt;
  </code></pre>
  
  <p>and then in individual blocks of table:</p>
  
  <pre><code>&lt;jsp:getProperty name="hello" name="building_name"&gt;
  </code></pre>
  
  <p>But nothing works, it throws error which says </p>
  
  <pre><code>org.apache.jasper.JasperException: Exception in JSP: /building_profile_details.jsp:82
  
  82:     &lt;td&gt;&lt;div align="center"&gt;&lt;%=hello.getBuilding_number()%&gt;&lt;/div&gt;&lt;/td&gt;
  </code></pre>
  
  <p>and similarly for other lines.</p>
  
  <p>How is this caused and how can I solve this?</p>