Thread: how to resolve org.postgresql.util.PSQLException: ResultSet notpositioned properly, perhaps you need to call next.

Hi experts,

I have been trying to find a solution in the internet but there is only 1 solution offered by stackoverflow in which
theuser uses if it is not there then use a else to print out there is no more data/row that appeared in that column.
 

Basically, I was trying to get a List<String> which meet certain search condition so the data will come from 2 tables -
oneof which is a column from a join table and the List<String> falls under this table.
 

So, here's the code that I attempt to get the data out but keep getting ResultSet not positioned properly.

    String[] subjectNames = request.getParameterValues("txtsubjects");
            String zipcode = request.getParameter("location");
            List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));            
            List<tutor> myList = new ArrayList<>();
            ArrayList<String> subList = new ArrayList<>();
            CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>(); 
            tutor t = new tutor();

            
            String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN
tutor_subjectt2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?))";
 
            
            PreparedStatement ps2 = connection.prepareStatement(sql1);
            ps2.setString(1, zipcode);
            for (int i = 0; i < subjectNames.length; i++) {
                Array array = connection.createArrayOf("text", subjList.toArray());
                ps2.setArray(2, array);
            }
            ResultSet rs = ps2.executeQuery();
            while (rs.next()) {        
                t.setContactNo(rs.getString("tutor_contact_no"));            
                t.setZipcode(rs.getString("zipcode"));
                t.settutor_id(rs.getInt("tutor_id"));            

               
                for (String s: subjList)
                    if (s.contains(rs.getString("subject_name")))
                      subjList.add(rs.getString("subject_name"));
                     newSub.addAll(subjList);
}
myList.add(t);                    
                request.setAttribute("tutors", myList);                       
                request.setAttribute("sub", newSub);    

Please.  I hope someone can tell me what's wrong with the code or how to overcome this error...I have been struggling
for2 weeks already.
 

Thanks.
            }
                myList.add(t);                    
                request.setAttribute("tutors", myList);                       
                request.setAttribute("sub", newSub);




Hello,

