- Home /
List of Quaternions - NullReferenceException with types or type check errors without?
I'm trying to create a list of quaternions. I need to be able to add and remove quaternions at any time, so I don't want to use an Array. But I seem to be getting NullReferenceExceptions no matter what I do.
Here's a simple example:
q = Quaternion(1, 0, 0, 0)
print(q.ToString()) # Works fine
qList = [] as List[of Quaternion]
qList.Add(q) # Crashes here with NullReferenceException: Object reference not set to an instance of an object.
Here's another example:
q = Quaternion(1, 0, 0, 0)
print(q.ToString()) # Works fine
qList = [q] as List[of Quaternion]
print(qList.ToString()) # Crashes here with NullReferenceException: Object reference not set to an instance of an object.
Note that if I remove as List[of Quaternion]
from that second example, it works, but then the compiler only sees qList as a list, so the following then doesn't work:
def methodThatTakesQList(qList as List[of Quaternion]:
print(qList.ToString())
q = Quaternion(1, 0, 0, 0)
print(q.ToString()) # Works fine
qList = [q]
methodThatTakesQList(qList) # The best overload for the method 'methodThatTakesQList(Boo.Lang.List[of UnityEngine.Quaternion])' is not compatible with the argument list '(Boo.Lang.List)'.
If I remove the as List[of Quaternion]
, this example perfectly works too, but I'd like to actually use some methods of quaternions (IE, multiplying them together) which fails the compilation type-check, since it sees it as me just trying to multiply objects together.
I'm finding that it's best to just avoid lists altogether... they cause an unnecessary impact on performance and a lot of Unity just doesn't seem to play nicely with them.
For further questions that are boo related, you might want to include something like [boo]
in the title. Boo questions are quite rare, so if there are people around that actually use boo, it's unlikely that this person will click on your question.
Answer by Bunny83 · Oct 29, 2014 at 09:35 PM
What you are doing there makes not much sense. A List and an array are two seperate things. What you do here is creating an array and then use the as-cast to cast it into a List. However since an array is not a List this cast will fail. And one of the properties of the as-cast is that it will return null if a cast is not possible.
I'm not familiar with boo at all (like most people here). However from what i read the as-cast works the same as in all .NET / Mono languages.
What you actually want is creating an instance of a List. This should look like this:
qList = List[of Quaternion]()
Usually the generic List also has a ToArray method if you need a built-in array from that list. However since boo has it's own List implementation it might be different.
edit
Little correction ;) the []
brackets seem to create an untyped List and not an array. Anyways the same problem: a List
is not a List[of Quaternion]
so an as cast will fail.
Thank you! I hit this same problem again months later and was going to post a new question, meanwhile thinking it has seemed like I had asked about this before. Checked my list of asked questions and lo and behold, I had! Tried your solution and it worked! Thanks!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Force typcasting of member objects in a List? (Boo) 1 Answer
List of Objects in Boo 0 Answers
How to Find an Object close to where the Player is Looking 1 Answer
Show boo list in inspector? 0 Answers