英文:
How to solve NULL pointer error when using Jackson 1.x
问题
以下是您要求的翻译部分:
我使用一个API来请求数据。对于一个请求,当我尝试将值设置为int时,我收到一个空指针异常。
public class Player implements Serializable {
// ...
// ...
@JsonProperty("offsides")
private Integer offsides;
@JsonProperty("offsides")
public Integer getOffsides() {
return offsides;
}
@JsonProperty("offsides")
public void setOffsides(Integer offsides) {
this.offsides = offsides;
}
// ...
}
public class Fixtures {
private final static long serialVersionUID = 2571183788260356219L;
public static void main(String args[]) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// ...
FixtureJSONMapp fixturesFixtures = mapper.readValue(fixturesFile, FixtureJSONMapp.class);
List<String> insertStatus = FixturesFixturesInsert.insertAllFixtures(fixturesFixtures,
statsNumb, playersNumb, eventsNumb, lineupsNumb);
}
}
public class FixturesFixturesInsert {
public static List<String> insertAllFixtures(FixtureJSONMapp fixturesFixtures, int statsNumb, int playersNumb, int eventsNumb, int lineupsNumb) throws Exception {
try {
Connection con = dbConnector.ConnectToDB();
PreparedStatement pstmtFixtures = con.prepareStatement(".....");
for (int i = 0; i < fixturesFixtures.getApi().getFixtures().size(); i++) {
// ...
int offsides = fixturesFixtures.getApi().getFixtures().get(i).getPlayers().get(j).getOffsides();
// ...
}
}
// ...
}
}
对于"int offsides",在这种情况下,我得到了一个空指针异常。我可以通过以下方式解决:
int offsides = (fixtures....getOffsides() != null ? getOffsides() : 0);
但我认为我应该能够使用mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
或类似的方法,但我无法使其工作。我希望能够在更多地方设置这个设置,因为我将不得不在许多地方这样做。
我想在收到的JSON字符串中,如果值为NULL,将其设置为0。这种做法是否可行?正如主题中提到的,我使用Jackson 1.x(org.codehaus.jackson)。
英文:
I use an API from where I request data. For one request I receive a NULL pointer exception when I try to set the value to an int.
public class Player implements Serializable {
...
...
...
...
@JsonProperty("offsides")
private Integer offsides;
...
...
@JsonProperty("offsides")
public Integer getOffsides() {
return offsides;
}
@JsonProperty("offsides")
public void setOffsides(Integer offsides) {
this.offsides = offsides;
}
...
public class Fixtures {
private final static long serialVersionUID = 2571183788260356219L;
public static void main(String args[]) throws Exception {
ObjectMapper mapper = new ObjectMapper();
...
...
...
FixtureJSONMapp fixturesFixtures = mapper.readValue(fixturesFile, FixtureJSONMapp.class);
List<String> insertStatus = FixturesFixturesInsert.insertAllFixtures(fixturesFixtures,
statsNumb, playersNumb, eventsNumb, lineupsNumb);
}
}
...
public class FixturesFixturesInsert {
public static List<String> insertAllFixtures(FixtureJSONMapp fixturesFixtures, int statsNumb, int playersNumb, int eventsNumb, int lineupsNumb) throws Exception {
try {
Connection con = dbConnector.ConnectToDB();
PreparedStatement pstmtFixtures = con.prepareStatement(".....");
for(int i = 0; i < fixturesFixtures.getApi().getFixtures().size(); i++) {
...
...
...
...
int offsides = fixturesFixtures.getApi().getFixtures().get(i).getPlayers().get(j).getOffsides();
...
}
}
It is for the "int offsides" I get an NULL pointer exception in this case. I can solve by
int offsides = (fixtures....getOffsides() != null ? getOffsides() : 0);
But I thought I should be able to use mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL); or something similar but I can´t get it to work. I would like to set this setting more global because I will have to do it in many places.
I would like to set value to 0 if it is NULL in the JSON String i receive. Is this possible to do?
As mentioned in the subject I use Jackson 1.x (org.codehaus.jackson)
答案1
得分: 1
首先确保 setSerializationInclusion(...)
应用于传入数据。我不这么认为,它对即将发送的数据生效。
注意:
- 即使它生效了,你正试图访问
null
并将其赋值给一个基本类型。 NullPointerException
表明你在一个null
对象上调用了getOffsides()
,即null.getOffsides()
- 如果你至少在使用 Java 8,你可以使用
Optional.ofNullable
在其为null
时将int offsides
设置为0
。
英文:
First of make sure that setSerializationInclusion(...)
is applied on an incoming data. I don't think so, it takes effect for data you are about to send.
NOTE:
- Even if it works, you are trying to access
null
and assign it to a primitive. NullPointerException
tells that you are callinggetOffsides()
on anull
object. i.e.null.getOffsides()
- If you are using atleast Java 8, you can use
Optional.ofNullable
to setint offsides
to0
whenever it isnull
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论