On Mon, 2019-09-02 at 06:28 +0000, Karen Goh wrote:
> Hi experts,
> 
> I have been trying to find a solution in the internet but there is
> only 1 solution offered by stackoverflow in which the user uses if it
> is not there then use a else to print out there is no more data/row
> that appeared in that column.
> 
> Basically, I was trying to get a List<String> which meet certain
> search condition so the data will come from 2 tables - one of which
> is a column from a join table and the List<String> falls under this
> table.
> 
> So, here's the code that I attempt to get the data out but keep
> getting ResultSet not positioned properly.
> 
>     String[] subjectNames =
> request.getParameterValues("txtsubjects");
>             String zipcode =
> request.getParameter("location");
>             List<String> subjList = new
> ArrayList<String>(Arrays.asList(subjectNames));            
>             List<tutor> myList = new ArrayList<>();
>             ArrayList<String> subList = new ArrayList<>();
>             CopyOnWriteArrayList<String>newSub = new
> CopyOnWriteArrayList<String>(); 
>             tutor t = new tutor();
> 
>             
>             String sql1 = "select t.tutor_contact_no,
> t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t
> JOIN  tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode =
> ?  And  t2.subject_name = Any((?))";
>             
>             PreparedStatement ps2 =
> connection.prepareStatement(sql1);
>             ps2.setString(1, zipcode);
>             for (int i = 0; i < subjectNames.length; i++) {
>                 Array array =
> connection.createArrayOf("text", subjList.toArray());
>                 ps2.setArray(2, array);
>             }
>             ResultSet rs = ps2.executeQuery();
>             while (rs.next()) {        
>                 t.setContactNo(rs.getString("tutor_cont
> act_no"));            
>                 t.setZipcode(rs.getString("zipcode"));
>                 t.settutor_id(rs.getInt("tutor_id"));    
>         
> 
>                
>                 for (String s: subjList)
>                     if
> (s.contains(rs.getString("subject_name")))
>                       subjList.add(rs.getString("subject_na
> me"));
>                      newSub.addAll(subjList);
> }
> myList.add(t);                    
>                 request.setAttribute("tutors", myList);
>                        
>                 request.setAttribute("sub", newSub);    
> 
> Please.  I hope someone can tell me what's wrong with the code or how
> to overcome this error...I have been struggling for 2 weeks already.
> 
> Thanks.
>             }
>                 myList.add(t);                
>     
>                 request.setAttribute("tutors", myList);
>                        
>                 request.setAttribute("sub", newSub);
> 
> 
> 


I'm not an "expert".

It is customary on these lists to supply the Postgres version, O/S,
JDBC driver version and the JDK version.
Also the error message from the PostgreSql log file.

Is t.zipcode defined as INTEGER or VARCHAR?

I would have used:-

AND  T2.subject_name = ANY(string_to_array(?))

supplying a suitably constructed string as the second parameter.

You also need to cater for the query returning no rows.

HTH,
Robert





I have problem replying at below due to Yahoo's mail limitation.

You suggestion doesn't help me in resolving the exception.

And there is nothing wrong with the sql as I got the solution from a guy who is an expert in Postgresql. Unfortunately, I can't get him now.

The version I am using is 11.

And since the code works inside Eclipse, so OS doesn't matter....

Can someone tells me how to resolve the problem?

As for the zipcode, it is not a concern whether it is text or whatsoever....because as mentioned all the other data can be printed....


On Monday, September 2, 2019, 3:35:21 PM GMT+8, rob stone <floriparob@gmail.com> wrote:


Hello,

On Mon, 2019-09-02 at 06:28 +0000, Karen Goh wrote:
> Hi experts,
>
> I have been trying to find a solution in the internet but there is
> only 1 solution offered by stackoverflow in which the user uses if it
> is not there then use a else to print out there is no more data/row
> that appeared in that column.
>
> Basically, I was trying to get a List<String> which meet certain
> search condition so the data will come from 2 tables - one of which
> is a column from a join table and the List<String> falls under this
> table.
>
> So, here's the code that I attempt to get the data out but keep
> getting ResultSet not positioned properly.
>
> String[] subjectNames =
> request.getParameterValues("txtsubjects");
> String zipcode =
> request.getParameter("location");
> List<String> subjList = new
> ArrayList<String>(Arrays.asList(subjectNames));
> List<tutor> myList = new ArrayList<>();
> ArrayList<String> subList = new ArrayList<>();
> CopyOnWriteArrayList<String>newSub = new
> CopyOnWriteArrayList<String>();
> tutor t = new tutor();
>
>
> String sql1 = "select t.tutor_contact_no,
> t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t
> JOIN tutor_subject t2 ON t.tutor_id = t2.tutor_id where t.zipcode =
> ? And t2.subject_name = Any((?))";
>
> PreparedStatement ps2 =
> connection.prepareStatement(sql1);
> ps2.setString(1, zipcode);
> for (int i = 0; i < subjectNames.length; i++) {
> Array array =
> connection.createArrayOf("text", subjList.toArray());
> ps2.setArray(2, array);
> }
> ResultSet rs = ps2.executeQuery();
> while (rs.next()) {
> t.setContactNo(rs.getString("tutor_cont
> act_no"));
> t.setZipcode(rs.getString("zipcode"));
> t.settutor_id(rs.getInt("tutor_id"));
>
>
>
> for (String s: subjList)
> if
> (s.contains(rs.getString("subject_name")))
> subjList.add(rs.getString("subject_na
> me"));
> newSub.addAll(subjList);
> }
> myList.add(t);
> request.setAttribute("tutors", myList);
>
> request.setAttribute("sub", newSub);
>
> Please. I hope someone can tell me what's wrong with the code or how
> to overcome this error...I have been struggling for 2 weeks already.
>
> Thanks.
> }
> myList.add(t);
>
> request.setAttribute("tutors", myList);
>
> request.setAttribute("sub", newSub);

>
>
>


I'm not an "expert".

It is customary on these lists to supply the Postgres version, O/S,
JDBC driver version and the JDK version.
Also the error message from the PostgreSql log file.

Is t.zipcode defined as INTEGER or VARCHAR?

I would have used:-

AND T2.subject_name = ANY(string_to_array(?))

supplying a suitably constructed string as the second parameter.

You also need to cater for the query returning no rows.

Hi Robert,

Ca

HTH,
Robert




> On Sep 2, 2019, at 12:28 AM, Karen Goh <karenworld@yahoo.com> wrote:
>
> Hi experts,
>
> I have been trying to find a solution in the internet but there is only 1 solution offered by stackoverflow in which
theuser uses if it is not there then use a else to print out there is no more data/row that appeared in that column. 
>
> Basically, I was trying to get a List<String> which meet certain search condition so the data will come from 2 tables
-one of which is a column from a join table and the List<String> falls under this table. 
>
> So, here's the code that I attempt to get the data out but keep getting ResultSet not positioned properly.
>
>     String[] subjectNames = request.getParameterValues("txtsubjects");
>             String zipcode = request.getParameter("location");
>             List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
>             List<tutor> myList = new ArrayList<>();
>             ArrayList<String> subList = new ArrayList<>();
>             CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>();
>             tutor t = new tutor();
>
>
>             String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN
tutor_subjectt2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?))"; 
>
>             PreparedStatement ps2 = connection.prepareStatement(sql1);
>             ps2.setString(1, zipcode);
>             for (int i = 0; i < subjectNames.length; i++) {
>                 Array array = connection.createArrayOf("text", subjList.toArray());
>                 ps2.setArray(2, array);
>             }
>             ResultSet rs = ps2.executeQuery();
>             while (rs.next()) {
>                 t.setContactNo(rs.getString("tutor_contact_no"));
>                 t.setZipcode(rs.getString("zipcode"));
>                 t.settutor_id(rs.getInt("tutor_id"));
>
>
>                 for (String s: subjList)
>                     if (s.contains(rs.getString("subject_name")))

Are you sure you want to add to the subList you’re iterating through?
>                       subjList.add(rs.getString("subject_name"));
>                      newSub.addAll(subjList);
> }
> myList.add(t);
>                 request.setAttribute("tutors", myList);
>                 request.setAttribute("sub", newSub);
>
> Please.  I hope someone can tell me what's wrong with the code or how to overcome this error...I have been struggling
for2 weeks already. 
>
> Thanks.
>             }
>                 myList.add(t);
>                 request.setAttribute("tutors", myList);
>                 request.setAttribute("sub", newSub);
>
>
>






Hi Rob,

I am still getting the same type of error even if I store it in another List...

CopyOnWriteArrayList<String> subList = new CopyOnWriteArrayList<>();

subList.add(rs.getString("subject_name")); // note: using a new List

Are there anybody experienced Java developer that can help ?


On Tuesday, September 3, 2019, 9:12:34 AM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:




> On Sep 2, 2019, at 12:28 AM, Karen Goh <karenworld@yahoo.com> wrote:
>
> Hi experts,
>
> I have been trying to find a solution in the internet but there is only 1 solution offered by stackoverflow in which the user uses if it is not there then use a else to print out there is no more data/row that appeared in that column.
>
> Basically, I was trying to get a List<String> which meet certain search condition so the data will come from 2 tables - one of which is a column from a join table and the List<String> falls under this table.
>
> So, here's the code that I attempt to get the data out but keep getting ResultSet not positioned properly.
>
> String[] subjectNames = request.getParameterValues("txtsubjects");
> String zipcode = request.getParameter("location");
> List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
> List<tutor> myList = new ArrayList<>();
> ArrayList<String> subList = new ArrayList<>();
> CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>();
> tutor t = new tutor();
>
>
> String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject t2 ON t.tutor_id = t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?))";
>
> PreparedStatement ps2 = connection.prepareStatement(sql1);
> ps2.setString(1, zipcode);
> for (int i = 0; i < subjectNames.length; i++) {
> Array array = connection.createArrayOf("text", subjList.toArray());
> ps2.setArray(2, array);
> }
> ResultSet rs = ps2.executeQuery();
> while (rs.next()) {
> t.setContactNo(rs.getString("tutor_contact_no"));
> t.setZipcode(rs.getString("zipcode"));
> t.settutor_id(rs.getInt("tutor_id"));
>
>
> for (String s: subjList)
> if (s.contains(rs.getString("subject_name")))

Are you sure you want to add to the subList you’re iterating through?

> subjList.add(rs.getString("subject_name"));
> newSub.addAll(subjList);
> }
> myList.add(t);
> request.setAttribute("tutors", myList);
> request.setAttribute("sub", newSub);
>
> Please. I hope someone can tell me what's wrong with the code or how to overcome this error...I have been struggling for 2 weeks already.
>
> Thanks.
> }
> myList.add(t);
> request.setAttribute("tutors", myList);
> request.setAttribute("sub", newSub);
>
>
>


Well we’ll need to see the stacktrace from the exception and proof (line numbers) that the exception is in the code you
sent.


> On Sep 2, 2019, at 11:15 PM, Karen Goh <karenworld@yahoo.com> wrote:
>
>
>
> Hi Rob,
>
> I am still getting the same type of error even if I store it in another List...
>
> CopyOnWriteArrayList<String> subList = new CopyOnWriteArrayList<>();
>
> subList.add(rs.getString("subject_name")); // note: using a new List
>
> Are there anybody experienced Java developer that can help ?
>
>
> On Tuesday, September 3, 2019, 9:12:34 AM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:
>
>
>
>
> > On Sep 2, 2019, at 12:28 AM, Karen Goh <karenworld@yahoo.com> wrote:
> >
> > Hi experts,
> >
> > I have been trying to find a solution in the internet but there is only 1 solution offered by stackoverflow in
whichthe user uses if it is not there then use a else to print out there is no more data/row that appeared in that
column.
> >
> > Basically, I was trying to get a List<String> which meet certain search condition so the data will come from 2
tables- one of which is a column from a join table and the List<String> falls under this table. 
> >
> > So, here's the code that I attempt to get the data out but keep getting ResultSet not positioned properly.
> >
> > String[] subjectNames = request.getParameterValues("txtsubjects");
> > String zipcode = request.getParameter("location");
> > List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
> > List<tutor> myList = new ArrayList<>();
> > ArrayList<String> subList = new ArrayList<>();
> > CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>();
> > tutor t = new tutor();
> >
> >
> > String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject
t2ON t.tutor_id = t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?))"; 
> >
> > PreparedStatement ps2 = connection.prepareStatement(sql1);
> > ps2.setString(1, zipcode);
> > for (int i = 0; i < subjectNames.length; i++) {
> > Array array = connection.createArrayOf("text", subjList.toArray());
> > ps2.setArray(2, array);
> > }
> > ResultSet rs = ps2.executeQuery();
> > while (rs.next()) {
> > t.setContactNo(rs.getString("tutor_contact_no"));
> > t.setZipcode(rs.getString("zipcode"));
> > t.settutor_id(rs.getInt("tutor_id"));
> >
> >
> > for (String s: subjList)
> > if (s.contains(rs.getString("subject_name")))
>
> Are you sure you want to add to the subList you’re iterating through?
>
> > subjList.add(rs.getString("subject_name"));
> > newSub.addAll(subjList);
> > }
> > myList.add(t);
> > request.setAttribute("tutors", myList);
> > request.setAttribute("sub", newSub);
> >
> > Please. I hope someone can tell me what's wrong with the code or how to overcome this error...I have been
strugglingfor 2 weeks already. 
> >
> > Thanks.
> > }
> > myList.add(t);
> > request.setAttribute("tutors", myList);
> > request.setAttribute("sub", newSub);
> >
> >
> >
>
>




Sure.
I tried various ways to see if there is any mistakes. Still, I get the same old error :(

Very difficult to type in Yahoo mail now...so I am continuing here (after you read the last sentence of the mail)

So, even if I remove the System.out.print...it still gives me the error...

Could it be Tomcat problem ? because I remove the page forwarded lines all altogether and it gave me the same old error as per not positioned properly, blah ...

org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2052)
at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2485)
at Controller.searchController.doPost(searchController.java:117)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

And below the line that is giving problem :
subjList.add(rs.getString("subject_name"));
newSub.addAll(subjList);

System.out.println( "tutor ID=" + rs.getInt("tutor_id") + "zipcode =" + rs.getString("zipcode") +
"subjectName=" + rs.getString("subject_name") + "tutorContact=" +
rs.getString("tutor_contact_no"));
}}
I now inserted it before the close bracket of resultset.

It points to System.out.print





On Wednesday, September 4, 2019, 3:23:46 AM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:


Well we’ll need to see the stacktrace from the exception and proof (line numbers) that the exception is in the code you sent.


> On Sep 2, 2019, at 11:15 PM, Karen Goh <karenworld@yahoo.com> wrote:
>
>
>
> Hi Rob,
>
> I am still getting the same type of error even if I store it in another List...
>
> CopyOnWriteArrayList<String> subList = new CopyOnWriteArrayList<>();
>
> subList.add(rs.getString("subject_name")); // note: using a new List
>
> Are there anybody experienced Java developer that can help ?
>
>
> On Tuesday, September 3, 2019, 9:12:34 AM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:
>
>
>
>
> > On Sep 2, 2019, at 12:28 AM, Karen Goh <karenworld@yahoo.com> wrote:
> >
> > Hi experts,
> >
> > I have been trying to find a solution in the internet but there is only 1 solution offered by stackoverflow in which the user uses if it is not there then use a else to print out there is no more data/row that appeared in that column.
> >
> > Basically, I was trying to get a List<String> which meet certain search condition so the data will come from 2 tables - one of which is a column from a join table and the List<String> falls under this table.
> >
> > So, here's the code that I attempt to get the data out but keep getting ResultSet not positioned properly.
> >
> > String[] subjectNames = request.getParameterValues("txtsubjects");
> > String zipcode = request.getParameter("location");
> > List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
> > List<tutor> myList = new ArrayList<>();
> > ArrayList<String> subList = new ArrayList<>();
> > CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>();
> > tutor t = new tutor();
> >
> >
> > String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject t2 ON t.tutor_id = t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?))";
> >
> > PreparedStatement ps2 = connection.prepareStatement(sql1);
> > ps2.setString(1, zipcode);
> > for (int i = 0; i < subjectNames.length; i++) {
> > Array array = connection.createArrayOf("text", subjList.toArray());
> > ps2.setArray(2, array);
> > }
> > ResultSet rs = ps2.executeQuery();
> > while (rs.next()) {
> > t.setContactNo(rs.getString("tutor_contact_no"));
> > t.setZipcode(rs.getString("zipcode"));
> > t.settutor_id(rs.getInt("tutor_id"));
> >
> >
> > for (String s: subjList)
> > if (s.contains(rs.getString("subject_name")))
>
> Are you sure you want to add to the subList you’re iterating through?
>
> > subjList.add(rs.getString("subject_name"));
> > newSub.addAll(subjList);
> > }
> > myList.add(t);
> > request.setAttribute("tutors", myList);
> > request.setAttribute("sub", newSub);
> >
> > Please. I hope someone can tell me what's wrong with the code or how to overcome this error...I have been struggling for 2 weeks already.
> >
> > Thanks.
> > }
> > myList.add(t);
> > request.setAttribute("tutors", myList);
> > request.setAttribute("sub", newSub);
> >
> >
> >
>
>



> On Sep 3, 2019, at 9:38 PM, Karen Goh <karenworld@yahoo.com> wrote:
>
> Sure.
> I tried various ways to see if there is any mistakes. Still, I get the same old error :(
>
> Very difficult to type in Yahoo mail now...so I am continuing here (after you read the last sentence of the mail)
>
> So, even if I remove the System.out.print...it still gives me the error...
>
> Could it be Tomcat problem ? because I remove the page forwarded lines all altogether and it gave me the same old
erroras per not positioned properly, blah ... 
>
> org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
> at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2052)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2485)
> at Controller.searchController.doPost(searchController.java:117)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
> at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
> at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
> at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
> at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> at java.lang.Thread.run(Thread.java:748)
>
> And below the line that is giving problem :
> subjList.add(rs.getString("subject_name"));
> newSub.addAll(subjList);
>
> System.out.println( "tutor ID=" + rs.getInt("tutor_id") + "zipcode =" + rs.getString("zipcode") +
> "subjectName=" + rs.getString("subject_name") + "tutorContact=" +
> rs.getString("tutor_contact_no"));
> }}
> I now inserted it before the close bracket of resultset.
>
> It points to System.out.print
>
Is "Controller.searchController.doPost(searchController.java:117)” your code?  Do you in fact call rs.next()
appropriately?
Also unless there’s a subjList = new List<> (or subjList.clear()) somewhere nearby you may be adding the contents of
subjListmore than once.  There’s not enough of your code here to tell for sure exactly what’s going on. 




Hi Rob,

I find the way you asked me if it is my code very strange and why would you say it is not my code, and I have been fighting every minute to resolve this and my head is pain with all these error !

If you can't help me, the least you can is to own up you don't have the answer or refrain from saying something that is not RIGHT.

Thanks
On Wednesday, September 4, 2019, 1:13:15 PM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:




> On Sep 3, 2019, at 9:38 PM, Karen Goh <karenworld@yahoo.com> wrote:
>
> Sure.
> I tried various ways to see if there is any mistakes. Still, I get the same old error :(
>
> Very difficult to type in Yahoo mail now...so I am continuing here (after you read the last sentence of the mail)
>
> So, even if I remove the System.out.print...it still gives me the error...
>
> Could it be Tomcat problem ? because I remove the page forwarded lines all altogether and it gave me the same old error as per not positioned properly, blah ...
>
> org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
> at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2052)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2485)
> at Controller.searchController.doPost(searchController.java:117)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
> at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
> at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
> at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
> at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> at java.lang.Thread.run(Thread.java:748)
>
> And below the line that is giving problem :
> subjList.add(rs.getString("subject_name"));
> newSub.addAll(subjList);
>
> System.out.println( "tutor ID=" + rs.getInt("tutor_id") + "zipcode =" + rs.getString("zipcode") +
> "subjectName=" + rs.getString("subject_name") + "tutorContact=" +
> rs.getString("tutor_contact_no"));
> }}
> I now inserted it before the close bracket of resultset.
>
> It points to System.out.print

>
Is "Controller.searchController.doPost(searchController.java:117)” your code?  Do you in fact call rs.next() appropriately?
Also unless there’s a subjList = new List<> (or subjList.clear()) somewhere nearby you may be adding the contents of subjList more than once.  There’s not enough of your code here to tell for sure exactly what’s going on.



Hi,

Rob is not being impolite. Asking to verify if it is your code is the most polite thing he can do since you have not posted the full code you are running.
If the Controller would have been a 3rd party library code then Rob would have had to pivot to searching what is wrong there.
In this case he made an educated guess and tried to propose further actions based on it.

It would really help if you posted the full contents of the method that obtains the Result set and handles it. We are really searching for the rs.next() call, as that is what the error message says is missing.
The two likely scenarios are: either it is missing from the code completely or you fail to call it again in a loop after each row.

-Mikko


From: Karen Goh <karenworld@yahoo.com>
Sent: 04 September 2019 08:58
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-jdbc@lists.postgresql.org <pgsql-jdbc@lists.postgresql.org>
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
 
Hi Rob,

I find the way you asked me if it is my code very strange and why would you say it is not my code, and I have been fighting every minute to resolve this and my head is pain with all these error !

If you can't help me, the least you can is to own up you don't have the answer or refrain from saying something that is not RIGHT.

Thanks
On Wednesday, September 4, 2019, 1:13:15 PM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:




> On Sep 3, 2019, at 9:38 PM, Karen Goh <karenworld@yahoo.com> wrote:
>
> Sure.
> I tried various ways to see if there is any mistakes. Still, I get the same old error :(
>
> Very difficult to type in Yahoo mail now...so I am continuing here (after you read the last sentence of the mail)
>
> So, even if I remove the System.out.print...it still gives me the error...
>
> Could it be Tomcat problem ? because I remove the page forwarded lines all altogether and it gave me the same old error as per not positioned properly, blah ...
>
> org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
> at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2052)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2485)
> at Controller.searchController.doPost(searchController.java:117)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
> at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
> at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
> at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
> at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> at java.lang.Thread.run(Thread.java:748)
>
> And below the line that is giving problem :
> subjList.add(rs.getString("subject_name"));
> newSub.addAll(subjList);
>
> System.out.println( "tutor ID=" + rs.getInt("tutor_id") + "zipcode =" + rs.getString("zipcode") +
> "subjectName=" + rs.getString("subject_name") + "tutorContact=" +
> rs.getString("tutor_contact_no"));
> }}
> I now inserted it before the close bracket of resultset.
>
> It points to System.out.print

>
Is "Controller.searchController.doPost(searchController.java:117)” your code?  Do you in fact call rs.next() appropriately?
Also unless there’s a subjList = new List<> (or subjList.clear()) somewhere nearby you may be adding the contents of subjList more than once.  There’s not enough of your code here to tell for sure exactly what’s going on.



On 4-9-2019 07:58, Karen Goh wrote:
> Hi Rob,
> 
> I find the way you asked me if it is my code very strange and why would 
> you say it is not my code, and I have been fighting every minute to 
> resolve this and my head is pain with all these error !

