Q:

java.lang.NullPointerException in jsp

Hi all I am a fresher in java and jsp and honestly I dont have knowledge of json. but I have some requirement that I have to pull the data from database and display on jsp page. but the criteria is that I have to use  Bootstrap Data Table why I am using this because it provide lot of flexibility like Pagination Filtering Sorting Multi-column sorting Individual column filter.
I am getting a java.lang.NullPointerException in my jsp
can any body tell me why am I getting this error.
This is my jsp page
dataTable.jsp

<%@page import="org.codehaus.jettison.json.JSONObject"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
   <%@ page import ="java.util.*" %>
  <%--  <%@ page import="com.varun.DataBase"%> --%>
   <%@ page import="java.sql.*" %>
<%--    <%@ page import="org.json.*"%> --%>
   <%@ page import ="net.sf.json.JSONArray" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DataTable</title>
</head>
<body>
<h1>Lets display the data from database using dataTabel</h1>

<%

String[] cols = { "id","engine", "browser", "platform", "version", "grade" };
String table = "ajax";

JSONObject result = new JSONObject();
JSONArray arr = new JSONArray();

int amount = 10;
int start = 0;
int echo = 0;
int col = 0;

int  id=0;
String engine = "";
String browser = "";
String platform = "";
String version = "";
String grade = "";

String dir = "asc";
String sStart = request.getParameter("iDisplayStart");
String sAmount = request.getParameter("iDisplayLength");
String sEcho = request.getParameter("sEcho");
String sCol = request.getParameter("iSortCol_0");
String sdir = request.getParameter("sSortDir_0");

engine = request.getParameter("sSearch_0");
browser = request.getParameter("sSearch_1");
platform = request.getParameter("sSearch_2");
version = request.getParameter("sSearch_3");
grade = request.getParameter("sSearch_4");

List<String> sArray = new ArrayList<String>();
if (!engine.equals("")) {
    String sEngine = " engine like '%" + engine + "%'";
    sArray.add(sEngine);
    //or combine the above two steps as:
    //sArray.add(" engine like '%" + engine + "%'");
    //the same as followings
}

if (!browser.equals("")) {
    String sBrowser = " browser like '%" + browser + "%'";
    sArray.add(sBrowser);
}
if (!platform.equals("")) {
    String sPlatform = " platform like '%" + platform + "%'";
    sArray.add(sPlatform);
}
if (!version.equals("")) {
    String sVersion = " version like '%" + version + "%'";
    sArray.add(sVersion);
}
if (!grade.equals("")) {
    String sGrade = " grade like '%" + grade + "%'";
    sArray.add(sGrade);
}

String individualSearch = "";
if(sArray.size()==1){
    individualSearch = sArray.get(0);
}else if(sArray.size()>1){
    for(int i=0;i<sArray.size()-1;i++){
        individualSearch += sArray.get(i)+ " and ";
    }
    individualSearch += sArray.get(sArray.size()-1);
}

if (sStart != null) {
    start = Integer.parseInt(sStart);
    if (start < 0)
        start = 0;
}
if (sAmount != null) {
    amount = Integer.parseInt(sAmount);
    if (amount < 10 || amount > 100)
        amount = 10;
}
if (sEcho != null) {
    echo = Integer.parseInt(sEcho);
}
if (sCol != null) {
    col = Integer.parseInt(sCol);
    if (col < 0 || col > 5)
        col = 0;
}
if (sdir != null) {
    if (!sdir.equals("asc"))
        dir = "desc";
}

String colName = cols[col];
int total = 0;
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","admin");
try {
    String sql = "SELECT count(*) FROM "+table;
    PreparedStatement ps = conn.prepareStatement(sql);
    ResultSet rs = ps.executeQuery();
    if(rs.next()){
        total = rs.getInt("count(*)");
    }
}catch(Exception e){

}
int totalAfterFilter = total;
//result.put("sEcho",echo);

