【问题标题】:Java - Custom type nested in classJava - 嵌套在类中的自定义类型
【发布时间】:2020-03-16 11:01:29
【问题描述】:

我再次请求技术支持。

我需要在一个类中定义一个自定义类型,我已经这样做了:

public class MainClass {
    private class CustomType {
        public byte[] varA;
        public int varB;

        public CustomType() {
            varA = new byte[3];   
            varB = 13;
        }
    }

    private CustomType[] myArray;


    public MainClass() {
        myArray = new CustomType[1024]
        System.out.println(this.CustomType[0].varB);
    }
}

当我运行它时,它会在System.out.println(this.CustomType[0].varB); 处抛出一个NullPointerException

我已经测试了 myArray 是否使用 1024 个元素正确初始化并且确实如此,但是我似乎无法访问它们。

我刚从 C++ 迁移到 Java,所以我还是习惯了,我是不是漏掉了什么明显的东西?。

【问题讨论】:

  • 在Java原语中默认为0,布尔默认为false,其他一切默认为null。

标签: java class inner-classes custom-type


【解决方案1】:

你只创建了一个没有任何对象的数组,所以 this.CustomType[0] 为空。

您应该将对象添加到数组中:

public MainClass() {
    myArray = new CustomType[1024]
    for (int i =0; i<myArray.length;i++ {
      myArray[i] = new CustomType();
    }
    System.out.println(this.myArray[0].varB);
}

您还应该将CustomType 的成员设为私有并通过getter 和setter 访问它。

【讨论】:

    【解决方案2】:

    两件事,

    • 您必须实例化 CustomType。
    • CustomType 不需要访问MainClass.this,因此您可以将其设为静态。

    所以

    public class MainClass {
        private static class CustomType {
            public byte[] varA;
            public int varB;
    
            public CustomType() {
                varA = new byte[3];   
                varB = 13;
            }
        }
    
        private CustomType[] myArray;
    
    
        public MainClass() {
            myArray = new CustomType[1024];
            for (int i = 0; i < myArray.length; ++i) {
                this.CustomType[i] = new CustomType();
            }
            // Or
            Arrays.setAll(myArray, CustomType::new);
            System.out.println(this.CustomType[0].varB);
        }
    }
    

    不使其静态在每个 CustomType 实例中存储一个 MainClass.this,这是不必要的开销。

    【讨论】:

    • @Jens 谢谢,更正了复制错误。并添加了 setAll,尽管在 C++ 到 java 切换之后可能太高级了。
    【解决方案3】:

    java 中的数组是对象。您发布的以下代码行创建了一个包含 1024 个元素的数组,其中每个元素都为空。

    myArray = new CustomType[1024];
    

    如果要将实际对象放入数组中,命名为myArray,则需要创建类CustomType 的实例并将它们分配给数组的元素,例如:

    CustomType instance = new CustomType();
    myArray[0] = instance;
    

    然后你可以执行下面这行代码,它不会抛出NullPointerException

    System.out.println(myArray[0].varB);
    

    【讨论】:

      【解决方案4】:

      这是获取varB 值的完整代码。您可以避免在其中声明CustomType[] myArray

      public class Test 
      {
          private static class CustomType 
          {
              public byte[] varA;
              public int varB;
      
              public CustomType() {
                  varA = new byte[3];   
                  varB = 13;
              }
          }
      
      
          public static void main(String... args) 
          {       
              System.out.println(new CustomType().varB);
          }
      }
      

      【讨论】:

      • op 引入这个数组是有原因的
      【解决方案5】:

      解决方案是向该数组添加一些元素。有关详细信息,请参阅以下步骤。

      1. 当您创建该类的对象时,将调用构造函数

      2. 然后您创建了一个大小为 1024 的 CustomType 空数组,并尝试访问第一个不存在的元素(默认为 null)并尝试对该 null 引用执行操作。所以你得到了 NullPointerException。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-02
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        相关资源
        最近更新 更多