With "your code", Rob was referring to the code you posted in your 
initial email. He was asking you to confirm that the code you posted 
earlier indeed contains the line 
Controller.searchController.doPost(searchController.java:117

Better would even be to explicitly point out the specific line in the 
code (with context).

It is very hard to diagnose a problem based on an error and a snippet of 
code without context.

Mark
-- 
Mark Rotteveel



Here's the entire code again:

String[] subjectNames = request.getParameterValues("txtsubjects");
> > String zipcode = request.getParameter("location");
> > List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
> > List<tutor> myList = new ArrayList<>();
> > ArrayList<String> subList = new ArrayList<>();
> > CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>();
> > tutor t = new tutor();
> >
> >
> > String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject t2 ON t.tutor_id = t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?))";
> >
> > PreparedStatement ps2 = connection.prepareStatement(sql1);
> > ps2.setString(1, zipcode);
> > for (int i = 0; i < subjectNames.length; i++) {
> > Array array = connection.createArrayOf("text", subjList.toArray());
> > ps2.setArray(2, array);
> > }
> > ResultSet rs = ps2.executeQuery();
> > while (rs.next()) {
> > t.setContactNo(rs.getString("tutor_contact_no"));
> > t.setZipcode(rs.getString("zipcode"));
> > t.settutor_id(rs.getInt("tutor_id"));
> >for (String : subjList)
even if i used subList.add(rs.get("mycolumnNameforthesubjects")
And then I used a system.out.println here to print out all the id, contact No, zipcode and subjects name
and then according to the stacktrace error, the System.out.println is the line that has the error - ResultSet not positioned properly, perhaps you need to call next.


Basically, I have removed the setAttributes lines and simply replace it with a System out line (which I had earlier used it to see the print-out and it worked before so am not sure why it is not working this time round)
to make it easier to diagnose what has gone wrong....

It could be due to Tomcat as yesterday I was trying to find out the issue but again and again, even after cleaning Tomcat directory before I run the code, even with certain lines already taken off, it shows me things that are not supposed to be there...

On Wednesday, September 4, 2019, 3:14:00 PM GMT+8, Mark Rotteveel <mark@lawinegevaar.nl> wrote:


On 4-9-2019 07:58, Karen Goh wrote:
> Hi Rob,
>
> I find the way you asked me if it is my code very strange and why would
> you say it is not my code, and I have been fighting every minute to
> resolve this and my head is pain with all these error !

With "your code", Rob was referring to the code you posted in your
initial email. He was asking you to confirm that the code you posted
earlier indeed contains the line

Controller.searchController.doPost(searchController.java:117


Better would even be to explicitly point out the specific line in the
code (with context).

It is very hard to diagnose a problem based on an error and a snippet of
code without context.

Mark
--
Mark Rotteveel



On 4-9-2019 09:33, Karen Goh wrote:
> Here's the entire code again:
> 
> String[] subjectNames = request.getParameterValues("txtsubjects");
>  > > String zipcode = request.getParameter("location");
>  > > List<String> subjList = new 
> ArrayList<String>(Arrays.asList(subjectNames));
>  > > List<tutor> myList = new ArrayList<>();
>  > > ArrayList<String> subList = new ArrayList<>();
>  > > CopyOnWriteArrayList<String>newSub = new 
> CopyOnWriteArrayList<String>();
>  > > tutor t = new tutor();
>  > >
>  > >
>  > > String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, 
> t2.subject_name FROM s_tutor t JOIN tutor_subject t2 ON t.tutor_id = 
> t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?))";
>  > >
>  > > PreparedStatement ps2 = connection.prepareStatement(sql1);
>  > > ps2.setString(1, zipcode);
>  > > for (int i = 0; i < subjectNames.length; i++) {
>  > > Array array = connection.createArrayOf("text", subjList.toArray());
>  > > ps2.setArray(2, array);
>  > > }
>  > > ResultSet rs = ps2.executeQuery();
>  > > while (rs.next()) {
>  > > t.setContactNo(rs.getString("tutor_contact_no"));
>  > > t.setZipcode(rs.getString("zipcode"));
>  > > t.settutor_id(rs.getInt("tutor_id"));
>  > >for (String : subjList)
> even if i used subList.add(rs.get("mycolumnNameforthesubjects")
> And then I used a system.out.println here to print out all the id, 
> contact No, zipcode and subjects name
> and then according to the stacktrace error, the System.out.println is 
> the line that has the error - ResultSet not positioned properly, perhaps 
> you need to call next.
> 
> Basically, I have removed the setAttributes lines and simply replace it 
> with a System out line (which I had earlier used it to see the print-out 
> and it worked before so am not sure why it is not working this time round)
> to make it easier to diagnose what has gone wrong....
>
> It could be due to Tomcat as yesterday I was trying to find out the 
> issue but again and again, even after cleaning Tomcat directory before I 
> run the code, even with certain lines already taken off, it shows me 
> things that are not supposed to be there...

We are missing a lot of context here. The code you quoted doesn't 
contain a System.out line, nor setAttributes lines. You also haven't 
indicated which line is line 117 of searchController.java.

Based on the exception stacktrace, the only thing we can tell you is 
that - apparently - the code is calling ResultSet.getInt(..) before a 
call to ResultSet.next() (or possibly after ResultSet.next() has 
returned false.

The only suspicious thing I noticed in the code in your first email is 
that the for-loop nested in the `while (rs.next())` loop doesn't use a 
block, while the indentation suggests it should.

As an aside, given the code has multiple > prefixes and no indentation 
makes it hard to read: could you copy directly from your IDE instead, or 
maybe publish it in a GitHub gist or something similar?

Mark
-- 
Mark Rotteveel



Hello,

On Wed, 2019-09-04 at 07:33 +0000, Karen Goh wrote:
> Here's the entire code again:
> 
> String[] subjectNames = request.getParameterValues("txtsubjects");
> > > String zipcode = request.getParameter("location");
> > > List<String> subjList = new
> ArrayList<String>(Arrays.asList(subjectNames)); 
> > > List<tutor> myList = new ArrayList<>();
> > > ArrayList<String> subList = new ArrayList<>();
> > > CopyOnWriteArrayList<String>newSub = new
> CopyOnWriteArrayList<String>();
> > > tutor t = new tutor();
> > >
> > > 
> > > String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id,
> t2.subject_name FROM s_tutor t JOIN tutor_subject t2 ON t.tutor_id =
> t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?))";
> > > 
> > > PreparedStatement ps2 = connection.prepareStatement(sql1);
> > > ps2.setString(1, zipcode);
> > > for (int i = 0; i < subjectNames.length; i++) {
> > > Array array = connection.createArrayOf("text",
> subjList.toArray());
> > > ps2.setArray(2, array);
> > > }
> > > ResultSet rs = ps2.executeQuery();
> > > while (rs.next()) { 
> > > t.setContactNo(rs.getString("tutor_contact_no")); 
> > > t.setZipcode(rs.getString("zipcode"));
> > > t.settutor_id(rs.getInt("tutor_id")); 
> > >for (String : subjList)
> 

As I said last time, I'm not an "expert".

subjectNames is declared as an array.
In your "for" loop you will be doing ps2.setArray(2, array); multiple
times depending on the number of subjects contained in subjectNames.

I suspect the query is failing and as there is no try .. catch block to
trap the SQLException, this is not reported back to your code.

If you alter the postgresql.conf to log all SQL statements, restart,
run your test again, look in the log file, it will show the passed
parameter values. You can then verify that that the array passed as the
second parameter is correct.

HTH,
Robert






As I said last time, I'm not an "expert".

subjectNames is declared as an array.
In your "for" loop you will be doing ps2.setArray(2, array); multiple
times depending on the number of subjects contained in subjectNames.

I suspect the query is failing and as there is no try .. catch block to
trap the SQLException, this is not reported back to your code.

If you alter the postgresql.conf to log all SQL statements, restart,
run your test again, look in the log file, it will show the passed
parameter values. You can then verify that that the array passed as the
second parameter is correct.

HTH,
Robert
Relieved am I that other’s could see that I was not doubting the OP wrote the code in question.  I do consider myself a Java programmer (expert is a trickier question).

We do need to see the entire routine (I think that might be doPost()).  If that’s more than 100 lines I suggest a refactor first. Have the doPost() call a method which does nothing but the desired interaction. 
Not at all. As mentioned, I could print out the queried subectNames, at the ide and at the database. But, I can’t anymore.

However, I need to deal with Tomcat problem first to give me a real reflection of my code error.


Sent from Yahoo Mail for iPhone

On Thursday, September 5, 2019, 12:39 AM, Rob Sargent <robjsargent@gmail.com> wrote:


As I said last time, I'm not an "expert".

subjectNames is declared as an array.
In your "for" loop you will be doing ps2.setArray(2, array); multiple
times depending on the number of subjects contained in subjectNames.

I suspect the query is failing and as there is no try .. catch block to
trap the SQLException, this is not reported back to your code.

If you alter the postgresql.conf to log all SQL statements, restart,
run your test again, look in the log file, it will show the passed
parameter values. You can then verify that that the array passed as the
second parameter is correct.

HTH,
Robert
Relieved am I that other’s could see that I was not doubting the OP wrote the code in question.  I do consider myself a Java programmer (expert is a trickier question).

We do need to see the entire routine (I think that might be doPost()).  If that’s more than 100 lines I suggest a refactor first. Have the doPost() call a method which does nothing but the desired interaction. 

I have posted the full code already in my first email. What other parts do you want to see. I am really puzzled about your question also.....

On Wednesday, September 4, 2019, 4:02:21 PM GMT+8, Mikko Tiihonen <mikko.tiihonen@nitor.com> wrote:


#yiv9100579513 P {margin-top:0;margin-bottom:0;}
Hi,

Rob is not being impolite. Asking to verify if it is your code is the most polite thing he can do since you have not posted the full code you are running.
If the Controller would have been a 3rd party library code then Rob would have had to pivot to searching what is wrong there.
In this case he made an educated guess and tried to propose further actions based on it.

It would really help if you posted the full contents of the method that obtains the Result set and handles it. We are really searching for the rs.next() call, as that is what the error message says is missing.
The two likely scenarios are: either it is missing from the code completely or you fail to call it again in a loop after each row.

-Mikko


From: Karen Goh <karenworld@yahoo.com>
Sent: 04 September 2019 08:58
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-jdbc@lists.postgresql.org <pgsql-jdbc@lists.postgresql.org>
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
 
Hi Rob,

I find the way you asked me if it is my code very strange and why would you say it is not my code, and I have been fighting every minute to resolve this and my head is pain with all these error !

If you can't help me, the least you can is to own up you don't have the answer or refrain from saying something that is not RIGHT.

Thanks
On Wednesday, September 4, 2019, 1:13:15 PM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:




> On Sep 3, 2019, at 9:38 PM, Karen Goh <karenworld@yahoo.com> wrote:
>
> Sure.
> I tried various ways to see if there is any mistakes. Still, I get the same old error :(
>
> Very difficult to type in Yahoo mail now...so I am continuing here (after you read the last sentence of the mail)
>
> So, even if I remove the System.out.print...it still gives me the error...
>
> Could it be Tomcat problem ? because I remove the page forwarded lines all altogether and it gave me the same old error as per not positioned properly, blah ...
>
> org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
> at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2052)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2485)
> at Controller.searchController.doPost(searchController.java:117)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
> at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
> at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
> at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
> at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> at java.lang.Thread.run(Thread.java:748)
>
> And below the line that is giving problem :
> subjList.add(rs.getString("subject_name"));
> newSub.addAll(subjList);
>
> System.out.println( "tutor ID=" + rs.getInt("tutor_id") + "zipcode =" + rs.getString("zipcode") +
> "subjectName=" + rs.getString("subject_name") + "tutorContact=" +
> rs.getString("tutor_contact_no"));
> }}
> I now inserted it before the close bracket of resultset.
>
> It points to System.out.print

