How to initialize a class object?

My code looks something like this:

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

And I get an error like this:

Exception in thread "Foo" java.lang.NullPointerException

on any line in Foo, I'm trying to call any function or value of the Bar class. How to prevent bar [] from being null?

EDIT: After some attempts, I finally fixed them, thanks everyone! However, I could not call the constructor to fix the situation; I had to create another function and call this function from Main (in my case, the Foo class is actually the Main class, if that really matters). My end result:

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

Anyone want to help me with this, or should I create another question? :)

+3
source share
8

, Bar[] bar = new Bar[10];, ? .

:

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

a = bar[0].baz;

Bar, Bar. . :

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

, 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;
}
+3

:

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

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

. , , , :

  • Foo Foo() {/*... */} .
  • , , java.util.Arrays.fill(Object [] a, Object 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()

Your method initializeFoo()does not compile. This method is static and cannot access the non-stationary member of the class (for example, barin your case).

And finally, public static void Foo()it is not a constructor, but a method. The constructor does not have a return type ( voidin your case).

+1
source

Source: https://habr.com/ru/post/1729785/


All Articles