Java 在理解数据类结构中嵌入的对象数组时遇到困难
我试图创建一个静态的嵌套数据结构。 嵌套项之一是对象数组。 我似乎无法将新对象分配给该数组。 这对于以太中的大师来说很可能是微不足道的Java 在理解数据类结构中嵌入的对象数组时遇到困难,java,Java,我试图创建一个静态的嵌套数据结构。 嵌套项之一是对象数组。 我似乎无法将新对象分配给该数组。 这对于以太中的大师来说很可能是微不足道的 ///////////////////////////////////////////////////// class test ///////////////////////////////////////////////////// { //=========================================== public stati
/////////////////////////////////////////////////////
class test
/////////////////////////////////////////////////////
{
//===========================================
public static class DataClass
//===========================================
{
static String thing = new String("thing 1");
public static class InnerClass
{
static String thing = new String("thing 2");
public class this_thingy
{
String thing;
String other;
void init() { thing="thing 3"; other="other"; }
}
public static this_thingy[] classArray = new this_thingy[3];
}
}//DataClass
//===========================================
public static void main( String[] args )
//===========================================
{
System.out.println( DataClass.InnerClass.thing );
System.out.println( DataClass.InnerClass.classArray[0].thing );
System.out.println( DataClass.InnerClass.classArray[0].other );
}//main
}//test
/*
null
Exception in thread "main" java.lang.NullPointerException: Cannot load from object array because "test$DataClass$InnerClass_3.InnerClass_3a" is null
at test.main(test.java:31)
*/
您需要初始化数组的每个元素
public static class this_thingy{
String thing;
String other;
void init() { thing="thing 3"; other="other"; }
}
public static this_thingy[] classArray = new this_thingy[3];
static {
for(int i = 0; i < classArray.length; i++){
(classArray[i] = new this_thingy()).init();
}
}
public static类这个东西{
弦的东西;
其他字符串;
void init(){thing=“thing 3”other=“other”}
}
public static this_thingy[]classArray=new this_thingy[3];
静止的{
for(int i=0;i
关闭。下面是我现在看到的:test.java:27:error:non-static变量this不能从静态上下文类数组中引用[I]=newthis_thingy()^@罗德,它给了你什么?非常感谢!!我的最后一个问题是我需要删除*.class文件。之后,一切顺利。非常感谢您的解决方案@没问题。