Reputation: 123
Even though I have initialized the HashSet, I get a null pointer exception when I try to add an element to it.
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "artikal")
private Set<Cenovnik> cene = new HashSet<Cenovnik>();
public Set<Cenovnik> getCene() {
if (cene == null)
this.cene = new HashSet<Cenovnik>();
return cene;
}
and I use it like this:
Cenovnik cenovnik = new Cenovnik(date, null, cena);
Artikal artikal = artikalDao.findById(id);
Cenovnik stari = artikalDao.cena(id);
if (stari != null) {
stari.setKrajkVazenja(date);
cenovnikDao.merge(stari);
}
cenovnik.setArtikal(artikal);
artikal.getCene().add(cenovnik); //error!
cenovnikDao.persist(cenovnik);
artikalDao.merge(artikal);
So, the error occurs when I try to add cenovnik to artikal.getCene, I have already checked, for some reason getCene seems to be the problem. Here is the Cenovnik class
public class Cenovnik implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1436274372349853081L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "cenovnik_id", unique = true, nullable = false)
private Integer id;
...
@ManyToOne
@JoinColumn(name="artikal_id", referencedColumnName="artikal_id")
private Artikal artikal;
public Artikal getArtikal() {
return artikal;
}
public void setArtikal(Artikal artikal) {
this.artikal = artikal;
}
...
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
} Here is the full stack trace:
java.lang.NullPointerException:
rs.ac.uns.acs.ism.server.servlet.DodajCenuArtiklaController.doGet(DodajCenuArtiklaController.java:78)
rs.ac.uns.acs.ism.server.servlet.DodajCenuArtiklaController.doPost(DodajCenuArtiklaController.java:109)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
rs.ac.uns.acs.ism.server.servlet.DodajArtikalController.doGet(DodajArtikalController.java:72)
rs.ac.uns.acs.ism.server.servlet.DodajArtikalController.doPost(DodajArtikalController.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:745)
and here is equals method:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cenovnik other = (Cenovnik) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
Upvotes: 2
Views: 5927
Reputation: 1869
if artikal is null that is a separate case. then do a null check like this
if(artikal != null) {
artikal.getCene().add(cenovnik);
}
Try this to debug break up the getCene() and add into two line and add some print statements of if you are using some logger use that. This will help know what's going on
if(artikal != null) {
System.out.println("artikal is not null");
Set<Cenovnik> cenovniks= artikal.getCene();
if(cenovniks != null) {
System.out.println("cenovniks set is not null");
cenovniks.add(cenovnik);
System.out.println("Added cenovnik to set");
} else {
// Handle null cenovniks as appropriate to your application
System.out.println("cenovniks set is null");
}
} else {
System.out.println("artikal is null");
// Handle null artikal as appropriate to your application
}
Otherwise
PLEAS NOTE this applies only to add() method of hashset when adding to hashset
One reason adding a object to a set can throw a null pointer exception is:
One of the properties of the object that is being added to hashset is used in calculation of hashcode of the object. And during calculation hashcode this property is throwing a null pointer exception.
It can be date or some other property. Check hashcode implementation of the class put a break point in hashcode method to see if that throwing this nullpointer.
Update 1 Looking at the hascode and equals methods seems like they are not causing the problem.
Upvotes: 1
Reputation: 44854
You are getting a record from a datasource and then using it without checking to see if it is null.
try
Artikal artikal = artikalDao.findById(id);
if (artikal != null) {
artikal.getCene().add(cenovnik);
} else
{
// throw something
}
Upvotes: 0