Results 1 to 7 of 7
  1. #1
    fiero is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default nullpointer exception in jsp

    hi guys,
    I am gettting org.apache.jasper.JasperException: java.lang.NullPointerException
    I am using Hibernate in my appln to get data
    pls help me;;;;;;;;;;;;;;;;;;;;;;;

    here is the code::::::::

    <%!
    public void jspInit(ServletConfig config) {
    try {
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();
    } catch (HibernateException e) {
    e.printStackTrace();
    }//init(ServletConfig)
    }

    /*Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn=DriverManager.getConnection("jdbc:oracle:thin :@path given:java","scott","tiger");
    PreparedStatement ps=conn.prepareStatement("select distinct(job) from hb_emp");
    ResultSet rs=ps.executeQuery();
    while(rs.next())*/



    Query query = session.createQuery("select eb.desg" + "from EmpBean eb");
    Iterator it = query.iterate();
    Boolean b = it.hasNext();
    %>
    <% while(b)
    { Object[] row = (Object[])it.next();

    %>
    <option value="<%=(String) row[0]%>"><%=(String) row[0]%></option>


    <%}
    %>
    Last edited by fiero; 09-29-2008 at 06:21 PM.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Without code, it's tough to see where an exception is thrown.

  3. #3
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I think it's in your while loop at the end of your code.

    Java Code:
    Boolean b = it.hasNext();
    %>
    <% while(b)
    { Object[] row = (Object[])it.next();
    
    %>
    <option value="<%=(String) row[0]%>"><%=(String) row[0]%></option>
    
    
    <%}
    %>
    Boolean b is only being checked once, not after each iteration of the loop like you intended.

    Try this:
    Java Code:
    %>
    <% while(it.hasNext())
    { Object[] row = (Object[])it.next();
    
    %>
    <option value="<%=(String) row[0]%>"><%=(String) row[0]%></option>
    
    
    <%}
    %>
    Putting hasNext in the loop condition will make sure that it is checked each time through the loop.

    I could be wrong though.

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I just looked at the API and noticed that Iterator's .next method throws a NoSuchElementException, not a NullPointerException so I might be wrong.

  5. #5
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Just a pointer: scriptlets are A Bad Thing anyway, and anything, scriptlets or otherwise, that connects a JSP to a database, is a bad design. Try and layer the app a bit more, otherwise you'll be copying and pasting data access code all over the place

  6. #6
    Vijay Mishra is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    It may b prb of result giving null data, just try to make condition which checks null pointer

  7. #7
    Vijay Mishra is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    It may b prb of result giving null data, just try to make condition which checks null pointer then try it

Similar Threads

  1. Exception
    By rmaadil in forum JDBC
    Replies: 1
    Last Post: 05-19-2008, 12:45 PM
  2. Exception!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-05-2008, 01:39 PM
  3. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 02:24 PM
  4. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 PM

Posting Permissions

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