0

I've seen other post on this forum, that the solution was an unitialized String.

Why is this returning a null pointer exception, since I'm initializing the string, and the array of Foo?

class Foo
{
    public String Name = "foobar";
    public int Score = 0;
 };

class Bar
{
    private Foo[] timerScore = new Foo[10];

    //....

    private void xpto()
    {
        timerScore[0].Name = "test"; //<---null pointer exception.why?!
        timerScore[0].Score = 30;
    }
}
waas1919
  • 2,365
  • 7
  • 44
  • 76

1 Answers1

1

Creating an array of Foo doesn't create the actual instances of Foo.

You need :

private void xpto()
{
    timerScore[0] = new Foo ();
    timerScore[0].Name = "test";
    timerScore[0].Score = 30;
}
Eran
  • 387,369
  • 54
  • 702
  • 768