Edgaras Karka Edgaras Karka - 9 months ago 37
Java Question

AngularJS application can`t get List from Server (Java Spring)

I try to load

to my front-end application form server. On Server I have Spring application and I use hibernate to load picks objects from database

Spring controller:

@RequestMapping(value = "/web2/getActivePicks", method = RequestMethod.POST)
public @ResponseBody List<Pick> getActivePicks() {
PickDAO pd = new PickDAO();
List<Pick> picks = pd.getPicksFrom(new Date());
return picks;

When I try to get this picks list I get `

HTTP Status 500 - Could not write JSON:
(was java.lang.NullPointerException)
(through reference chain: java.util.ArrayList[0]-&gt;com.gepick.app.objects.rmo.Pick[&quot;match&quot;]-&gt;com.gepick.app.objects.rmo.soccer.Match_soccer[&quot;ar&quot;]);
nested exception is org.codehaus.jackson.map.JsonMappingException: (was java.lang.NullPointerException)
(through reference chain: java.util.ArrayList[0]-&gt;com.gepick.app.objects.rmo.Pick[&quot;match&quot;]-&gt;com.gepick.app.objects.rmo.soccer.Match_soccer[&quot;ar&quot;])

My pick class:

public class Pick {
private int pid;

private Match_soccer match;

private Algo1 algo;

@Column(length = 100)
private String pick;

@Column(length = 5)
private double plimit;

I change controller to:

@RequestMapping(value = "/web2/getActivePicks", method = RequestMethod.POST)
public @ResponseBody JSONArray getActivePicks() {
PickDAO pd = new PickDAO();
JSONArray picks = new JSONArray(pd.getPicksFrom(new Date()));
return picks;

But now i get http status 406 and messgae:

The resource identified by this request is only capable of generating responses
with characteristics not acceptable according to the request "accept" headers


It seems that when Jackson, your JSON mapper is trying to convert a Pick instance, the Match_soccer is null and that throws an exception.

You could either see that your Match_soccer is initialized before the mapping or you could annotate your Pick class with this @JsonInclude(Include.NON_NULL).

This will instruct the mapper to ignore all the null fields from the mapping