- Home /
Why am I getting a null reference?
if (response is IList<Buddy>)
{
IList<Buddy> buddy = (List<Buddy>) response;
for(int i=0;i<buddy.Count;i++){
Debug.Log("Buddy request from: " + buddy[i].GetBuddyName());
buddy_requests_list.Add(buddy[i].GetBuddyName());
}
GetBuddyRequest.buddy_requests = buddy_requests_list.ToArray();
}
public static string[] buddy_requests;
void OnGUI(){
{
scrollPosition = GUI.BeginScrollView(new Rect(Screen.width - 950, Screen.height - 400, 100, 200), scrollPosition, new Rect(0, 0, 0, 1000));
buddy_selected = GUILayout.SelectionGrid(buddy_selected, buddy_requests, buddy_requests.Length);
GUI.EndScrollView();
}
}
so the code is generating a buddy list then i'm converting it to an array and putting it into the buddy_requests variable. But I'm still getting a null reference error on the line buddy_selected = GUILayout.SelectionGrid(buddy_selected, buddy_requests, buddy_requests.Length);
And I'm not sure why, I use code identical to this in another script and seems to work fine.
Is the whole... section in a method or anything? Right now it looks like it's floating.
This section below...
if (response is IList)
{
IList<Buddy> buddy = (List<Buddy>) response;
for(int i=0;i<buddy.Count;i++){
Debug.Log("Buddy request from: " + buddy[i].GetBuddyName());
buddy_requests_list.Add(buddy[i].GetBuddyName());
}
GetBuddyRequest.buddy_requests = buddy_requests_list.ToArray();
}
yea it's in a public void OnSuccess (object response)
which is the BuddyCallBack for the get GetFriendRequests method
buddyService = sp.BuildBuddyService();
buddyService.GetFriendRequest(userName, new BuddyCallBack());
Can you please paste the actual code, it's confusing without seeing the method in there for the top half that I mentioned.
Answer by wildex999 · Mar 13, 2014 at 07:09 PM
The only thing in the line you mention that can give a Null reference exception is that
public static string[] buddy_requests;
is null at the time OnGUI is called. since buddy_requests.Length would be null.Length. Try putting a Debug where you first set buddy_requests, and a Debug at the beginning of OnGUI to see which is called first.
Answer by Aladine · Mar 13, 2014 at 07:10 PM
i think it's because you are using a variable that is only initiated inside an if statement, mean that it is possible that it will never initiate, and so you get a null reference exception
Your answer
Follow this Question
Related Questions
Scroll List Problem 1 Answer
A node in a childnode? 1 Answer
Make the audio manager select from an array of sounds 1 Answer
List all children in array? 5 Answers