-1

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;
    }
Takeda15
  • 35
  • 1
  • 10
  • I know what a NullReference Exception is but I want to know what's wrong especially in my case. Do I need to create an instance of an object somewhere? – Takeda15 Mar 17 '15 at 10:32
  • 2
    On which line exactly are you getting an error? And you will need to create and instance of an object yes, question is just which object, which will be determined by debugging – Bernd Linde Mar 17 '15 at 10:34
  • I get it when it enters the "checkUserInput" method and there on the first line when a String gets declared (input from a form1 Textbox is saved to a String variable) – Takeda15 Mar 17 '15 at 10:36
  • I agree with @BerndLinde. If I had to guess I'd say `backgroundWorker1` is not initilized, because I don't see the declaration. – Binkan Salaryman Mar 17 '15 at 10:36
  • 1
    Please show the code for "checkUserInput". Also check that the parameter `this` has a value when it is being passed into "checkUserInput" – Bernd Linde Mar 17 '15 at 10:38
  • Have you checked that `this` has a value when it is being passed into "checkUserInput" and that `form1` and `form1.user` has a value when they are being used? – Bernd Linde Mar 17 '15 at 10:49
  • `form1`, `form1.user` or `form1.user.Text` is null. If you use VisualStudio as IDE, it should pop up a debug hint at the null reference object. You might drop a breakpoint at the line, add null-checks (with `if`-`else`) or write some `Console.WriteLine(object)` statements... – Binkan Salaryman Mar 17 '15 at 10:50
  • With the Debugger I took a look at the "object sender" and it comes from my backgroundWorker Form. I can't read out if the user.Text is null because he has to skip this line but he crashed there. When I call the method without the worker (directly over Form1 then everything is fine) - UPDATE: I can't see the other variables but he shows me that form1 is null. I don't know if that changes when he would read the line – Takeda15 Mar 17 '15 at 10:58
  • Ok it's the form1 that is empty. I tried to create var form1 = new Form1(); in my checkUserInput method and then the Textbox String is empty. What to do? – Takeda15 Mar 17 '15 at 11:16
  • If you create the form again, naturally the Textbox will be empty since it is not the same form as what the user saw. I recommend you read up on parameter passing and using the background worker with user arguments. – Bernd Linde Mar 17 '15 at 11:36
  • I'm very new to C#. Where to find that or can you show me how to do it in my example? – Takeda15 Mar 17 '15 at 11:42
  • Ok I posted this in another question – Takeda15 Mar 17 '15 at 12:20

2 Answers2

1

I'm guessing that this is not the actual code because you define a 'worker' attribute of type 'BackgroundWorker' but never assign it to an actual instance but then you reference a variable 'backgroundWorker1' that does not appear to be initialised (or defined).

I think the answer to you question is that 'backgroundWorker1' is null when it is accessed within the 'startWorker' method. Add the following to resolve:

BackgroundWorker backgroundWorker1;

public void startWorker()
{
    backgroundWorker1 = new BackgroundWorker();
    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
}

For more info see: http://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners

karmasponge
  • 994
  • 2
  • 11
  • 24
0

if you want to only access user.text from form1. Then you can create static string property in form1 which returns user.text (with get{} modifier only.) Set this prooperty in Textchanged event of user textbox.

//Create static property in form1

 private static string struserInput = "";

        public static string UserInput
        {
            get { return struserInput; }
        }

//assign value on textchanged event

private void user_TextChanged(object sender, EventArgs e)
{
      struserInput = user.Text;
}

//To use in CheckUserInput access as form1.UserInput (classname.variable name) instead of form1.user.text

public String checkUserInput(object sender)
       {

                    String val = String.Empty; 

                    String saveFile = "currentSettings" + form1.UserInput + ".txt";
        }