- Home /
null reference exception problem with generic lists in unityscript (i think...)
Sorry if the following code is confusing!
I get a null reference exception on line noted below and can't fix it...i'm fairly new to generic lists so I thought it might be because i was declaring a generic list at runtime but i get not better results when declaring outside of runtime... Hopefully there's something glaringly obvious to someone with fresh eyes who can work their way through my mess of code..
this is part of a couple of large scripts which have been made to essentially create a coded sample playback system to recreate music in a slightly random way, this part of the code is to check if any of the major parts (that belong to the currently playing section) are actually playing (child_play) and if not to set one of them playing (chosen randomly). This is to avoid embarrassing total silences.
playing_section and current_section are both enum variables set through code and inspector (respectively)
(oh, the Debug.Log line just before the add line works as expected)
Any help will be greatly appreciated!
if (should_check_important_parts){
var current_section_important_parts : List.<MusicMakerAnglesChild01>;// list of scripts
var an_important_part_is_playing : boolean = false;
for (var i : int = 0;i < important_parts.Count; i++){
for (var ii : int = 0;ii < important_parts[i].child_belongs_in.length; ii++){
if (important_parts[i].child_belongs_in[ii] ==
sections[current_section].playing_section){
Debug.Log("current section important part to add: " + important_parts[i].gameObject.name);
current_section_important_parts.Add(important_parts[i]);// NULL REFERENCE ERROR HERE**
if (important_parts[i].child_play && !an_important_part_is_playing){
an_important_part_is_playing = true;
}
}
}
}
if (!an_important_part_is_playing){
var part_to_start : int = Random.Range(0,current_section_important_parts.Count);
current_section_important_parts[part_to_start].child_play = true;
}
should_check_important_parts = false;
}
Answer by Benproductions1 · May 23, 2014 at 12:07 PM
Hello,
I think you need to go and learn how to use generic lists.
Like any other class
type, List
is a reference type, meaning both that it's default value is null
, and that it will get passed around by reference, rather than by value. (If you don't know what that means, go learn some OOP)
This means your:
var list:List.<Type>;
will be null
to begin with. You will have to create a new instance of List
to do what you want:
List.<Type>();
On that note, the other things wrong with your code are: (not in any particular order)
Indentation (My god, use a tab or four spaces, not 2, 5, then 8!)
Inconsistante variable names (UseCamelCaseForVariables, not_this_unconventional_stuff)
Long/Bad variable names (`MusicMakerAnglesChild01`... need I say more?)
No comments or any empty lines to indicate any sort of code organization...
Inconsistant Spacing (Please, for the sake or readable code, put a space between a
)
and a{
!)
Fixing at least some of the above, as well as making them a habit, will not only make you a better coder, but it will also make coming back to the code and fixing any bugs much easier. At least when you come back to the code 4 months later you can at least still read the code and understand what it does, rather than having to throw it all out and start again :)
I tend to use Camel case for function then lower Camel case for variables. sort of like this
//variable example
object myVariable;
//Function Example
object $$anonymous$$yFunction( object myVariable ) {
throw System.NotImplementedExpception()
}
It helps me easily distinguish from variables , classes, and Functions. another reason for the lower case at the start is that some languages use capital starting letters to denote a constant variable. which i format in all caps
thanks Benproductions1 ! I can't believe I've got anything working so far (with generic lists) without knowing that - looks like i'll have to revisit all the others i've been not instantiating - is it possible that lists declared outside of a function are automatically instantiated? Certainly seems that way....
I've been making up this system as i go and yes it's a mess - i've accepted i'll probably need to rebuild it from the ground up at some point but i appreciate your other comments as well :
Indentation is all fine in the script - must have been altered in the copy/paste to here
$$anonymous$$usic$$anonymous$$akerAnglesChild01 is a terrible script name i know! Need to change that
I always use lower case with underscores for variables but if there is an accepted na$$anonymous$$g method please point me at it! I didn't think there was one (lower camel-case probably would be easier as Fornoreason1000 suggested if only for typing purposes. I'll definitely consider that in future projects)
i always intend to comment code after i've got it working correctly. I always don't... that and the inconsistent na$$anonymous$$g (i don't mean formatting) is what i need to sort out
re. spaces between brackets, these are consistent and read well to me but again, if there is an accepted way to do this...
anyway, thanks again for the solution!
UnityGems has a beautiful article: Coding Conventions. Of course the article is written for C#, but most of the conventions still hold. Since UnityScript is loosely based on JavaScript, I'd suggest you also take a look at some JavaScript coding conventions, such as this one.
Generally I try to write comments while I code, or at least after I've finished writing a little block. This means that when I come back tomorrow, or a week later, I'll still know what I was working towards. Comments are sort of like documentation. If you don't write them while you progress, you'll end up with a working application, but no way to understand how it works.