0
public applybuttonform(string s)
{
    InitializeComponent();
    passingid.Text = s;   
}

When I try to do this I get exception

NullReferenceEexception was unhandled

public applybuttonform(string s)
{
    InitializeComponent();
    if(string.IsNullOrEmpty(s))
    {
       passingid.Text = s;
    }         
}

If i do this i don't get an error but this doesn't solve my problem as i have to use that string 's' which was passed from another form. So, what can i do now?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Arnab Rahman
  • 1,003
  • 1
  • 9
  • 15
  • 1
    `InitializeComponent` in `applybuttonform`? Also, if `s` is null (or empty), why do you want to assign it to `passingid.Text` at all? – Tim Schmelter Jul 24 '13 at 21:10
  • What is 'passingid.Text = s;'? Is Text a property that cannot handle nulls? Can you show some more code related to that? – PostureOfLearning Jul 24 '13 at 21:11
  • 1
    You have to debug `passingid`. Since you haven't shown us what it is, we can't do it for you. Start by reading http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – Dour High Arch Jul 24 '13 at 21:12
  • Post some more code that actually contains declaration of `passingid` – Shumail Jul 24 '13 at 21:13

3 Answers3

4

Your passingid variable is null. The error goes away in the second version, because you are actually making sure that s is null or empty, which it isn't, so that assigning to passingid.Text doesn't happen.

TextBox passingid;
passingid.Text = s;

will produce your error, since passingid is null at that point.

TextBox passingid = new TextBox();
passingid.Text = s;

will work fine.

Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45
  • passingid is a textbox variable.how can i assign that string 's' to a textbox variable without getting nullreferenceexception? – Arnab Rahman Jul 24 '13 at 21:21
  • 1
    Your `passingid` textbox variable is not properly initialized. See my edit. – Jan Dörrenhaus Jul 24 '13 at 21:23
  • But why can't i use that passingid outside the public applybuttonform(string s) constructor – Arnab Rahman Jul 24 '13 at 21:29
  • Because it is a variable inside your constructor. Variables only live in their scope. Place your passingid textbox in your class scope instead of in your constructor scope, and you can use it anywhere in your class. – Jan Dörrenhaus Jul 24 '13 at 21:31
  • i want to use the passingid inside private void register_button_Click(object sender, EventArgs e){}. How can i do that? – Arnab Rahman Jul 24 '13 at 21:51
1

Well, you probably want

if(!string.IsNullOrEmpty(s))
{
   passingid.Text = s;
} 

But that's just a guess. You might want to edit your question with the entirety of the exception, including which statement is generating the exception.

Reacher Gilt
  • 1,813
  • 12
  • 26
0

Consider below code:

public class PassingID2
{
    private string _Text = null;
    public string Text
    {
        get { return _Text; }
        set { if (_Text.Length <= 0) _Text = value; }
    }
}
public class PassingID1
{
    public string Text { get; set; }
}

Then somewhere else in the code:

string s = string.Empty;

PassingID1 passingid1;
passingid1.Text = s; //This will cause a NullReferenceException because passingid1 is null

PassingID2 passingid2 = new PassingID2();
passingid2.Text = s; //This will cause a NullReferenceException because _Text is null

As you can see, both cases will cause the exception.

Also note that the problem is not "s"

PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44
  • I would call such an API *deliberately unstable*. And the WinForms TextBox surely does **not** behave like that. – Jan Dörrenhaus Jul 24 '13 at 21:39
  • Of course it is deliberate. I am simply illustrating two different scenarios that throw the exception in question. This didn't seem to be understood by the poster. Also, I didn't see the question referring to WinForms. – PostureOfLearning Jul 24 '13 at 21:46