Java – 类中嵌套的自定义类型

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

Java - Custom type nested in class

问题

我再次请求技术支持。

我需要在类内部定义一个自定义类型,我是这样做的:

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.myArray[0].varB);
    }
}

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

我已经测试过 myArray 是否被正确地初始化为 1024 个元素,它确实被初始化了,但我似乎无法访问它们。

我刚从 C++ 转到 Java,所以我还在适应中,我是不是漏掉了什么明显的东西?

英文:

I am once again asking for technical support.

I need to define a custom type inside a class, I've done it like this:

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);
    }
}

When I run it throws a NullPointerException at System.out.println(this.CustomType[0].varB);

I've tested if myArray gets properly initialized with 1024 elements and it does, however I can't seem to access them.

I just moved from C++ to Java so I'm still getting used to it, am I missing something blatant?.

答案1

得分: 2

你只是创建了一个没有任何对象的数组,所以 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 进行访问。

英文:

You only create an array without any objects, so this.CustomType[0] is null.

You should add the objects to the array:

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

Also you should make the member of CustomType private and access it via getter and setter.

答案2

得分: 1

  • 你必须实例化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.myArray[i] = new CustomType();
        }
        // Or
        Arrays.setAll(myArray, CustomType::new);
        System.out.println(this.myArray[0].varB);
    }
}

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

英文:

Two things,

  • You must instantiate CustomType.
  • CustomType does not need access to MainClass.this so you can make it static.

So

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 &lt; myArray.length; ++i) {
            this.CustomType[i] = new CustomType();
        }
        // Or
        Arrays.setAll(myArray, CustomType::new);
        System.out.println(this.CustomType[0].varB);
    }
}

Not making it static stores a MainClass.this in every CustomType instance which is unnecessary overhead.

答案3

得分: 1

在Java中,数组是对象。你发布的代码中的下一行创建了一个包含1024个元素的数组,其中每个元素都是null。

myArray = new CustomType[1024];

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

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

然后,你可以执行以下代码,它不会抛出NullPointerException异常。

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

Arrays in java are objects. The following line of the code you posted creates an array of 1024 elements where each and every element is null.

myArray = new CustomType[1024];

If you want to place actual objects in the array, named myArray, you need to create instances of class CustomType and assign them to elements of the array, for example:

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

Then you can execute the following line of code and it will not throw NullPointerException.

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

答案4

得分: -1

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

Here is the full code to get the value of varB. In which you can avoid declaring 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);
	}
}

答案5

得分: -1

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

  1. 构造函数将在创建该类的对象时被调用。

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

英文:

The solution is to add some elements to that array. See the below steps for more information.

  1. constructor will be invoked, when you create the object of that class

  2. And then you created an empty array of CustomType with size 1024 and trying to access the first element which does not exist(default is null) and trying to perform operations on that null reference. So you are getting the NullPointerException.

huangapple
  • 本文由 发表于 2020年3月16日 19:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/60704731.html
匿名

发表评论

匿名网友

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

确定