C
C#2y ago
thejobe

✅ object reference not set to an instance of an object (beginner)

Hello. I am working on a Registration / login system for a bigger project. I am getting an exception thrown when it hits the login portion of the createuser class. I have a bunch of other issues to solve but this is the current problem I am working to solve. if anyone has any clues please feel free to tear this apart. Thanks in advance for the assistance.
namespace StartMenu
{
internal class Program
{
private static createuser newUser;

public static void mainMenu()
{


Console.WriteLine("Are you currently a Member?");
Console.WriteLine("if you are not a member please select Register below.");
Console.WriteLine("1: Register");
Console.WriteLine("2: Login");


int select = int.Parse(Console.ReadLine());

if (select == 1)
{
createuser newUser = new createuser();


}
else if (select == 2)
{
createuser.Login(newUser);

}

}

public static void userMenu()
{
var select = int.Parse (Console.ReadLine());
Console.WriteLine("You are now in the system. what would you like to do?");
Console.WriteLine("1: look at my information.");
Console.WriteLine("2: change my password");
Console.WriteLine("3: log out.");

switch (select)
{
case 1: createuser.userInfo(newUser);
break;
case 2: createuser.changePass(newUser);
break;
}
}



static void Main(string[] args)
{
Console.WriteLine("Hello! Welcome to the world Database system.");
Console.WriteLine("please select an option from the list below.");
while (true)
{
mainMenu();


}
}
}
}
namespace StartMenu
{
internal class Program
{
private static createuser newUser;

public static void mainMenu()
{


Console.WriteLine("Are you currently a Member?");
Console.WriteLine("if you are not a member please select Register below.");
Console.WriteLine("1: Register");
Console.WriteLine("2: Login");


int select = int.Parse(Console.ReadLine());

if (select == 1)
{
createuser newUser = new createuser();


}
else if (select == 2)
{
createuser.Login(newUser);

}

}

public static void userMenu()
{
var select = int.Parse (Console.ReadLine());
Console.WriteLine("You are now in the system. what would you like to do?");
Console.WriteLine("1: look at my information.");
Console.WriteLine("2: change my password");
Console.WriteLine("3: log out.");

switch (select)
{
case 1: createuser.userInfo(newUser);
break;
case 2: createuser.changePass(newUser);
break;
}
}



static void Main(string[] args)
{
Console.WriteLine("Hello! Welcome to the world Database system.");
Console.WriteLine("please select an option from the list below.");
while (true)
{
mainMenu();


}
}
}
}
12 Replies
thejobe
thejobeOP2y ago
Second page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartMenu
{
internal class createuser
{
private string userName { get; set; }
private string password { get; set; }



public createuser()
{

Console.WriteLine("Hello welcome to the register system");
Console.WriteLine("Please enter your user name");
userName = Console.ReadLine();
createpassword();
}

public static void Login(createuser newUser)
{

string userinput;
string passwordinput;

Console.WriteLine("Hello. To login please enter your username.");
userinput = Console.ReadLine();
Console.WriteLine("Please enter your password.");
passwordinput = Console.ReadLine();

if (userinput == newUser.userName && passwordinput == newUser.password)
{
Console.WriteLine($"System access granted. Hello {newUser.userName}");
}
else if(userinput == newUser.userName && passwordinput != newUser.password)
{
Console.WriteLine("Incorrect password.");
}
else
{
Console.WriteLine("Sorry that user does not exist");
}

}


private void createpassword()
{
Console.WriteLine("Please enter a password.");
password = Console.ReadLine();
Console.Clear();
string newpassword = new string('*', password.Length);
Console.WriteLine($"Your Username: {userName}");
Console.WriteLine($"Your password: {newpassword}");
Console.WriteLine("Please press any key to continue.");
Console.ReadKey();
Console.Clear();
Program.mainMenu();

}

public static void changePass(createuser newUser)
{
Console.WriteLine("please type your old password.");
var passcha = Console.ReadLine();
if (passcha == newUser.password)
{
Console.WriteLine("Please type your new Password");
string newpass = Console.ReadLine();
newUser.password = newpass;
Console.WriteLine($"Your new password is {newUser.password}");
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
Console.Clear();
Program.userMenu();
}

}

public static void userInfo(createuser newUser)
{

string newpassword = new string('*', newUser.password.Length);
Console.WriteLine($"Your User Name is: {newUser.userName}");
Console.WriteLine($"Your Password is: {newpassword}");
Console.WriteLine("Press any Key to return to menu.");
Console.ReadKey();
}


}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartMenu
{
internal class createuser
{
private string userName { get; set; }
private string password { get; set; }



public createuser()
{

Console.WriteLine("Hello welcome to the register system");
Console.WriteLine("Please enter your user name");
userName = Console.ReadLine();
createpassword();
}

public static void Login(createuser newUser)
{

string userinput;
string passwordinput;

Console.WriteLine("Hello. To login please enter your username.");
userinput = Console.ReadLine();
Console.WriteLine("Please enter your password.");
passwordinput = Console.ReadLine();

if (userinput == newUser.userName && passwordinput == newUser.password)
{
Console.WriteLine($"System access granted. Hello {newUser.userName}");
}
else if(userinput == newUser.userName && passwordinput != newUser.password)
{
Console.WriteLine("Incorrect password.");
}
else
{
Console.WriteLine("Sorry that user does not exist");
}

}


private void createpassword()
{
Console.WriteLine("Please enter a password.");
password = Console.ReadLine();
Console.Clear();
string newpassword = new string('*', password.Length);
Console.WriteLine($"Your Username: {userName}");
Console.WriteLine($"Your password: {newpassword}");
Console.WriteLine("Please press any key to continue.");
Console.ReadKey();
Console.Clear();
Program.mainMenu();

}

public static void changePass(createuser newUser)
{
Console.WriteLine("please type your old password.");
var passcha = Console.ReadLine();
if (passcha == newUser.password)
{
Console.WriteLine("Please type your new Password");
string newpass = Console.ReadLine();
newUser.password = newpass;
Console.WriteLine($"Your new password is {newUser.password}");
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
Console.Clear();
Program.userMenu();
}

}

public static void userInfo(createuser newUser)
{

string newpassword = new string('*', newUser.password.Length);
Console.WriteLine($"Your User Name is: {newUser.userName}");
Console.WriteLine($"Your Password is: {newpassword}");
Console.WriteLine("Press any Key to return to menu.");
Console.ReadKey();
}


}
}
Henkypenky
Henkypenky2y ago
why are you doing this?
private static createuser newUser;
private static createuser newUser;
ero
ero2y ago
you never set that static createuser newUser to anything
thejobe
thejobeOP2y ago
Oh I think visual studio did that as a potential fix one time. Forgot it was there. Is that the issue?
ero
ero2y ago
Yes
if (select == 1)
{
createuser newUser = new createuser();
}
else if (select == 2)
{
createuser.Login(newUser);
}
if (select == 1)
{
createuser newUser = new createuser();
}
else if (select == 2)
{
createuser.Login(newUser);
}
this right here in the if block, you declare a local variable called newUser. this variable is never used and forgotten about in the else if block, you use the private static createuser newUser variable you have in your class. this is never set to anything and remains null these things are easily fixed when just conforming to c# naming conventions and looking at the colors of the variables in your editor
thejobe
thejobeOP2y ago
Wait that is local? So how do i make it usable elsewhere? I thought this is how you create the class object.
ero
ero2y ago
just
if (select == 1)
{
newUser = new createuser();
}
else if (select == 2)
{
createuser.Login(newUser);
}
if (select == 1)
{
newUser = new createuser();
}
else if (select == 2)
{
createuser.Login(newUser);
}
thejobe
thejobeOP2y ago
Ahhh ok I see. I will have to work on this. Thanks. Do you see any other issues I might run into with the structure I have now? hey Ero. I tried it with newUser = new createuser(); and there is no context for it to work. would I have to create the variable of newUser first for it to work? but then it wouldnt be a class object anymore. right?
ero
ero2y ago
No that definitely should work Otherwise the else if block would fail too
Anton
Anton2y ago
what's a class object?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
thejobe
thejobeOP2y ago
yes I was able to resolve the issue. Thank you for your help
Want results from more Discord servers?
Add your server