Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-嵌套在类中的自定义类型_Java_Class_Inner Classes_Custom Type - Fatal编程技术网

Java-嵌套在类中的自定义类型

Java-嵌套在类中的自定义类型,java,class,inner-classes,custom-type,Java,Class,Inner Classes,Custom Type,我再次请求技术支持 我需要在类中定义自定义类型,我是这样做的: public class MainClass { private class CustomType { public byte[] varA; public int varB; public CustomType() { varA = new byte[3]; varB = 13; } }

我再次请求技术支持

我需要在类中定义自定义类型,我是这样做的:

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,所以我还是习惯了,我漏掉了什么东西吗?< /p> < p>你只创建一个没有任何对象的数组,所以这个。CuoType(0)是NULL。 应将对象添加到阵列中:

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);
}
public MainClass(){
myArray=新自定义类型[1024]
对于(int i=0;i两件事

  • 您必须实例化CustomType
  • CustomType不需要访问
    MainClass。此
    可以使其成为静态的
所以

public类MainClass{
私有静态类CustomType{
公共字节[]varA;
公共int varB;
公共自定义类型(){
varA=新字节[3];
varB=13;
}
}
私有CustomType[]myArray;
公共类(){
myArray=新自定义类型[1024];
对于(int i=0;i

不使其成为静态将存储一个
main类。这在每个
CustomType
实例中都是不必要的开销。

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

myArray=newcustomtype[1024];
如果要将实际对象放置在名为
myArray
的数组中,则需要创建类
CustomType
的实例,并将它们指定给数组的元素,例如:

CustomType实例=新的CustomType();
myArray[0]=实例;
然后,您可以执行以下代码行,它不会抛出
null pointerexception

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

以下是获取
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);
    }
}

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

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

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


  • 在java原语默认为0,布尔默认为false,其他默认值为null。@ JEN谢谢,纠正了复制错误。并且添加了SETALL,尽管C++到java交换机之后可能会太先进。
    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);
        }
    }