java.sql.SQLException: Before start of result set

I don't understand why this isn't working, I keep getting this java.sql.SQLException: Before start of result set error, ive looked online but because ive been staring at a computer screen for hours my brain is frazzling so I'm not able to find any soloutions. please can you help me?! thanks

String from = "";
String subject= "";
String to= "";
String message= "";
try
{
    while(retrieveMessages().next())
    {
         to = retrieveMessages().getString("tblEmail.to");
         from = retrieveMessages().getString("tblEmail.from");
         subject = retrieveMessages().getString("tblEmail.subject");
         message = retrieveMessages().getString("tblEmail.message");
         MailBox.add(new Email(to, from, subject, message));
    }
}
catch (SQLException e)
{

    e.printStackTrace();
}
Jon Skeet
people
quotationmark

You're calling retrieveMessages() multiple times. It doesn't help that you haven't shown us what that does, but I suspect you want to call it once and store the result in a local variable. At the moment, I suspect you're calling next() on one result set, then getString() on a different result set. For example:

try (ResultSet results = retrieveMessages()) {
    while (results.next()) {
        String to = results.getString("tblEmail.to");
        String from = results.getString("tblEmail.from");
        String subject = results.getString("tblEmail.subject");
        String message = results.getString("tblEmail.message");
        MailBox.add(new Email(to, from, subject, message));
    }
}

people

See more on this question at Stackoverflow