java.lang.IllegalArgumentException: number of arguments must be even at org.javalite.activejdbc.Model.set

一曲冷凌霜 提交于 2019-12-12 03:40:01

问题


The request body:

request.body {"username":"kkk.k999@gmail.com","userImage":"https://lh3.googleusercontent.com/-XdUIqdMkffA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50","role":"ROLE_USER","token":"ya29.GmIYBNhh5zs2Cpq1UI2iVzdxDvMVf2x8ggpEgM9Jvk51f5FOGodUZINrabY6K9Mhn6L82XpUhOyh5uIPhLZkAjIqS1hBu7un9QhMzRW35
RJM5ZwFozlBIuuxFRP4Y5xsTtdPGw"}

Error while Saving to Db:

@Override
public void saveUserInfo(Request request) {
    Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/47seconds", "root", "admin");
    ObjectMapper mapper = new ObjectMapper();
    try {
        UserInfo userInfo = mapper.readValue(request.body(), UserInfo.class);

        userInfo.set(userInfo.getUsername());
        userInfo.set(userInfo.getUserImage());
        userInfo.set(userInfo.getRole());
        userInfo.set(userInfo.getToken());

        userInfo.saveIt();

    } catch (IOException ex) {
        Logger.getLogger(UserServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    Base.close();
}

Error: java.lang.IllegalArgumentException: number of arguments must be even at org.javalite.activejdbc.Model.set

The number of arguments match in the model class and in the DB. The id inside database is auto incremented so I am not passing it from the code.

Model class:

@Table("user_info")
public class UserInfo extends Model {

    private String username;
    private String userImage;
    private String token;
    private String role;

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the userImage
     */
    public String getUserImage() {
        return userImage;
    }

    /**
     * @param userImage the userImage to set
     */
    public void setUserImage(String userImage) {
        this.userImage = userImage;
    }

    /**
     * @return the token
     */
    public String getToken() {
        return token;
    }

    /**
     * @param token the token to set
     */
    public void setToken(String token) {
        this.token = token;
    }

    /**
     * @return the role
     */
    public String getRole() {
        System.out.println("printing role "+ role);
        return role;
    }

    /**
     * @param role the role to set
     */
    public void setRole(String role) {
        this.role = role;
    }

}

回答1:


Your use of ActiveJDBC APIs is not correct.

  1. Do not need annotation @Table, since your model name corresponds to a table name
  2. Do not need to declare properties
  3. You call the wrong method set
  4. You have spaghetti code userInfo.set(userInfo.getUsername()); - you are setting data from an object to the same object?????

Your model needs to be (in its entirety):

public class UserInfo extends Model {}

this is all you need.

If you need to set values on a model, you can:

userInfo.set("user_name", userName); 

etc.

I suggest you read the documentation and try to resolve the issues yourself, as your question displays a lack of effort on your part.



来源:https://stackoverflow.com/questions/43009622/java-lang-illegalargumentexception-number-of-arguments-must-be-even-at-org-java

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