【问题标题】:Check value for null检查 null 值
【发布时间】:2016-12-03 12:09:53
【问题描述】:

我有这个 Java 代码,我用它来将对象创建到 Java 列表中:

ListIssues obj = new ListIssues(
                    get.getId(),
                    get.getUrl().toString(),
                    get.getTitle(),
                    get.getState().name(),
                    get.getBody(),
                    get.getMilestone().toString(),
                    get.getLabels().toArray(),
                    get.getCreatedAt(),
                    get.getClosedAt()
                );

但有时我有空值。如何以某种聪明的方式检查这些值?

【问题讨论】:

  • 为什么你的ListIssues 构造函数允许空值?它不会抛出异常吗?
  • 这是一个非常简单的Java构造函数。我不知道如何防止这种情况发生。
  • 是什么引发了异常?构造函数中的代码,还是上面的代码? (例如,get.getUrl() 是否有时会返回 null,然后当您尝试调用 toString 时会导致 NPE?)底线是我们没有足够的信息来真正帮助您。
  • 同意。我们需要更多地了解您想看到的内容。如果在构造函数中传递了 null,或者使用了默认值,或者其他一些行为,您是否希望引发异常?

标签: java null


【解决方案1】:

但有时我有空值。我如何检查中的值 一些聪明的方法?

最好的做法是,如果缺少任何必填字段,ListIssues 构造函数应该抛出 IllegalArgumentExcetion(而不是在构造函数之外显式检查 null)。

另外,如果有可以设置的可选字段那么你可以使用builder模式,你可以看here

【讨论】:

    【解决方案2】:

    这有点困难,并且可能会根据应用程序而变化,您应该有一个验证方法,该验证方法返回一个带有 一致 验证结果的布尔值,我强调连贯这个词,因为在某些元素中,如get.getLabels().toArray() 会更好地验证数组是否为空而不是 null...

    无论如何都是一个例子:

    public static boolean isValid(Iget get){
        boolean ret = true;
        ret &= get.getId() !=null;
        ret &= get.getUrl().toString()!=null;
        ...
        ...
        return ret;
    }
    

    【讨论】:

      【解决方案3】:
      // Builder pattern for ListIssues class
      public class ListIssues {
          private String id, title, url, name, body, milestone, createdAt, closedAt;
          private Array labels[];
      
          public static class Builder {
              private String id, title, url, name, body, milestone, createdAt, closedAt;
              private Array labels[];
      
              public Builder withId(String id) {
                  this.id = id;
                  return this;
              }
      
              public Builder withTitle(String title) {
                  this.title = title;
                  return this;
              }
      
              public Builder withURL(String url) {
                  this.url = url;
                  return this;
              }
      
              public Builder withName(String name) {
                  this.name = name;
                  return this;
              }
      
              public Builder withBody(String body) {
                  this.body = body;
                  return this;
              }
      
              public Builder withMilestone(String milestone) {
                  this.milestone = milestone;
                  return this;
              }
      
              public Builder withCreatedAt(String createdAt) {
                  this.createdAt = createdAt;
                  return this;
              }
      
              public Builder withClosedAt(String closedAt) {
                  this.closedAt = closedAt;
                  return this;
              }
      
              public Builder withLabels(Array[] labels) {
                  this.labels = labels;
                  return this;
              }
      
              public ListIssues build() {
                  return new ListIssues(this);
              }
          }
      
          private ListIssues(Builder b) {
              this.id = b.id;
              this.title = b.title;
              this.url = b.url;
              this.name = b.name;
              this.body = b.body;
              this.milestone = b.milestone;
              this.createdAt = b.createdAt;
              this.closedAt = b.closedAt;
              this.labels = b.labels;
          }}
      
              // Now you can check for null input parameters using ternary operators before you create the object.
          ListIssues listIssues = new ListIssues.Builder()
          .withURL(get.getUrl() == null ? "" : get.getUrl().toString())
          .withMilestone(get.getMilestone() == null ? "":get.getMilestone().tpString())
      .withBody(get.getBody())
          .build();
      

      【讨论】:

        【解决方案4】:

        你的问题在某种程度上仍然不清楚,所以我不知道这是否回答了它,但我想提一下我通常这样写我的构造函数:

        public ListIssues(String id, String url, String title, String state,
                Body body, String milestone, String[] labels, Instant createdAt,
                Instant closedAt) {
            super();
            this.id = Objects.requireNonNull(id);
            this.url = Objects.requireNonNull(url);
            this.title = Objects.requireNonNull(title);
            this.state = Objects.requireNonNull(state);
            this.body = Objects.requireNonNull(body);
            this.milestone = Objects.requireNonNull(milestone);
            this.labels = Objects.requireNonNull(labels);
            this.createdAt = Objects.requireNonNull(createdAt);
            this.closedAt = Objects.requireNonNull(closedAt);
        }
        

        如果参数为空,requireNonNull() 将抛出 NullPointerException。如果允许一个参数为空,则省略调用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-01-08
          • 2021-02-19
          • 1970-01-01
          • 2011-02-24
          • 2016-01-10
          • 2012-01-02
          • 1970-01-01
          相关资源
          最近更新 更多