简体   繁体   中英

Play! Framework, created object is null even after passing correct value into method

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!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM