skip to Main Content

I’m working in creating a java web app using play framework(2.8.19). While creating registration page I’m trying to pass the inputs from the registration page(scala.html) to the controller class where it saves the input to the Postgres table. Here I’m facing null pointer exception while accessing the input fields from the controller class. Below is code for registration page and controller class and my routes file.

Routes.file

`POST /users/ controllers.Users.save(request: Request)`

registraion.scala.html

@(userFrom: Form[commons.Forms.User])
@()(implicit request: Http.Request)
<!--@(userFrom: Form[commons.Forms.User]) (implicit request: RequestHeader, messagesProvider: play.i18n.MessagesProvider)-->
@main("User form") {
    <h1>User form</h1>
    @helper.form(action = routes.Users.save()) {

    <form>
    <!-- <legend>User (@userFrom("name").value)</legend>-->
    <!-- <legend>User (userFrom("email"))</legend>-->
    <div class="form-group">
    <label for="name">Email address</label>
    <input type="text" class="form-control" id="name" name="name" placeholder="Name">
    </div>
    <div class="form-group">
    <label for="email">Email address</label>
    <input type="email" class="form-control" id="email" name="email" placeholder="Email">
    </div>
    <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Password">
    </div>
    <input type="submit" class="btn btn-primary" value="Save">
    <a class="btn" href="@routes.Authentication.login()">Cancel</a>
    </form>

    }
}

Users.java(Controller)

public class Users extends Controller {
    private IUserDao iuserDao;
    private final Form<Forms.User> formFactory;
    private final play.i18n.MessagesApi messagesApi;

    @Inject
    public Users(FormFactory formFactory, MessagesApi messagesApi) {
        this.formFactory = formFactory.form(Forms.User.class);
        this.messagesApi = messagesApi;
    }

    public Result save(Http.Request request) {
        Messages messages = this.messagesApi.preferred(request);
        System.out.println("calling here");
        System.out.println(request.getHeaders());
        Form<Forms.User> userForm = formFactory.bindFromRequest(request);

        if (userForm.hasErrors()) {
            return badRequest(registration.render(userForm));
        }

        Forms.User user = userForm.get(); 
        System.out.println(user.email); //** <====== Facing error in this line =====>**
        models.AppUser searchResult = (AppUser) iuserDao.findByEmail(user.email);
        if (searchResult != null) {
            return badRequest(registration.render(userForm));
        }

        iuserDao.create(user);
        return redirect(routes.Authentication.login());
    }
}

Here the Form User class is

public class Forms {

    public static class User {
        public String name;
        public String email;
        public String password;
    }
}

Snapshot of error is here.

ERROR

2

Answers


  1. The error message indicates a NullPointerException at the line where System.out.println(user.email) is called in the save method of the Users class.

    The issue seems to be that the userForm.get() method is returning null, which leads to the NullPointerException when trying to access the email property of the user object.

    To debug this issue, you can check the following:

    1. Ensure that the form fields in the registration view (registraion.scala.html) have the correct name attributes corresponding to the properties in the Forms.User class. In your case, the form fields should have name="name", name="email", and name="password".

    2. Check if the form data is properly sent in the request. You can print the request headers in the save method to verify that the form data is present.

      System.out.println(request.getHeaders());

    3. Verify that the form binding (formFactory.bindFromRequest(request)) is successful and the userForm object is populated correctly. You can print the form data to check if it contains the expected values.

      System.out.println(userForm.data());

    If the form data is not being properly sent or the form binding is not successful, it can result in a null value for the userForm. Make sure that the form fields are correctly named and that the form data is sent as expected.

    Additionally, it’s a good practice to handle error cases gracefully. Instead of directly accessing properties like user.email, consider using userForm.get("email") to retrieve the form value and handle the case when the value is null or empty.

    By investigating these points, you should be able to identify the cause of the NullPointerException and resolve the issue.

    Hope that helps.

    Login or Signup to reply.
  2. There’s a critical error here which I noticed, and that’s probably what is causing the issue. You are creating a nested form. The line

    @helper.form(action = routes.Users.save()
    

    creates a form tag, and you have again added a form tag right below it. Try removing the inner (explicit) <form> tag and matching </form>. It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search