2010-01-24 50 views
3

我的代码是这样的:如何初始化一个类的对象?

public class Foo { 
    public int a; 
    Bar[] bar = new Bar[10]; 

    a = bar[0].baz; 
} 

public class Bar { 
    public int b; 

    public Bar() { //I tried doing away with this constructor, but that didn't 
        //fix anything 
     b = 0; 
    } 

    public int Baz() { 
     //do somthing 
    } 
} 

我也得到类似的错误消息:

Exception in thread "Foo" java.lang.NullPointerException 

在富哪个行我尝试调用Bar类的任何功能或价值。如何防止bar []变为空?

编辑:经过一番捣鼓之后,我终于搞定了,谢谢大家! 但是,我无法调用构造函数来解决问题;我必须创建另一个函数,并从Main调用该函数(在我的情况下,Foo类实际上是Main类,如果真的很重要)。我的最终结果:

public class Foo { 
    public int a; 
    Bar[] bar = new Bar[10]; 

    public Foo() { //this constructor wasn't called for some reason... I checked this 
        //by using System.out.println... no message was print onscreen 
     for (int a = 0; a < bar.length; a++) 
      bar[a] = new Bar(); 
    } 

    public static void initializeFoo() { 
     for (int a = 0; a < bar.length; a++) 
      bar[a] = new Bar(); 
    } 

    public static void Foo() { 
     initializeFoo(); 
     a = bar[0].baz; 
    } 
} 

任何人都想帮助我,或者我应该创建另一个问题? :)

+0

你总是做同样的方式 - 初始化它指向堆中的对象。 – duffymo 2010-01-24 16:12:42

+0

如果你需要帮助你的代码,你将不得不向我们展示实际导致问题的代码,而不是你认为代表问题的其他代码,但错过了重要的位,甚至没有编译。 – 2010-01-24 16:57:40

+0

@Michael谢谢,下次我会这样做 – wrongusername 2010-01-25 01:05:40

回答

6

您有引用分配内存通过编写此Bar[] bar = new Bar[10];但谁将会分配内存数组元素?事实上引用类型的数组是用空引用初始化的。

您需要分配内存数组元素过多:

for(int i=0; i<bar.length; ++i) 
    bar[i]=new Bar(); 
3

你得到一个NullPointerException异常的原因是因为该行的:

a = bar[0].baz; 

bar[0]还不存在。

发生什么事是当Foo被初始化时,它的默认初始值设定项会运行(就像上面提到的那样)。您将bar构造为Bar引用的数组,但不要初始化数组中的各个位置。如果你希望它们都被初始化,你需要做的是这样的:

public Foo() { 
    bar = new Bar[10]; 
    for (int i = 0; i < 10; i++) { 
     bar[i] = new Bar(); 
    } 
    a = bar[0].baz; 
} 
1

试试这个:

public class Foo 
{ 
    public int a; 
    Bar[] bar = new Bar[10]; 

    public Foo() 
    { 
     for (int i = 0; i < 10; ++i) 
     { 
      this.bar[i] = new Bar(); 
     } 
    } 
    a = bar[0].baz(); 
} 

public class Bar 
{ 
    public int b; 


    public int baz() 
    { 
     return b; 
    } 
} 
+0

构造函数中的'b = 0'是多余的。 ,并在两个地硬编码10是不是很好;) – Bozho 2010-01-24 16:16:25

+0

肯定的,但是这是他的代码。我一次只修理一个问题。 – duffymo 2010-01-24 16:17:13

+0

只是提醒:) – Bozho 2010-01-24 16:18:40

8
Bar[] bar = new Bar[10]; 

a = bar[0].baz; 

上面创建类型酒吧的数组,但不填充它与任何实际的Bar对象。引用类型的数组使用空引用进行初始化。你需要这样做:

for(int i=0; i<bar.length; i++) { 
    bar[i] = new Bar(); 
} 
+0

的感谢!工作,但由于某种原因,我不能让构造函数来让它工作;我不得不在另一种方法中使用该代码 – wrongusername 2010-01-24 16:42:38

1

Bar[] bar = new Bar[10]紧接着,你的酒吧阵列与空值初始化。所以bar[0]包含null并且调用bar[0].baz将导致NullPointerException

您需要使用像bar[0] = new Bar()这样的语句来填充阵列。或

for (int i = 0; i < bar.length; i++) { 
    bar[i] = new Bar(); 
} 
1

这里的关键问题是array initialization in Java一种误解。 bar []的初始化将是“10个空栏的引用”。

这里有一个修正的例子(它不使用10),虽然它不是其他原因非常优秀的Java风格(如公共成员):

class Foo { 
    Bar[] bar = new Bar[] { new Bar(), new Bar() }; 
    public int a = bar[0].b; 
} 

public class Bar { 
    public int b = 0; 

    public static void main(String... args) { 
     Foo foo = new Foo(); 
     System.out.println(foo.a); 
    } 
} 

这个例子更接近真实风格Java:

class Foo { 
    static final int numBars = 10; 
    private Bar2[] bar = new Bar2[numBars]; 
    private int a; 

    public int getA() { return a; } 

    public Foo() { 
     for (int i = 0; i < numBars; i++) { 
      bar[i] = new Bar2(); 
     } 
     a = bar[0].getB(); 
    } 
} 

public class Bar2 { 
    private int b = 0; 
    public int getB() { return b; } 

    public static void main(String... args) { 
     Foo foo = new Foo(); 
     System.out.println(foo.getA()); 
    } 
} 
1

许多正确答案的工作。我想你可能有一些问题:

  • 一种富构造将 美孚(){/ * ... * /} 不仅将在类的任意代码。
  • 而不是自己使用一个循环,你可以使用 java.util.Arrays.fill(对象[]一,对象VAL)

像这样:

public class Foo { 
    public int a; 
    Bar[] bar = new Bar[10]; 

    Foo() { 
     java.util.Arrays.fill(bar, new Bar()); 
     a = bar[0].baz(); 
    } 
} 

编辑:为了您的更新的问题,只需拨打 新的Foo() 从 主(...) 方法。另外,你不能像你那样有一个静态构造函数。

1

一些答案和注释代码:

构造函数不会被调用自身,您的发言new Foo()

initializeFoo()方法不编译activly调用它。该方法是静态的(在你的情况一样bar)不能访问非静态类成员。

最后,public static void Foo()不是一个构造函数,但方法。构造函数没有返回类型(您的情况为void)。