如何解决在使用 Jackson 1.x 时出现的空指针错误。

huangapple go评论71阅读模式
英文:

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(&quot;offsides&quot;)
private Integer offsides;
...
...
@JsonProperty(&quot;offsides&quot;)
public Integer getOffsides() {
return offsides;
}
@JsonProperty(&quot;offsides&quot;)
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&lt;String&gt; insertStatus = FixturesFixturesInsert.insertAllFixtures(fixturesFixtures, 
statsNumb, playersNumb, eventsNumb, lineupsNumb);
}
}

...

public class FixturesFixturesInsert {
public static List&lt;String&gt; insertAllFixtures(FixtureJSONMapp fixturesFixtures, int statsNumb, int playersNumb, int eventsNumb, int lineupsNumb) throws Exception	{
try	{
Connection con = dbConnector.ConnectToDB();
PreparedStatement pstmtFixtures = con.prepareStatement(&quot;.....&quot;);
for(int i = 0; i &lt; 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(...) 应用于传入数据。我不这么认为,它对即将发送的数据生效。

注意:

  1. 即使它生效了,你正试图访问 null 并将其赋值给一个基本类型。
  2. NullPointerException 表明你在一个 null 对象上调用了 getOffsides(),即 null.getOffsides()
  3. 如果你至少在使用 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:

  1. Even if it works, you are trying to access null and assign it to a primitive.
  2. NullPointerException tells that you are calling getOffsides() on a null object. i.e. null.getOffsides()
  3. If you are using atleast Java 8, you can use Optional.ofNullable to set int offsides to 0 whenever it is null

huangapple
  • 本文由 发表于 2020年10月20日 16:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64441580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定