- Home /
Null reference when accessing database manager singleton
I'm starting to get back to my app. I wanted to convert the database functions to a singleton model, previously had the database functions in each scene which worked fine until I export it to an Android file. It would try to do it and stop because a get_datapath error.
I'm getting a null reference every time I try to access the database functions. I know it is working because they are showing up in the code after the call DatabaseManager.GetInstance().functionname. Where am I going wrong here?
void Awake()
{
Debug.Log("Member Manager Awake Fires");
//Instance = this;
SQLiteInit();
Members=new List<TheMembers>();
}
// Use this for initialization
void Start ()
{
Debug.Log("Member Manager Start Fires");
DatabaseManager.GetInstance().GetAllMembers();
ShowAllMembers();
}
On the database manager script static DatabaseManager instance; public MembersManager MM; public static DatabaseManager GetInstance() { return instance; }
public void GetAllMembers()
{
Debug.Log("Get all members called!");
MM.Members.Clear();-- clears the list from the other script
mConnection.Open();
mSQLString = "SELECT * FROM " + SQL_TABLE_MEMBERS + " ORDER BY " + COL_MEMBER_ID;
mCommand.CommandText = mSQLString;
mCommand.ExecuteNonQuery();
mReader = mCommand.ExecuteReader();
while (mReader.Read())
{------------Adds to the list from the other script
MM.Members.Add(new TheMembers(mReader.GetString(0),
mReader.GetString(1)));
Debug.Log(mReader.GetString(0) + mReader.GetString(1));
}
mReader.Close();
mConnection.Close();
}
Do I need to define the variables such as the list and the others like MemberID, etc in the database manager script? I thought I could just point back to the Member script, but I'm not sure how that works exactly. Had a hard time even getting the Init function to fire off without a null reference, so I'm calling it locally, which defeats the purpose of using a singleton. Please give detailed answers as some of this is still new. Thanks!
Is Database$$anonymous$$anager a $$anonymous$$onobehaviour?
No thats perfectly fine. $$anonymous$$onobehaviour Singletons are different than C# singletons though.
Here is what your singleton code should look like:
public static Database$$anonymous$$anager Instance { get; private set; }
void Awake() {
if (Instance != null & Instance != this) {
Destory(gameObject);
return;
}
Instance = this;
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
SQLite Sort and or List Sort 1 Answer
Return List from Singleton 1 Answer
A node in a childnode? 1 Answer