Why do I get a “Null value was assigned to a property of primitive type setter of” error message when using HibernateCriteriaBuilder in Grails

后端 未结 12 670
长情又很酷
长情又很酷 2021-01-30 07:58

I get the following error when using a primitive attribute in my grails domain object:

Null value was assigned to a property of primitive type setter of MyDomain         


        
相关标签:
12条回答
  • 2021-01-30 08:17

    According to this SO thread, the solution is to use the non-primitive wrapper types; e.g., Integer instead of int.

    0 讨论(0)
  • 2021-01-30 08:18

    A null value cannot be assigned to a primitive type, like int, long, boolean, etc. If the database column that corresponds to the field in your object can be null, then your field should be a wrapper class, like Integer, Long, Boolean, etc.

    The danger is that your code will run fine if there are no nulls in the DB, but will fail once nulls are inserted.

    And you can always return the primitive type from the getter. Ex:

      private Integer num;
    
      public void setNum(Integer i) {
        this.num = i;
      }
    
      public int getNum() {
        return this.num;
      }
    

    But in most cases you will want to return the wrapper class.

    So either set your DB column to not allow nulls, or use a wrapper class.

    0 讨论(0)
  • 2021-01-30 08:20

    I'll try to make you understand with the help of an example. Suppose you had a relational table (STUDENT) with two columns and ID(int) and NAME(String). Now as ORM you would've made an entity class somewhat like as follows:-

    package com.kashyap.default;
    
    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    /**
     * @author vaibhav.kashyap
     *
     */
    @Entity
    @Table(name = "STUDENT")
    public class Student implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = -1354919370115428781L;
    
        @Id
        @Column(name = "ID")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
    
        @Column(name = "NAME")
        private String name;
    
        public Student(){
    
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    Lets assume table already had entries. Now if somebody asks you add another column of "AGE" (int)

    ALTER TABLE STUDENT ADD AGE int NULL

    You'll have to set default values as NULL to add another column in a pre-filled table. This makes you add another field in the class. Now the question arises whether you'll be using a primitive data type or non primitive wrapper data type for declaring the field.

    @Column(name = "AGE")
    private int age;
    

    or

    @Column(name = "AGE")
    private INTEGER age;
    

    you'll have to declare the field as non primitive wrapper data type because the container will try to map the table with the entity. Hence it wouldn't able to map NULL values (default) if you won't declare field as wrapper & would eventually throw "Null value was assigned to a property of primitive type setter" Exception.

    0 讨论(0)
  • 2021-01-30 08:21

    Change the parameter type from primitive to Object and put a null check in the setter. See example below

    public void setPhoneNumber(Long phoneNumber) {
        if (phoneNumber != null)
            this.phoneNumber = phoneNumber;
        else
            this.extension = 0l;
    }
    
    0 讨论(0)
  • 2021-01-30 08:23

    There are two way

    • Make sure that db column is not allowed null
    • User Wrapper classes for the primitive type variable like private int var; can be initialized as private Integer var;
    0 讨论(0)
  • 2021-01-30 08:32
    @Column(name ="LEAD_ID")
    private int leadId; 
    

    Change to

    @Column(name ="LEAD_ID")
    private Integer leadId; 
    
    0 讨论(0)
提交回复
热议问题