Pages

Tuesday, November 9, 2010

Java stuff: Declaring space in Array

One thing I notice about the Array in Java is that, you have to declare whether the Array have space or not, before you can finally Push() values in.

While I was writing my current project's script, I did something like this:

private var arr : Array;

function Start () {
  arr.Push("2");
}

When I click Play, the compiler give me this error:
NullReferenceException: Object reference not set to an instance of an object

I realized I had faced the same problem before and my solution previously was to declare a "space" for the array by adding "[]" after declaring the array...

private var arr : Array = [];

function Start () {
  arr.Push("2");
}

I posted this because I never had the same problem in Flash AS3 before... I'm still consider new in Java since I first started using Unity this March, but they are basically the same, just that AS3 is more to object oriented stuff... 

No comments: