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

GrailsGrails Orm

Grails Problem Overview


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.myAttribute
 org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of MyDomain.myAttribute
at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1077)

Grails Solutions


Solution 1 - Grails

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

Solution 2 - Grails

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.

Solution 3 - Grails

A primitive type cannot be null. So the solution is replace primitive type with primitive wrapper class in your tableName.java file. Such as:

@Column(nullable=true, name="client_os_id")
private Integer client_os_id;

public int getClient_os_id() {
	return client_os_id;
}

public void setClient_os_id(int clientOsId) {
	client_os_id = clientOsId;
}

reference http://en.wikipedia.org/wiki/Primitive_wrapper_class to find wrapper class of a primivite type.

Solution 4 - Grails

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.

Solution 5 - Grails

use Integer as the type and provide setter/getter accordingly..

private Integer num;

public Integer getNum()...

public void setNum(Integer num)...

Solution 6 - Grails

@Column(name ="LEAD_ID")
private int leadId; 

Change to

@Column(name ="LEAD_ID")
private Integer leadId; 

Solution 7 - Grails

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;

Solution 8 - Grails

Do not use primitives in your Entity classes, use instead their respective wrappers. That will fix this problem.

Out of your Entity classes you can use the != null validation for the rest of your code flow.

Solution 9 - Grails

Either fully avoid null in DB via NOT NULL and in Hibernate entity via @Column(nullable = false) accordingly or use Long wrapper instead of you long primitives.

A primitive is not an Object, therefore u can't assign null to it.

Solution 10 - Grails

@Dinh Nhat, your setter method looks wrong because you put a primitive type there again and it should be:

public void setClient_os_id(Integer clientOsId) {
client_os_id = clientOsId;
}

Solution 11 - Grails

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;
}

Solution 12 - Grails

Make sure your database myAttribute field contains null instead of zero.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterView Question on Stackoverflow
Solution 1 - GrailsPeterView Answer on Stackoverflow
Solution 2 - GrailsMattCView Answer on Stackoverflow
Solution 3 - GrailsNhat DinhView Answer on Stackoverflow
Solution 4 - GrailscodechefvaibhavkashyapView Answer on Stackoverflow
Solution 5 - Grailsarn-arnView Answer on Stackoverflow
Solution 6 - GrailsRakesh YadavView Answer on Stackoverflow
Solution 7 - Grails4302836View Answer on Stackoverflow
Solution 8 - GrailsIsraelmView Answer on Stackoverflow
Solution 9 - Grailsam0waView Answer on Stackoverflow
Solution 10 - GrailsKamiel AhmadpourView Answer on Stackoverflow
Solution 11 - GrailsSandeep Shankar HarikrishnanView Answer on Stackoverflow
Solution 12 - GrailsAravinthan KView Answer on Stackoverflow