Basically this is what I have:
----------------------------
MainMenu main = new MainMenu();
main.LoggedUser = u;
main.Show();
----------------------------
Then in MainMenu.cs I have:
----------------------------
name_room.DrawnText = LoggedUser.User_First_Name;
----------------------------
The error is coming from MainMenu.cs, when I move accross too this page.
What I'm trying to do here is use the object "u" which has the user details in it on another page form.
Any help would be much appreciated!
Thanks
6 replies to this topic
#1
Posted 17 August 2010 - 02:31 AM
|
|
|
#2
Posted 17 August 2010 - 02:45 AM
I get the error:
object reference not set to an instance of an object
object reference not set to an instance of an object
#3
Posted 17 August 2010 - 03:16 AM
That error happens if you do stuff with null / nothing
Most likely 'u' is null. Which will result in you attempting to get the User_First_Name of null which results in your error.
String string; string.Length; //fails String string2 = "Aa"; string2.Length; //works
Most likely 'u' is null. Which will result in you attempting to get the User_First_Name of null which results in your error.
#4
Posted 17 August 2010 - 03:22 AM
Thank you got the reply, but 'u' isn't null, it's filled in the 'Login.cs'
Here I'll show you what I mean.
There are 3 scripts here, User.cs, Login.cs and MainMenu.cs
A reply would be much appreciated, I've been trying to figure this out for 3 hours now :(
--------------------------------------User.cs--------------------------------------------
namespace OfficeNotes
{
public class User
{
//----------------------------------------------------------------------------------------
//Username string
private string _username;
public string Username
{
get { return _username; }
set { _username = value; }
}
//Pin String
private string _pin;
public string Pin
{
get { return _pin; }
set { _pin = value; }
}
//UserID String
private int _userID;
public int UserID
{
get { return _userID; }
set { _userID = value; }
}
//UserFirstname String
private string _userfirstname;
public string User_First_Name
{
get { return _userfirstname; }
set { _userfirstname = value; }
}
//UserLastName String
private string _userlastname;
public string User_Last_Name
{
get { return _userlastname; }
set { _userlastname = value; }
}
//UserRoom String
private string _userroom;
public string User_Room
{
get { return _userroom; }
set { _userroom = value; }
}
//--------------------------------------------------------------------------------
//Connects to SQL database
private SqlConnection connect;
//Constructor of "Login" also connection to the SQL database and storing details in "constring".
public User()
{
string constring = "Data Source=OLLY-PC\\SQLEXPRESS;Initial Catalog=OfficeNotes;User ID=OfficeNotes;Password=222;";
ConnectToDatabase(constring);
}
public void ConnectToDatabase(string info)
{
connect = new SqlConnection(info);
connect.Open();
}
public bool FillObject(string inPIN, string inUser){
//Filling "sql" inPin & inUser with key's from FillObject
string sql = "SELECT UserID, Username, Pin, FirstName, LastName, Room FROM NotesUser WHERE Pin = '" + inPIN + "' AND Username = '" + inUser +"'";
//Executing the application on the database
SqlCommand sqlcommand = new SqlCommand(sql,connect);
SqlDataReader sr = sqlcommand.ExecuteReader();
//Fills strings with values from database if datareader reads, the process = strings sql > sqlcommand > sr > DATA
if(sr.Read())
{
this.UserID = sr.GetSqlInt32(0).Value;
this.Username = sr.GetSqlString(1).Value;
this.Pin = sr.GetSqlString(2).Value;
this.User_First_Name = sr.GetSqlString(3).Value;
this.User_Last_Name = sr.GetSqlString(4).Value;
this.User_Room = sr.GetSqlString(5).Value;
return true;
}
return false;
}
public int FindIDByName(string name)
{
return 0;
}
public class UserStore
{
UserStore ustore = new UserStore();
ustore.Logged = u;
}
}
}
--------------------------------------Login.cs--------------------------------------------
namespace OfficeNotes
{
public partial class Login : Template
{
public Login()
{
InitializeComponent();
}
//once the "Go" button is clicked the following method is attepmted.
public void btn_login(object sender, EventArgs e)
{
string username = txtusername.Text;
string pin = txtPin.Text;
//creating the object user, u
User u = new User();
bool success = u.FillObject(pin, username);
//Going onto next form, MainMenu
if (success == true)
{
MainMenu main = new MainMenu();
main.LoggedUser = u;
main.Show();
Login log = new Login();
log.Close();
}
//Alert the user that there is no user under the current login.
else
{
alert_box.DrawnText = "No user";
}
}
}
}
--------------------------------------MainMenu.cs--------------------------------------------
namespace OfficeNotes
{
public partial class MainMenu : Form
{
private User _loggedUser; // variable
public User LoggedUser // encapsulating property
{
get { return _loggedUser; }
set { _loggedUser = value; }
}
public MainMenu()
{
InitializeComponent();
//Date and Date
textbar_time_date.DrawnText = DateTime.Now.ToString("hh:mmtt : dd/MMMM/yyyy");
//Display first/last name and room number from object user
name_room.DrawnText = LoggedUser.Username;
}
}
}
Here I'll show you what I mean.
There are 3 scripts here, User.cs, Login.cs and MainMenu.cs
A reply would be much appreciated, I've been trying to figure this out for 3 hours now :(
--------------------------------------User.cs--------------------------------------------
namespace OfficeNotes
{
public class User
{
//----------------------------------------------------------------------------------------
//Username string
private string _username;
public string Username
{
get { return _username; }
set { _username = value; }
}
//Pin String
private string _pin;
public string Pin
{
get { return _pin; }
set { _pin = value; }
}
//UserID String
private int _userID;
public int UserID
{
get { return _userID; }
set { _userID = value; }
}
//UserFirstname String
private string _userfirstname;
public string User_First_Name
{
get { return _userfirstname; }
set { _userfirstname = value; }
}
//UserLastName String
private string _userlastname;
public string User_Last_Name
{
get { return _userlastname; }
set { _userlastname = value; }
}
//UserRoom String
private string _userroom;
public string User_Room
{
get { return _userroom; }
set { _userroom = value; }
}
//--------------------------------------------------------------------------------
//Connects to SQL database
private SqlConnection connect;
//Constructor of "Login" also connection to the SQL database and storing details in "constring".
public User()
{
string constring = "Data Source=OLLY-PC\\SQLEXPRESS;Initial Catalog=OfficeNotes;User ID=OfficeNotes;Password=222;";
ConnectToDatabase(constring);
}
public void ConnectToDatabase(string info)
{
connect = new SqlConnection(info);
connect.Open();
}
public bool FillObject(string inPIN, string inUser){
//Filling "sql" inPin & inUser with key's from FillObject
string sql = "SELECT UserID, Username, Pin, FirstName, LastName, Room FROM NotesUser WHERE Pin = '" + inPIN + "' AND Username = '" + inUser +"'";
//Executing the application on the database
SqlCommand sqlcommand = new SqlCommand(sql,connect);
SqlDataReader sr = sqlcommand.ExecuteReader();
//Fills strings with values from database if datareader reads, the process = strings sql > sqlcommand > sr > DATA
if(sr.Read())
{
this.UserID = sr.GetSqlInt32(0).Value;
this.Username = sr.GetSqlString(1).Value;
this.Pin = sr.GetSqlString(2).Value;
this.User_First_Name = sr.GetSqlString(3).Value;
this.User_Last_Name = sr.GetSqlString(4).Value;
this.User_Room = sr.GetSqlString(5).Value;
return true;
}
return false;
}
public int FindIDByName(string name)
{
return 0;
}
public class UserStore
{
UserStore ustore = new UserStore();
ustore.Logged = u;
}
}
}
--------------------------------------Login.cs--------------------------------------------
namespace OfficeNotes
{
public partial class Login : Template
{
public Login()
{
InitializeComponent();
}
//once the "Go" button is clicked the following method is attepmted.
public void btn_login(object sender, EventArgs e)
{
string username = txtusername.Text;
string pin = txtPin.Text;
//creating the object user, u
User u = new User();
bool success = u.FillObject(pin, username);
//Going onto next form, MainMenu
if (success == true)
{
MainMenu main = new MainMenu();
main.LoggedUser = u;
main.Show();
Login log = new Login();
log.Close();
}
//Alert the user that there is no user under the current login.
else
{
alert_box.DrawnText = "No user";
}
}
}
}
--------------------------------------MainMenu.cs--------------------------------------------
namespace OfficeNotes
{
public partial class MainMenu : Form
{
private User _loggedUser; // variable
public User LoggedUser // encapsulating property
{
get { return _loggedUser; }
set { _loggedUser = value; }
}
public MainMenu()
{
InitializeComponent();
//Date and Date
textbar_time_date.DrawnText = DateTime.Now.ToString("hh:mmtt : dd/MMMM/yyyy");
//Display first/last name and room number from object user
name_room.DrawnText = LoggedUser.Username;
}
}
}
#5
Posted 17 August 2010 - 05:04 AM
Can anyone help please?
#6
Posted 17 August 2010 - 05:38 AM
MainMenu main = new MainMenu(); main.LoggedUser = u;
MainMenu constructor:
public MainMenu()
{
InitializeComponent();
//Date and Date
textbar_time_date.DrawnText = DateTime.Now.ToString("hh:mmtt : dd/MMMM/yyyy");
//Display first/last name and room number from object user
name_room.DrawnText = [COLOR="red"][B]LoggedUser.Username;[/B][/COLOR]
}
The red part accesses the loggedUser which is null at that time.Possible solution would be to pass the user as parameter to the MainMenu's constructor.
PS click the #-button for code tags ( [CODE]code here [/ CODE]) for posting code
#7
Posted 17 August 2010 - 06:57 AM
Thank's for your help :)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









