#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    34
    Rep Power
    2

    Erro: root cause java.lang.NullPointerException


    i have this error when i run my servlet
    root cause

    java.lang.NullPointerException
    mighty.NewServlet2.doPost(NewServlet2.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)




    here is my servlet
    package mighty;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    *
    * @author nomosa
    */
    @WebServlet(name = "NewServlet2", urlPatterns = {"/NewServlet2"})


    public class NewServlet2 extends HttpServlet {
    //connnects to the first databse (ESR)
    public static Connection getConnection(String db){
    Connection con = null;
    try{
    Class.forName("org.postgresql.Driver").newInstance();
    con = DriverManager.getConnection("jdbcostgresql://216.77.96.20:5432/ESR" + db ,"postgres","postgres");

    }catch (Exception ex){
    ex.printStackTrace();
    }
    return con;
    }
    //connects to the second database(FRNACHISE DB)
    public static Connection getConnection2(String db){
    Connection conn = null;
    try{
    Class.forName("org.postgresql.Driver").newInstance();
    conn = DriverManager.getConnection("jdbcostgresql://216.77.96.20:5432/FranchiseDB" + db ,"","");

    }catch (Exception ex){
    ex.printStackTrace();
    }
    return conn;
    }



    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.print("<html><head>");
    out.print("</head><body>");
    out.print("<form action=\"");
    out.print( request.getRequestURI() );
    out.print("\" method=\"post\">");
    out.print("<input type=\"submit\" ");
    out.print("value=\" \"> ");
    out.print("Display Records</form>");

    out.print("</body></html>");
    out.close();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();


    out.print("<html><head>");
    out.print("</head><body>");
    out.print("<code><pre>");
    out.print("<font color=green>Franchise ID\tFranchise Name\tMonth\tyear\tReported V\tESR Values\tDifference");
    Connection con = getConnection("//216.77.96.20/ESR)");


    Statement st = null;

    ResultSet rs = null;
    try{


    String franno = request.getParameter("franno");
    String month = request.getParameter("month");
    String year = request.getParameter("year");
    String query = "select * from ticket where franno ='"+franno+"' and month='"+month+"' and year='"+year+"'order byfranno";

    rs = st.executeQuery(query);

    System.out.println("Connected to the database");



    while(rs.next()){
    out.print(rs.getObject(1).toString());
    out.print("\t");
    out.print(rs.getObject(2).toString());
    out.print("\t\t");
    out.print(rs.getObject(3).toString());
    out.print("\n");
    } } catch (Exception e) {
    throw new
    ServletException("Servlet Could not display records.", e);
    }
    finally {
    try {
    if(rs != null) {
    rs.close();
    rs = null;
    }
    if(st != null) {
    st.close();
    st = null;
    }
    if(con != null) {
    con.close();
    con = null;
    }
    } catch (Exception e) {}
    }




    out.print("</pre></code>");

    out.print("<p>Search took : ");
    out.print(" ms.</p>");

    out.print("<p\"><a href=\"");
    out.print( request.getRequestURI() );
    out.print("\">Back</a></p>");

    out.print("</body></html>");
    out.close();
    }
    }
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1712
    You really don't want any help do you? What is line 94? What has been assigned to the variables on line 94?

    You've got to try to solve some of this yourself. Throwing your hands up, ignoring the error message, and then saying "help me world" while forcing the people providing you help for free to download your unformatted code and figure out where the hell line 94 is themselves is no way to get help.

    Hint: Google for "basic jdbc example". You're missing the creation of the Statement.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    34
    Rep Power
    2
    Originally Posted by stdunbar
    You really don't want any help do you? What is line 94? What has been assigned to the variables on line 94?

    You've got to try to solve some of this yourself. Throwing your hands up, ignoring the error message, and then saying "help me world" while forcing the people providing you help for free to download your unformatted code and figure out where the hell line 94 is themselves is no way to get help.

    Hint: Google for "basic jdbc example". You're missing the creation of the Statement.
    my goodness stdunbar come down...i just realised that i was missing that..
    i really appreciate you helping me out
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    34
    Rep Power
    2

    HELP help!


    Originally Posted by nicholas.omosa
    my goodness stdunbar come down...i just realised that i was missing that..
    i really appreciate you helping me out
    I am having this kind of error and i know its something to do with class path ..
    i am using tomcat 7 as my web server...


    Differencejava.lang.ClassNotFoundException: org.postgresql.Driver
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    Originally Posted by nicholas.omosa
    I am having this kind of error and i know its something to do with class path ..
    i am using tomcat 7 as my web server...


    Differencejava.lang.ClassNotFoundException: org.postgresql.Driver
    You should place the jar file containing the postgresql driver in the appropriate place. In Tomcat 5 the location was $TOMCAT_HOME/common/lib It may be different in Tomcat 7
  10. #6
  11. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1712
    Originally Posted by bullet
    You should place the jar file containing the postgresql driver in the appropriate place. In Tomcat 5 the location was $TOMCAT_HOME/common/lib It may be different in Tomcat 7
    In Tomcat 7 it is $TOMCAT_HOME/lib. Or you can place the driver jar in your web application directory under WEB-INF/lib if you choose.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    34
    Rep Power
    2

    The module has not been deployed error


    Originally Posted by stdunbar
    In Tomcat 7 it is $TOMCAT_HOME/lib. Or you can place the driver jar in your web application directory under WEB-INF/lib if you choose.
    Hey guys when i try to run my project, am getting this :
    :\Users\nomosa\Documents\NetBeansProjects\mighty\nbproject\build-impl.xml:724: The module has not been deployed


    now earlier on i waas having an error of classExcepiton not found and this was because tomcat was not reading my org.postgresql.Driver. so i went to my web.xml and added this code:
    <database jndi-name="jdbc/ESR"/>
    <driver>
    <type>org.postgresql.Driver</type>
    <url>jdbcostgresql://216.77.96.20:5432/ESR</url>
    <user>postgres</user>
    <password>postgres</password>
    </driver>
    </database>


    help me out please
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    34
    Rep Power
    2

    important!


    Originally Posted by nicholas.omosa
    Hey guys when i try to run my project, am getting this :
    :\Users\nomosa\Documents\NetBeansProjects\mighty\nbproject\build-impl.xml:724: The module has not been deployed


    now earlier on i waas having an error of classExcepiton not found and this was because tomcat was not reading my org.postgresql.Driver. so i went to my web.xml and added this code:
    <database jndi-name="jdbc/ESR"/>
    <driver>
    <type>org.postgresql.Driver</type>
    <url>jdbcostgresql://216.77.96.20:5432/ESR</url>
    <user>postgres</user>
    <password>postgres</password>
    </driver>
    </database>


    help me out please

    thank you all my project is working fine!! though i have another post titled "[Servlets/JSP] How to print report using multiple unique numbers(primary keys)"
    please help me on that one

IMN logo majestic logo threadwatch logo seochat tools logo