java.lang.NullPointerException
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: java.lang.NullPointerException

  1. #1
    Join Date
    Feb 2012
    Posts
    2

    java.lang.NullPointerException

    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;

    try{
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    Class.forName(driver);
    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();
    if(s>0){
    System.out.println("Uploaded successfully !");
    }
    else{
    System.out.println("Error!");
    }

    }
    catch(Exception e){
    e.printStackTrace();
    }
    finally {
    // close all the connections.
    //ps.close();
    //con.close();
    }

    ----------------------------------------
    java.lang.NullPointerException
    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
    Posts
    2

    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
    Posts
    24

    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

This is a CodeGuru survey question.


Featured


HTML5 Development Center