>
Is "Controller.searchController.doPost(searchController.java:117)” your code?  Do you in fact call rs.next() appropriately?
Also unless there’s a subjList = new List<> (or subjList.clear()) somewhere nearby you may be adding the contents of subjList more than once.  There’s not enough of your code here to tell for sure exactly what’s going on.



After you address your Tomcat issue I would like to see the entire method named in the stack

On Sep 4, 2019, at 7:59 PM, Karen Goh <karenworld@yahoo.com> wrote:


I have posted the full code already in my first email. What other parts do you want to see. I am really puzzled about your question also.....

On Wednesday, September 4, 2019, 4:02:21 PM GMT+8, Mikko Tiihonen <mikko.tiihonen@nitor.com> wrote:


Hi,

Rob is not being impolite. Asking to verify if it is your code is the most polite thing he can do since you have not posted the full code you are running.
If the Controller would have been a 3rd party library code then Rob would have had to pivot to searching what is wrong there.
In this case he made an educated guess and tried to propose further actions based on it.

It would really help if you posted the full contents of the method that obtains the Result set and handles it. We are really searching for the rs.next() call, as that is what the error message says is missing.
The two likely scenarios are: either it is missing from the code completely or you fail to call it again in a loop after each row.

-Mikko


From: Karen Goh <karenworld@yahoo.com>
Sent: 04 September 2019 08:58
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-jdbc@lists.postgresql.org <pgsql-jdbc@lists.postgresql.org>
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
 
