This discussion is archived
9 Replies Latest reply: Jun 21, 2011 11:34 AM by 864737 RSS

Bean not behaving as expected

864737 Newbie
Currently Being Moderated
OK, I have a JSP running the following script section.
<% irCollection mgrq = new irCollection();  
      mgrq.setMgrid("Chris Novish"); 
      mgrq.populateCollection();
      int pagenum;
      if (request.getParameter("p") != null) { 
        String pagedatum=request.getParameter("p");
        pagenum = Integer.parseInt(pagedatum); 
      } else { pagenum = 0; }
      for (int i=0;i<10;i++) {
        int rownum = pagenum * 10 + i;
        InquireRecord currec = mgrq.getCurRecords(rownum);
        out.println(currec.getID()); %>
irCollection has an ArrayList property that stores a several InquireRecord objects. It gets this data from a database using the mgrid as (set in line 2 there) as the matching term.

But I'm getting an IndexOutOfBounds exception on what appears here as line 11.

I've done some tests, and I'm pretty sure that it's because populateCollection() isn't getting things done. I have a getSize method that gives me a size of 0.

I made a test class in Eclipse to make sure all my methods were working:
package com.serco.inquire;

public class test {
     
     public static void main (String[] args) {
          String mgr = "Chris Novish";
          irCollection bob = new irCollection();
          bob.setMgrid(mgr);
          bob.populateCollection();
          InquireRecord fred = bob.getCurRecords(1);
          System.out.println(fred.getID());
     }
     
}
That test class produces exactly what I'd expect.
Other than the names of some of the local variables, I can't see what I'm doign different in the JSP.

So... tell me, what noobish mistake did I make?

for the sake of being thorough, here's the populateCollection() method:
public void populateCollection() {
          try {
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               String filename = "inquire.mdb";
               String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
               database+= filename.trim() + ";DriverID=22;READONLY=true}";
               Connection con = DriverManager.getConnection( database ,"","");
               Statement s = con.createStatement();
               s.execute ("SELECT * FROM inquiries WHERE manager = '" + mgrid + "'");
               ResultSet rs = s.getResultSet();
               int cur;
               if (rs != null) { 
               while (rs.next()) {
                    cur = rs.getRow();
                    cur -- ;
                    int curID = rs.getInt("ID");
                    this.newIR.setID(curID);
                    String cursub = rs.getString("submitter");
                    this.newIR.setSubmitter(cursub);
                    this.iRecords.add(cur, this.newIR);
                    }
               this.size = iRecords.size();
               this.pages = this.size / 10;
               int remain = this.size % 10;
               if (remain > 0) { this.pages++; }
                         } else { System.out.println("no records."); }
          }
          catch (Throwable e) {
               System.out.println(e);
          }
     }
Edited by: EJP on 21/06/2011 09:15: added {noformat}
{noformat} tags. Please use them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 1. Re: Bean not behaving as expected
    EJP Guru
    Currently Being Moderated
    But I'm getting an IndexOutOfBounds exception on what appears here as line 11.
    Line 11 of what? You've posted three separate pieces of code.
                   Statement s = con.createStatement();
                   s.execute ("SELECT * FROM inquiries WHERE manager = '" + mgrid + "'");
    PreparedStatement s = con.prepareStatement("SELECT * FROM inquiries WHERE manager = ?'", Statement.NO_GENERATED_KEYS);
    s.setObject(1, mgrid);
                   ResultSet rs = s.getResultSet();
                   int cur;
                   if (rs != null) { 
    'rs' cannot be null. Test is pointless.
                        cur = rs.getRow();
                        cur -- ;
    cur = rs.getRow()-1;
                        this.iRecords.add(cur, this.newIR);
    this.iRecords.add(this.newIR);
    'newIR' mustn't be a member variable, it should be local, and refer to a newly created instance every time around the loop. At the moment you are overwriting previous elements of 'iRecords'.
                   this.pages = this.size / 10;
    this.pages = (this.size+9)/10;
                   if (remain > 0) { this.pages++; }
                             } else { System.out.println("no records."); }
    Wrong message. If 'remain' is zero there may be any multiple of 10 records. You can remove this entire block: adding 9 above does all you need.
  • 2. Re: Bean not behaving as expected
    864737 Newbie
    Currently Being Moderated
    EJP wrote:
    But I'm getting an IndexOutOfBounds exception on what appears here as line 11.
    Line 11 of what? You've posted three separate pieces of code.
    "line 11" refers to the 11th line of the first code piece. To wit: "InquireRecord currec = mgrq.getCurRecords(rownum);"
                   Statement s = con.createStatement();
                   s.execute ("SELECT * FROM inquiries WHERE manager = '" + mgrid + "'");
    PreparedStatement s = con.prepareStatement("SELECT * FROM inquiries WHERE manager = ?'", Statement.NO_GENERATED_KEYS);
    s.setObject(1, mgrid);
    mgrid is a property of the irCollection object. It is set by the 2nd line in the first code sample "mgrq.setMgrid("Chris Novish");"
    'rs' cannot be null. Test is pointless.
    How would I write a test to see if there are no actual rows returned?
    cur = rs.getRow()-1;
    I will make that change.
    this.iRecords.add(this.newIR);
    'newIR' mustn't be a member variable, it should be local, and refer to a newly created instance every time around the loop. At the moment you are overwriting previous elements of 'iRecords'.
    so I should include something like this?:
    InquireRecord localIR = new InquireRecord();
    this.pages = (this.size+9)/10;
    Wrong message. If 'remain' is zero there may be any multiple of 10 records. You can remove this entire block: adding 9 above does all you need.
    Thanks. I'll update that too.

    Edited by: The_E on Jun 21, 2011 8:10 AM
  • 3. Re: Bean not behaving as expected
    864737 Newbie
    Currently Being Moderated
    I made those changes and still get an IndexOutOfBoundsException at the same spot in the JSP
  • 4. Re: Bean not behaving as expected
    gimbal2 Guru
    Currently Being Moderated
    The_E wrote:
    How would I write a test to see if there are no actual rows returned?
    hasNext() returns false right away. An empty ResultSet is perfectly valid.
  • 5. Re: Bean not behaving as expected
    864737 Newbie
    Currently Being Moderated
    i don't see any documentation on hasNext() and Eclipse doesn't want to compile with the code.
  • 6. Re: Bean not behaving as expected
    864737 Newbie
    Currently Being Moderated
    but let me ask this. next() returns a boolean, if it returns false, the while statement won't run at all, right? so the test IS worthless. I can just put a bit at the end that checks the size of the ArrayList I've built and if that size is zero I send the message of "no records" right?
  • 7. Re: Bean not behaving as expected
    796440 Guru
    Currently Being Moderated
    The_E wrote:
    i don't see any documentation on hasNext() and Eclipse doesn't want to compile with the code.
    It's next(), actually.

    Although it doesn't explicitly state that it immediately returns false when no rows are returned, it's not hard to infer that from a) what it does say, and b) the fact that it doesn't say anything like "returns null if there are no rows". Certainly I would advise you to write a tiny program to test exactly that, rather than assuming a null even when the docs say nothing of the sort.
  • 8. Re: Bean not behaving as expected
    796440 Guru
    Currently Being Moderated
    The_E wrote:
    but let me ask this. next() returns a boolean, if it returns false, the while statement won't run at all, right? so the test IS worthless.
    Exactly!
    I can just put a bit at the end that checks the size of the ArrayList I've built and if that size is zero I send the message of "no records" right?
    I haven't read the code, but yeah, if you're creating a new AL before starting the loop, and then calling add() on each pass through the loop, then once you're done, list.isEmpty() would be a valid way to test for no rows returned.
  • 9. Re: Bean not behaving as expected
    864737 Newbie
    Currently Being Moderated
    OK, well, here's where I'm at on this issue. If I creat a Java object with a main method that instantiates my irCollection object and then includes this line:
    mgrq.populateCollection();
    It builds the ArrayList like I want.

    However, if in a JSP I use that same line of code:
    <% mgrq.populateCollection(); %>
    The object has been instantiated and it has an ArrayList property but that ArrayList remains empty.

    Why would the test.class do it but the myq.jsp file NOT do it?

    Edited by: The_E on Jun 21, 2011 11:34 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points