- Home /
NullReference when updating a List
I have a Nullreference error when updating a List. This List only keep items for an inventory, but the problem is not the List itself but when it tries to update it the first time.
The List is in a separate menager using a singleton, but I'm trying to update it on the inventory buttons, the buttons when the inventory is activated will load the corresponding item of the list to the button number. I'm using NGUI to build up the inventory, but here is the button script:
using UnityEngine;
[AddComponentMenu("UI Scripts/PDA/Inventory/UI Inventory Item Button")]
public class UIInvItemButton : UIInventoryButton {
private bool isEmpty = true;
public int itemSlotNumber;
InventoryMenager inventoryMenager;
ItemInfo itemInfo;
UISprite UIItemPreviewSprite;
UILabel UIInvItemDescLabel;
UILabel UIInvItemDescTitleLabel;
void Awake () {
UIInvItemDescLabel = GameObject.Find ("/UIPlayer/Camera/UIAnchor_C/PDAPanel_C/InventoryPanel_C/UIInvItemDescArea/UIInvItemDescLabel").GetComponent<UILabel>();
UIInvItemDescTitleLabel = GameObject.Find ("/UIPlayer/Camera/UIAnchor_C/PDAPanel_C/InventoryPanel_C/UIInvItemDescArea/UIInvItemDescTitleLabel").GetComponent<UILabel>();
}
void Start () {
inventoryMenager = InventoryMenager.Instance;
}
void OnEnable () {
UIInvItemDescLabel.enabled = false;
UIInvItemDescTitleLabel.enabled = false;
}
//Check for items avaible in slots
public void LoadInventoryItems() {
UIItemPreviewSprite = GetComponentInChildren<UISprite>();
if(isEmpty){
if(inventoryMenager.itemsList.size > itemSlotNumber){
itemInfo = inventoryMenager.itemsList[itemSlotNumber];
}
}
UIItemPreviewSprite.spriteName = (itemInfo != null) ? itemInfo.UIItemIconSelect : "inventoryItemSlotEmpty";
isEmpty = (itemInfo != null) ? false : true;
}
//Overlay item description
void OnClick () {
UIInvItemDescLabel.enabled = true;
UIInvItemDescTitleLabel.enabled = true;
if (!isEmpty) {
UIInvItemDescLabel.text = itemInfo.itemDesc;
UIInvItemDescTitleLabel.text = itemInfo.itemName;
}
}
}
The function that is run is LoadInventoryItems(), it is run by another script that is the UI menager. I'm getting the error on this line:
if(inventoryMenager.itemsList.size > itemSlotNumber){
itemSlotNumber is manually written on the inspector starting from 0 as int. I can avoid this problem only by putting that function in Update, but I want to avoid it as updating a List on 16 buttons can be considerably a performance loss.
The eror only appear once, then as soon I pickup an item is all ok and it update. Any help is apreciate.
On a side note, you can notice that the inventorymenager uses size instead of Count, that because I've been using BetterList of NGUI, but is the same thing.
Answer by Nidre · Dec 14, 2012 at 01:40 PM
As i understood you don't get any null referance exception when you put the LoadInventoryItems method on Update method.And when you put it out of Update this exception is only appearing once.
If i understood correctly that might mean that
void Start () {
inventoryMenager = InventoryMenager.Instance;
}
did not fired yet when you are calling LoadInventoryItems method first time.
Can you replace your LoadInventoryItems method with the following method to see if thats the case.
//Check for items avaible in slots
public void LoadInventoryItems() {
UIItemPreviewSprite = GetComponentInChildren<UISprite>();
if(isEmpty){
if(InventoryMenager.Instance.itemsList.size > itemSlotNumber){
itemInfo = InventoryMenager.Instance.itemsList[itemSlotNumber];
}
}
UIItemPreviewSprite.spriteName = (itemInfo != null) ? itemInfo.UIItemIconSelect : "inventoryItemSlotEmpty";
isEmpty = (itemInfo != null) ? false : true;
}
If the code above works for you.That means you are calling LoadInventoryItems before inventoryMenager is initialized.
You can try to replace your Start method with Awake method as a fix.
Hope this helps you out.
Answer by Shrandis · Dec 14, 2012 at 01:00 PM
if (inventoryManager.itemsList.size > itemSlotNumber)
If this line is throwing a null reference exception, it can mean 2 things:
1)inventoryManager is null.
2)inventoryManager.itemsList is null.
There's also this code:
void Start () {
inventoryMenager = InventoryMenager.Instance;
}
My guess is, LoadInventoryItems method is being called BEFORE this script's Start method assings inventoryManager, causing a null reference exception. Check where you call LoadInventoryItems and make sure inventoryManager is assigned to InventoryManager.Instance BEFORE you call LoadInventoryItems.
Answer by MarkFinn · Dec 14, 2012 at 01:13 PM
Almost certainly the issue is that the itemsList List object has not been created when this call is made.
The two options for a fix are :
1) Quick and dirty (add the extra check):
if (inventoryMenager.itemsList==null){ inventoryMenager.itemsList=new List(); } if(inventoryMenager.itemsList.size > itemSlotNumber){
2) Proper interface based design:
Make the itemsList List private within the singleton, and add public methods Size(), Add(Whatever newitem), etc... which cope properly with the list being null or empty.
Answer by $$anonymous$$ · Dec 14, 2012 at 09:52 PM
All of you were right, the instance weren't getting there in time, still putting it on Awake didn't help like it got ignored, but I did a work around putting a public function only for the instance:
public void InstantiateInventory () { inventoryMenager = InventoryMenager.Instance; }
Then I just call that on the my Ui menager at startup, after that it all goes right. I guess the main problem was the fact that the UIInvItemsButton is deactivated at startup and so the code just skip it. By the way thanks to everyone for the help.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Object Method returning null when calling Object List from another Class 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers