1

我创建了接受上传图像/头像的注册表单。但我得到了这个错误:[NullPointerException: null]

当我接受表格时。这是我的代码:

----Register.java----

public class Register extends Controller{

static Form<Users> form = form(Users.class);

  public static Result blank() {
    return ok(register.render(form));
  }

  public static Result submit() throws IOException {
    Form<Users> filledForm = form.bindFromRequest();

      MultipartFormData body = request().body().asMultipartFormData();
      FilePart picture = body.getFile("picture");
      if (picture != null) {
        String fileName = picture.getFilename();
        String contentType = picture.getContentType(); 
 File file = picture.getFile(); // ERROR MARKED IN THIS LINE : NullPointerException

        String orig = file.getAbsolutePath();
        String dest = "/home/thangnk/demo/public/upload/"+fileName;
      }
//Copy file into "demo/public/upload/"   folder     
        InputStream in = new FileInputStream(orig);
        OutputStream out = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
           out.write(buf, 0, len);
        }
        in.close();
        out.close();
      }
// Check accept conditions
      if(!"true".equals(filledForm.field("accept").value())) {
          filledForm.reject("accept", "You must accept the terms and conditions");
      }

// Check repeated password
      if(!filledForm.field("password").valueOr("").isEmpty()) {
          if(!filledForm.field("password").valueOr("").equals(filledForm.field("repeatPassword").value())) {
              filledForm.reject("repeatPassword", "Password don't match");
          }
      }

// Check if the username is valid
      if(!filledForm.hasErrors()){
          String name = filledForm.field("username").value();
          Finder<String, Users> find = new Finder<String, Users>(String.class, Users.class);
          List<Users> users = find.where().like("username", '%'+ name +'%').findList();

          if(!(users!=null && users.isEmpty())){
              filledForm.reject("username", "This username is already taken");
          }

      }

 if(filledForm.hasErrors()) {
            return badRequest(register.render(filledForm));
          }
      else{
            Users.create(filledForm.get());
            return ok(register_result.render());
          }
      }

----Users.java----

@Entity
public class Users extends Model{

    @Id
    public Long id;

    @Constraints.Required
    @NotNull
    public String username;

    @Constraints.Required
    @NotNull
    @Constraints.Email
    public String email;

    @Constraints.Required
    @Constraints.MinLength(6)
    @NotNull
    public String password;

    @Constraints.Required
    public String path;

    public Users(){}

    public Users(String username,String email,String password){
        this.username=username;
        this.email=email;
        this.password=password;
    }

      public static Finder<Long,Users> find = new Finder<Long,Users>(Long.class, Users.class); 


    public static List<Users> all() {
        return new ArrayList<Users>();
      }

    public static void create (Users user){
        user.save();
    }
}

注册.scala.html

 @helper.form(action = routes.Register.submit()) {
              <h3> Create Account Informations </h3>

       @inputText(
                signupForm("username"), 
                '_label -> "", 
                '_help -> "Please choose a valid username.",
                '_error -> signupForm.globalError,
                'placeholder ->"Username"
            )

       @inputText(
                signupForm("email"), 
                '_label -> "",
                '_help -> "Enter a valid email address.",
                '_error -> signupForm.error("email"),
                'placeholder ->"Email"
            )

       @inputPassword(
                signupForm("password"), 
                '_label -> "",
                '_help -> "A password must be at least 6 characters. ",
                '_error -> signupForm.error("password"),
                'placeholder ->"Password"
            )

       @inputPassword(
                signupForm("repeatPassword"), 
                '_label -> "",
                '_help -> "Please repeat your password again.",
                '_error -> signupForm.error("password"),
                'placeholder ->"Repeat password"
            )

       @inputFile(
                signupForm("picture"), 
                '_label -> "",
                '_help -> "Max size 120x130px",
                '_error -> signupForm.error("picture"),
                'placeholder ->"Upload file"
            )

       <a href="#"><h3>Terms and conditions</h3></a>

       @checkbox(
                signupForm("accept"), 
                '_label -> None, '_text -> "You aggree the Terms and conditions",
                '_showConstraints -> false
            )

        <div class="actions">
            <input type="submit" class="button" value="Register">
            <a href="@routes.Register.blank"><input class="button" type="button" value="Reset"></a>
            <a href="@routes.Application.index"><input class="button" type="button" value="Cancel"></a>
        </div>
}

帮我解决它。非常感谢!!!
P/s:如果我删除上传文件,它工作正常。

4

0 回答 0