英文:
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<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 < 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
解决方案是向该数组添加一些元素。有关更多信息,请参阅以下步骤。
-
构造函数将在创建该类的对象时被调用。
-
然后,您创建了一个大小为1024的空的CustomType数组,并尝试访问第一个不存在的元素(默认为null),并试图对该空引用执行操作。因此,您会得到空指针异常。
英文:
The solution is to add some elements to that array. See the below steps for more information.
-
constructor will be invoked, when you create the object of that class
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论