Getting a RuntimeException : Datasource user is null ? when updating User model in Play Framework 2.2.1

时光怂恿深爱的人放手 提交于 2019-12-13 11:11:47

问题


I am currently trying to enhance the To-Do List tutorial from Play framework's website. I've added a login form, (and of course a User class acting as a model), and it works perfectly well. I also made a small "dashboard" in which the logged user is able to change his password and his email address. But when I submit the form, I get a "Datasource user is null ?" error (RuntimeException).

The whole problem came when I wanted to restrict the edition possibilities (I first used a whole User form, which is quite over the top (User do not need to edit their ID). So I made a small inner class in my Application file called UpdateUser which gathers the required informations, just as I did for the login system.

Searching this error gave me many results but people saw their problem fixed by uncommenting the ebean.default line in the conf file, which I already did.

Here is the method I used to update user's informations :

Firstly, I made a small class in my Application to hold the form (exactly like I did for the login thing). Then I made a update function as found here in my user class :

public static String update(String id, User newuser) {
    newuser.update(id);
    return("Your profile has been updated");    
}

which returns the String that will be in my flash and which is according to my compiler the problem function.

This function is called in my Application like this :

public static Result updateUser(String id)
{
    Form<UpdateUser> filledForm = updateUserForm.bindFromRequest();
    System.out.println("Updated User : "+filledForm.get().id);
    if(filledForm.hasErrors())
        {
            flash("success","Error while updating");
    }else{
        User user = new User(filledForm.get().id, filledForm.get().email, User.find.byId(filledForm.get().id).name, User.find.byId(filledForm.get().id).surname, filledForm.get().password);
        flash("success", User.update(id,user));
    }
    return redirect(routes.Application.dashboard());
}

I tracked the data in the Form and it is not null (I mean I can get everything from the form). But I wonder if I have to create another ebean or if it's my function which is wrong. I also wonder if it's not my User creation that fail. Or maybe I should take the updateUser function and put it in my inner UpdateUser class ?

I have to admit that I worked on that all of yesterday (and probably today too), and I can't find anything on the internet except the ebean.default thing.

------EDIT

I continued to search, so here's what I tried :

1) Getting the form result into an instance of UpdateUser in order to use it
2) Use this instance instead of getting the data from the form

But it failed too. What's really weird is that I've added a toString() method for User class, and calling it on the user I want to insert (as an update) gives me the full stuff. I think it must be a configuration problem, but I can't see it.

Another thing : when I come to the error page and when I try to come back to the application by modifying the URL, I am disconnected. Is it my ebean that closes himself ?

Last edit for the day, I'm getting tired of this. I tried to delay the action (i.e. making it happen after the user has logged out), the new data are correctly saved but I still get the error when calling the update function.


回答1:


Alright, I finally found it, but totally by chance.

I just had to write this :

public static String update(String id, User user) {
    user.update((Object)id);
    return ("Your profile has been updated");
}

Because for some reason that I don't really understand, The id String needs to be cast to Object. The rest of the code was correct. But apparently, when using update() on this particular case (is it because my id is a String or because I get the informations from another class before using it as my Model), the parameter which is supposed to be a String (even in the documentation) HAS to be cast.

That's it.



来源:https://stackoverflow.com/questions/21681680/getting-a-runtimeexception-datasource-user-is-null-when-updating-user-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!