try {
    String searchSQL = "";
    String sql = "SELECT * FROM "+table;
    String searchTerm = request.getParameter("sSearch");
    String globeSearch =  " where (engine like '%"+searchTerm+"%'"
                            + " or browser like '%"+searchTerm+"%'"
                            + " or platform like '%"+searchTerm+"%'"
                            + " or version like '%"+searchTerm+"%'"
                            + " or grade like '%"+searchTerm+"%')";
    if(searchTerm!=""&&individualSearch!=""){
        searchSQL = globeSearch + " and " + individualSearch;
    }
    else if(individualSearch!=""){
        searchSQL = " where " + individualSearch;
    }else if(searchTerm!=""){
        searchSQL=globeSearch;
    }
    sql += searchSQL;
    sql += " order by " + colName + " " + dir;
    sql += " limit " + start + ", " + amount;

    PreparedStatement ps = conn.prepareStatement(sql);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
       // JSONArray ja = new JSONArray();
        net.sf.json.JSONArray ja = new net.sf.json.JSONArray(); 

        ja.add(rs.getInt("id"));
        ja.add(rs.getString("engine"));
        ja.add(rs.getString("browser"));
        ja.add(rs.getString("platform"));
        ja.add(rs.getString("version"));
        ja.add(rs.getString("grade"));
        arr.add(ja); 
    } 
    String sql2 = "SELECT count(*) FROM "+table;
    if (searchTerm != "") {
        sql2 += searchSQL;
        PreparedStatement ps2 = conn.prepareStatement(sql2);
        ResultSet rs2 = ps2.executeQuery();
        if (rs2.next()) {
            totalAfterFilter = rs2.getInt("count(*)");
        }
    }
    result.put("iTotalRecords", total);
    result.put("iTotalDisplayRecords", totalAfterFilter);
    result.put("aaData", arr);
    response.setContentType("application/json");
    response.setHeader("Cache-Control", "no-store");
    out.print(result);
    conn.close();
} catch (Exception e) {

}

%>

</body>
</html>

Stack Trace
11:26:50,224 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.dataTable_jsp._jspService(dataTable_jsp.java:114)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)

A:

If the answer of Ajil Mohan prevent the error to appear, it means that you have no value in your ParametersMap. 
engine = request.getParameter("sSearch_0");
browser = request.getParameter("sSearch_1");
platform = request.getParameter("sSearch_2");
version = request.getParameter("sSearch_3");
grade = request.getParameter("sSearch_4");

These lines just get the data in the Parameter map of your request. If the given is not in the map it returns null (https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29). 
This map is created when you send the POST request on the previous page with the data contained in your form. Your input field has to be set with the name you give to the getParameter method.
If you don't have previous page with a form then you may want to send data from server to JSP. If you use Servlet then you should use the request AttributeMap. 
public class BasicTotoServlet extends HttpServlet {
 /**
  * Manage GET HTTP Request
  */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        String data = getData();
        request.setAttibute("sSearch_0", data)
    }
}

Then in your JSP : 
engine = request.getAttribute("sSearch_0");

I hope I was clear enough. 
Ask for precision if needed
EDIT : 
Here is what I understand. 
You arrive on your JSP and you get several Parameter from the previous page's form. This form may not give to you all the information, that is why you get a NPE on this
engine = request.getParameter("sSearch_0");
engine.equals("")

To solve this as told you Ajil Mohan, just turn the equals the other way round, so that if engine is NULL you have no problem
"".equals(engine);

As "" is never null you'll never get NPE.
Now you don't have any data in you table. Why? If I'm correct you create a SQL Query by concatenating several data. searchSQL = globeSearch + " and " + individualSearch; 
individualSearch is the result of concatenating your Parameters but what if they're null?
if (!"".equals(browser)) {
    String sBrowser = " browser like '%" + browser + "%'";
    sArray.add(sBrowser);
}

In this code, if browser is null (as we saw earlier it can be), you will add the String " browser like '%null%'" to you query with the and key word. I'm not sure there will be any browser with "null" in its name.
You better put this test :
if (!"".equals(browser) && browser != null) {
    String sBrowser = " browser like '%" + browser + "%'";
    sArray.add(sBrowser);
}