Hi Rob,

I find the way you asked me if it is my code very strange and why would you say it is not my code, and I have been fighting every minute to resolve this and my head is pain with all these error !

If you can't help me, the least you can is to own up you don't have the answer or refrain from saying something that is not RIGHT.

Thanks
On Wednesday, September 4, 2019, 1:13:15 PM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:




> On Sep 3, 2019, at 9:38 PM, Karen Goh <karenworld@yahoo.com> wrote:
>
> Sure.
> I tried various ways to see if there is any mistakes. Still, I get the same old error :(
>
> Very difficult to type in Yahoo mail now...so I am continuing here (after you read the last sentence of the mail)
>
> So, even if I remove the System.out.print...it still gives me the error...
>
> Could it be Tomcat problem ? because I remove the page forwarded lines all altogether and it gave me the same old error as per not positioned properly, blah ...
>
> org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
> at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2052)
> at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2485)
> at Controller.searchController.doPost(searchController.java:117)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
> at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
> at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
> at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
> at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> at java.lang.Thread.run(Thread.java:748)
>
> And below the line that is giving problem :
> subjList.add(rs.getString("subject_name"));
> newSub.addAll(subjList);
>
> System.out.println( "tutor ID=" + rs.getInt("tutor_id") + "zipcode =" + rs.getString("zipcode") +
> "subjectName=" + rs.getString("subject_name") + "tutorContact=" +
> rs.getString("tutor_contact_no"));
> }}
> I now inserted it before the close bracket of resultset.
>
> It points to System.out.print

>
Is "Controller.searchController.doPost(searchController.java:117)” your code?  Do you in fact call rs.next() appropriately?
Also unless there’s a subjList = new List<> (or subjList.clear()) somewhere nearby you may be adding the contents of subjList more than once.  There’s not enough of your code here to tell for sure exactly what’s going on.





On Sep 2, 2019, at 12:28 AM, Karen Goh <karenworld@yahoo.com> wrote:

String[] subjectNames = request.getParameterValues("txtsubjects");
String zipcode = request.getParameter("location");
List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
List<tutor> myList = new ArrayList<>();
ArrayList<String> subList = new ArrayList<>();
CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>(); 
tutor t = new tutor();


String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?))";

PreparedStatement ps2 = connection.prepareStatement(sql1);
ps2.setString(1, zipcode);
for (int i = 0; i < subjectNames.length; i++) {
Array array = connection.createArrayOf("text", subjList.toArray());
ps2.setArray(2, array);
}
ResultSet rs = ps2.executeQuery();
while (rs.next()) {
t.setContactNo(rs.getString("tutor_contact_no"));
t.setZipcode(rs.getString("zipcode"));
t.settutor_id(rs.getInt("tutor_id"));

   
    for (String s: subjList)
        if (s.contains(rs.getString("subject_name")))
          subjList.add(rs.getString("subject_name"));
      newSub.addAll(subjList);
}
myList.add(t);
request.setAttribute("tutors", myList);        
request.setAttribute("sub", newSub);

Please.  I hope someone can tell me what's wrong with the code or how to overcome this error...I have been struggling for 2 weeks already.

Thanks.
}
myList.add(t);
request.setAttribute("tutors", myList);        
request.setAttribute("sub", newSub);

I put your code into my editor as if it were an entire method.  That formatted version is below.
I find the for loop very odd. See comments with your code below, in bold for easy reference:

void func() {
// subjectNames set from the request
    String[] subjectNames = request.getParameterValues("txtsubjects");
    String zipcode = request.getParameter("location”);
// subjList built from subjectNames
    List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
    List<tutor> myList = new ArrayList<>();
    ArrayList<String> subList = new ArrayList<>();
    CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>(); 
    tutor t = new tutor();

    String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?))";
    PreparedStatement ps2 = connection.prepareStatement(sql1);
    ps2.setString(1, zipcode);
    for (int i = 0; i < subjectNames.length; i++) {
// subjList add to query
        Array array = connection.createArrayOf("text", subjList.toArray());
        ps2.setArray(2, array);
    }
    ResultSet rs = ps2.executeQuery();
    while (rs.next()) {
        t.setContactNo(rs.getString("tutor_contact_no"));
        t.setZipcode(rs.getString("zipcode"));
        t.settutor_id(rs.getInt("tutor_id"));

  
        for (String s: subjList)
// test if one entry of subjList matches the current row’s “subject_name” value, 
// repeatedly asking for “subject_name” from current rs
            if (s.contains(rs.getString("subject_name")))
// if so add current value to list being iterated
                subjList.add(rs.getString("subject_name"));
        newSub.addAll(subjList);
// If I follow the intent correctly (match subjects to tutor) I believe the following would be more effective
        String rsSubject = rs.getString("subject_name");
        if (subjList.contains(rsSubject)) {
            newSub.add(rsSubject);
        }
// However your sql seems to be asking for all tutors in a given zipcode and what subjects they handle.  You might want to at least order the return set by tutor to deal with one tutor over several result rows.  
Alternatively you could group subjects into an array per tutor:
String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, array_agg(t2.subject_name) FROM s_tutor t JOIN tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?)) group by t.tutor_contact_no,t.zipcode,t.tutor_id”;
and grab the array of tutor’s subjects.
Further your responce doesn’t tie specific tutor to specific subjects.  Perhaps you can add List<String>subjects to your “tutor” class?


    }
    myList.add(t);
    request.setAttribute("tutors", myList);       
    request.setAttribute("sub", newSub);
}

I hope this helps.
Hi Rob,

I found out that Tomcat is inconsistent in giving me the result and as mentioned, even though certain lines are commented out it is pointing to the commented out lines.

And even after clearing cache whatsoever, I am still getting the old error which is not supposed to be there.

Till I really iron out all Tomcat problem, I can't comment anything. But, as of now, the problem I posted here is dissolved.

