CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012


    I would be very gratefull if you could find where is my problem and how can I fix it. (java.lang.NullPointerException)

    From a html page I get a textfield and I upload 2 files. In the servlet I save the uploaded files into a specific folder and after that I am connecting to a database where I want to store the string got from html and the file paths from the uploaded files

    I am sure that the error is somewhere in the code I copy-paste it here.

    String idmarca = request.getParameter("rid");

    String itemName1 = fi.getName();
    String path1 = new String("C:\\UploadedFiles\\" + itemName1.substring(itemName1.lastIndexOf("\\")));

    String itemName2 = fi2.getName();
    String path2 = new String("C:\\UploadedFiles\\" + itemName2.substring(itemName2.lastIndexOf("\\")));
    Connection con = null;
    PreparedStatement ps;

    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String db = "jdbcdbc:upload";
    con = DriverManager.getConnection(db, "", "");
    String sql = "INSERT INTO file1(marca,file1,file2) VALUES(?, ?, ?)";
    ps = con.prepareStatement(sql);

    ps.setString(1, idmarca);
    ps.setString(2, path1);
    ps.setString(3, path2);
    int s = ps.executeUpdate();
    System.out.println("Uploaded successfully !");

    catch(Exception e){
    finally {
    // close all the connections.

    at UploadServlet.doPost(UploadServlet.java:126)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

    Thank you in advance

  2. #2
    Join Date
    Feb 2012

    Re: java.lang.NullPointerException

    Problem solved! I had to use null check to the variables before using them to store in database.

  3. #3
    Join Date
    Jan 2011

    Re: java.lang.NullPointerException

    Glad to hear that. As a general rule I will look on the line where its coming. e.g.

    at UploadServlet.doPost(UploadServlet.java:126)

    and any object which is calling method or accessing property must be null on that line than find out why its null before putting null check because sometime problem may be on the source from where its coming.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.