0

I am having trouble working out why my foundUser object comes back as null when the correct parameter is being passed to the findByUserName method

Here is where the method is called

public static ObjectNode getAllConnections(String userName){

    List<Connections> connectionsUsernames = find.where().eq("username", userName).findList();

    Iterator<Connections> connectionsIterator = connectionsUsernames.iterator();

    ObjectNode response = Json.newObject();
    ObjectNode responseItem = Json.newObject();

    while (connectionsIterator.hasNext()){
        Connections connection = connectionsIterator.next();

        User foundUser = User.findByUserName(connection.connectionUserName);
        responseItem.put("jobtitle", foundUser.jobTitle);

        responseItem.put("connectionid", connection.connectionId.toString());
        responseItem.put("connectionusername", connection.connectionUserName);
        responseItem.put("connectiondate", connection.connectionDate);

        response.put(connection.connectionUserName, responseItem);

    }
    return response;
}

It is failing on the line User foundUser = User.findByUserName(connection.connectionUserName);

What I am trying to do is get the details from the database of the user who's userName is stored in the connection.ConnectionUserName variable but the foundUser variable is null even when I can see in debug that a correct String is being passed to the method

Here is the findByUserName method in the User class

public static User findByUserName(String userName) {
    return find.where().eq("username", userName).findUnique();
}

Any help would be appreciated, I think I just need a different set of eyes to look over this and see an obvious error that I'm missing!

Thanks in advance!!

adamtrousdale
  • 415
  • 6
  • 18

1 Answers1

0

You are the only person who can help you in this case I think.

  • first enable statement logging to the console by adding lines to conf

    db.default.logStatements=true
    logger.com.jolbox=DEBUG
    

    so you can just copy the invalid SQL statement and then try to perform it in your favorite DB gui

  • Use debugger to break at this place and check the resolved values, maybe problem is not in users' finder?

  • BTW you could iterate with for loop it would be simpler code.

  • Finally, you are looking connections by username but want to get it as connectionUserName why? If you really has two separate fields, make sure that they always have identical values if you want to mess things like that. use your DB gui and check directly in database if they are.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182
  • Thanks for letting me know about the SQL logging, that will be useful in future. Turned out the problem was a tired programmer, the username I was passing into the findByUserName method ended in .com not .co.uk like the username in the database. Stupidity on my part! Your suggestions helped me get there so thanks for taking the time to answer! – adamtrousdale Feb 13 '14 at 17:10