0

I built my thymeleaf template to display some values from database in specific fields . I had two classes "Iden" and "Target" joined , each "iden" had many "target". I m trying to display those values interconnecting the two classes i got an error in my thymeleaf template ("Property or field 'ref' cannot be found on null"). Any one could help to find what happens.

<td th:text="${p.iden.ref}"></td> 

this is the code of iden class :

package com.example.dot.entities; import java.io.Serializable; import java.util.Date; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; @Entity @Table(name = "iden") @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id_iden") public class Iden implements Serializable {	/** * */	private static final long serialVersionUID = 1L;	@Id	@GeneratedValue	private Long id_iden;	@DateTimeFormat(pattern = "yyyy-MM-dd")	@Column(name = "dte_deb_inter", columnDefinition="DATE")	private Date dte_deb_inter;	private String cse_deb_inter;	private String ref ;	private String dir_gen;	private String dir_chg;	@OneToMany(mappedBy = "iden", fetch = FetchType.LAZY)	private List<Target> targets;	@ManyToOne	@JoinColumn(name = "CODE_ORD")	public Ordre ordre;	public List<Target> getTargets() {	return targets;	}	public void setTargets(List<Target> targets) {	this.targets = targets;	}	public Iden() {	super();	// TODO Auto-generated constructor stub	}	public Iden(Date dte_deb_inter, String cse_deb_inter,	String ref,String dir_gen,String dir_chg ,Ordre ordre) {	super();	this.dte_deb_inter = dte_deb_inter;	this.cse_deb_inter = cse_deb_inter;	this.dir_chg =dir_chg;	this.dir_gen =dir_gen;	this.ref =ref;	this.ordre = ordre;	}	public Long getId_iden() {	return id_iden;	}	public void setId_iden(Long id_iden) {	this.id_iden = id_iden;	}	public Date getDte_deb_inter() {	return dte_deb_inter;	}	public void setDte_deb_inter(Date dte_deb_inter) {	this.dte_deb_inter = dte_deb_inter;	}	public String getCse_deb_inter() {	return cse_deb_inter;	}	public void setCse_deb_inter(String cse_deb_inter) {	this.cse_deb_inter = cse_deb_inter;	}	public Ordre getOrdre() {	return ordre;	}	public void setOrdre(Ordre ordre) {	this.ordre = ordre;	}	public String getRef() {	return ref;	}	public void setRef(String ref) {	this.ref = ref;	}	public String getDir_gen() {	return dir_gen;	}	public void setDir_gen(String dir_gen) {	this.dir_gen = dir_gen;	}	public String getDir_chg() {	return dir_chg;	}	public void setDir_chg(String dir_chg) {	this.dir_chg = dir_chg;	} }

this is the code of target class :

package com.example.dot.entities; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; @Entity @Table(name = "target") @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id_trg") public class Target implements Serializable{	/** * */	private static final long serialVersionUID = 1L;	@Id	@GeneratedValue	private Long id_trg;	@DateTimeFormat(pattern = "yyyy-MM-dd")	@Column(name = "dte_fin_inter", columnDefinition="DATE")	private Date dte_fin_inter; private String cse_fin_inter;	private String acti ;	private Integer flag_status;	private String name ;	private String type ;	private Long number ;	private String st;	private String ope;	private String por ;	@ManyToOne	@JoinColumn(name = "CODE_IDEN")	public Iden iden;	public Target() {	super();	// TODO Auto-generated constructor stub	}	public Target(Date dte_fin_inter, String cse_fin_inter, String acti, Integer flag_status, String name, String type,	Long number, String st, String por,String ope, Iden iden) {	super();	this.ope = ope;	this.dte_fin_inter = dte_fin_inter;	this.cse_fin_inter = cse_fin_inter;	this.acti = acti;	this.flag_status = flag_status;	this.name = name;	this.type = type;	this.number = number;	this.st = st;	this.por = por;	this.iden = iden;	}	public Long getId_trg() {	return id_trg;	}	public void setId_trg(Long id_trg) {	this.id_trg = id_trg;	}	public Date getDte_fin_inter() {	return dte_fin_inter;	}	public void setDte_fin_inter(Date dte_fin_inter) {	this.dte_fin_inter = dte_fin_inter;	}	public String getCse_fin_inter() {	return cse_fin_inter;	}	public void setCse_fin_inter(String cse_fin_inter) {	this.cse_fin_inter = cse_fin_inter;	}	public String getActi() {	return acti;	}	public void setActi(String acti) {	this.acti = acti;	}	public Integer getFlag_status() {	return flag_status;	}	public void setFlag_status(Integer flag_status) {	this.flag_status = flag_status;	}	public String getName() {	return name;	}	public void setName(String name) {	this.name = name;	}	public String getType() {	return type;	}	public void setType(String type) {	this.type = type;	}	public Long getNumber() {	return number;	}	public void setNumber(Long number) {	this.number = number;	}	public String getSt() {	return st;	}	public void setSt(String st) {	this.st = st;	}	public String getpor() {	return por;	}	public void setpor(String por) {	this.por = por;	}	public Iden getIden() {	return iden;	}	public void setIden(Iden iden) {	this.iden = iden;	}	public String getOpe() {	return ope;	}	public void setOpe(String ope) {	this.ope = ope;	} }

this is the controller code to get the list of targets :

@Controller public class TargetController {	@Autowired Targetrepository trgrepo;	@RequestMapping("/alltarget")	public String formall(Model model) {	List<Target> listTarget =trgrepo.findAll();	model.addAttribute("list",listTarget);	return "allIdentifiant";	} }

and Finally the thymeleaf template :

<tbody>	<tr th:each="p:${list}">	<td th:text="${p.cse_fin_inter}"></td>	<td><span th:text="${p.iden.ref}" ></span>	</td>	<td th:text="${p.acti}"></td>	<td><span th:text="${p.por}" ></span></td>	<td><span th:text="${p.ope}"></span></td>	<td th:text="${#dates.format(p.dte_fin_inter, 'yyyy-MM-dd')}"></td>	<td><span th:text="${p.st}"></span></td>	<td><span th:text="${p.number}"></span></td>	<td><span th:text="${p.name}"></span></td>	<td>	</td>	</tbody>

1 Answer 1

1

The error is pretty self explainatory in this case. Property or field 'X' cannot be found on null means thymeleaf tried to call a getter method on a null object. In your case, p.iden.ref, it means iden was null.

The possible solutions you've are:

  • Make sure in the controller that your iden attribute is not null in any of your targets.
  • Check that you won't try to access iden if it is null, in the following way:

    <span th:if="${p.iden != null}" th:text="${p.iden.ref}" ></span> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.