I have a question. Somethingis wrong with my code. I have a backgroundworker in an own Form (loadScreen.cs), a Form1 and another class called "LogIn.cs" which loads certain data. Now I tried this:
public partial class loadScreen : Form
{
public Form1 form1 = null;
public LogIn logIn = null;
BackgroundWorker worker;
public loadScreen(Form1 frm1)
{
InitializeComponent();
form1 = frm1;
}
public void startWorker()
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(0);
var logIn = new LogIn(this);
logIn.checkUserInput(this);
backgroundWorker1.ReportProgress(0);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
percentage.Text = e.ProgressPercentage.ToString() + " %";
}
So when I press the "Sign In" Button in my Form1 the following happens:
private void signIn_Click(object sender, EventArgs e)
{
var ldScreen = new loadScreen(this);
if(!ldScreen.Visible)
{
ldScreen.Show();
}
else
{
ldScreen.BringToFront();
}
ldScreen.startWorker();
He should call the startWorker method which raises the DoWork Event and calls the method "checkUserInput" in my LogIn class but somehow when he enters this method he throws a NullReference Exception. What's wrong here? I don't know what object instance is missing or which one is needed here.
The code where he crashes is the following:
public String checkUserInput(object sender)
{
String val = String.Empty;
String saveFile = "currentSettings" + form1.user.Text + ".txt"; //NullReference Exception
In the LogIn class I have:
loadScreen ldScreen = null;
public LogIn(loadScreen ldScrn)
{
ldScreen = ldScrn;
}