Right now, it is giving me concurrentModificationList exception at (String s : subject) but this is Java problem and nothing to do with Postgresql...so that's the state of thing now.

Thanks for the reply thought....oh the list is part of the domain model.....as what you have to do to every Model construction...
On Friday, September 6, 2019, 3:24:42 PM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:




On Sep 2, 2019, at 12:28 AM, Karen Goh <karenworld@yahoo.com> wrote:

String[] subjectNames = request.getParameterValues("txtsubjects");
String zipcode = request.getParameter("location");
List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
List<tutor> myList = new ArrayList<>();
ArrayList<String> subList = new ArrayList<>();
CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>(); 
tutor t = new tutor();


String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?))";

PreparedStatement ps2 = connection.prepareStatement(sql1);
ps2.setString(1, zipcode);
for (int i = 0; i < subjectNames.length; i++) {
Array array = connection.createArrayOf("text", subjList.toArray());
ps2.setArray(2, array);
}
ResultSet rs = ps2.executeQuery();
while (rs.next()) {
t.setContactNo(rs.getString("tutor_contact_no"));
t.setZipcode(rs.getString("zipcode"));
t.settutor_id(rs.getInt("tutor_id"));

   
    for (String s: subjList)
        if (s.contains(rs.getString("subject_name")))
          subjList.add(rs.getString("subject_name"));
      newSub.addAll(subjList);
}
myList.add(t);
request.setAttribute("tutors", myList);        
request.setAttribute("sub", newSub);

Please.  I hope someone can tell me what's wrong with the code or how to overcome this error...I have been struggling for 2 weeks already.

Thanks.
}
myList.add(t);
request.setAttribute("tutors", myList);        
request.setAttribute("sub", newSub);

I put your code into my editor as if it were an entire method.  That formatted version is below.
I find the for loop very odd. See comments with your code below, in bold for easy reference:

void func() {
// subjectNames set from the request
    String[] subjectNames = request.getParameterValues("txtsubjects");
    String zipcode = request.getParameter("location”);
// subjList built from subjectNames
    List<String> subjList = new ArrayList<String>(Arrays.asList(subjectNames));
    List<tutor> myList = new ArrayList<>();
    ArrayList<String> subList = new ArrayList<>();
    CopyOnWriteArrayList<String>newSub = new CopyOnWriteArrayList<String>(); 
    tutor t = new tutor();

    String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, t2.subject_name FROM s_tutor t JOIN tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?))";
    PreparedStatement ps2 = connection.prepareStatement(sql1);
    ps2.setString(1, zipcode);
    for (int i = 0; i < subjectNames.length; i++) {
// subjList add to query
        Array array = connection.createArrayOf("text", subjList.toArray());
        ps2.setArray(2, array);
    }
    ResultSet rs = ps2.executeQuery();
    while (rs.next()) {
        t.setContactNo(rs.getString("tutor_contact_no"));
        t.setZipcode(rs.getString("zipcode"));
        t.settutor_id(rs.getInt("tutor_id"));

  
        for (String s: subjList)
// test if one entry of subjList matches the current row’s “subject_name” value, 
// repeatedly asking for “subject_name” from current rs
            if (s.contains(rs.getString("subject_name")))
// if so add current value to list being iterated
                subjList.add(rs.getString("subject_name"));
        newSub.addAll(subjList);
// If I follow the intent correctly (match subjects to tutor) I believe the following would be more effective
        String rsSubject = rs.getString("subject_name");
        if (subjList.contains(rsSubject)) {
            newSub.add(rsSubject);
        }
// However your sql seems to be asking for all tutors in a given zipcode and what subjects they handle.  You might want to at least order the return set by tutor to deal with one tutor over several result rows.  
Alternatively you could group subjects into an array per tutor:
String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, array_agg(t2.subject_name) FROM s_tutor t JOIN tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?)) group by t.tutor_contact_no,t.zipcode,t.tutor_id”;
and grab the array of tutor’s subjects.
Further your responce doesn’t tie specific tutor to specific subjects.  Perhaps you can add List<String>subjects to your “tutor” class?


    }
    myList.add(t);
    request.setAttribute("tutors", myList);       
    request.setAttribute("sub", newSub);
}

I hope this helps.


On Sep 6, 2019, at 9:21 PM, Karen Goh <karenworld@yahoo.com> wrote:

Hi Rob,

I found out that Tomcat is inconsistent in giving me the result and as mentioned, even though certain lines are commented out it is pointing to the commented out lines.

And even after clearing cache whatsoever, I am still getting the old error which is not supposed to be there.

Till I really iron out all Tomcat problem, I can't comment anything. But, as of now, the problem I posted here is dissolved.

Right now, it is giving me concurrentModificationList exception at (String s : subject) but this is Java problem and nothing to do with Postgresql...so that's the state of thing now.

Yes, that’s what I suspected in my note about “current list being iterated”.  I think you might be better off with something similar to my approach to reading the “subject_name” value only once for each row.

Does your query return multiple rows per tutor per execution as I suspect from reading your query?


Thanks for the reply thought....oh the list is part of the domain model.....as what you have to do to every Model construction...
   
        for (String s: subjList)
// test if one entry of subjList matches the current row’s “subject_name” value, 
// repeatedly asking for “subject_name” from current rs
            if (s.contains(rs.getString("subject_name")))
// if so add current value to list being iterated
                subjList.add(rs.getString("subject_name"));
        newSub.addAll(subjList);
// If I follow the intent correctly (match subjects to tutor) I believe the following would be more effective
        String rsSubject = rs.getString("subject_name");
        if (subjList.contains(rsSubject)) {
            newSub.add(rsSubject);
        }
// However your sql seems to be asking for all tutors in a given zipcode and what subjects they handle.  You might want to at least order the return set by tutor to deal with one tutor over several result rows.  
Alternatively you could group subjects into an array per tutor:
String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, array_agg(t2.subject_name) FROM s_tutor t JOIN tutor_subject t2  ON t.tutor_id = t2.tutor_id where t.zipcode = ?  And  t2.subject_name = Any((?)) group by t.tutor_contact_no,t.zipcode,t.tutor_id”;
and grab the array of tutor’s subjects.
Further your responce doesn’t tie specific tutor to specific subjects.  Perhaps you can add List<String>subjects to your “tutor” class?


    }
    myList.add(t);
    request.setAttribute("tutors", myList);       
    request.setAttribute("sub", newSub);
}

I hope this helps.