0

so i have assigned the properties to a recipe at the start of program, but after the window ends, i cannot refer to this later, because the value of strawberryrecipe.type is now null. (EDIT: so all i had to do was remove "Recipe" in Recipe strawberryrecipe = new Recipe(); since i already put it as Recipe in class level i guess)

public partial class Production_Optimization_Window : Window
{
    Ingredient strawberry;
    Recipe strawberryrecipe;



    public Production_Optimization_Window()
    {
        InitializeComponent();

       Recipe strawberryrecipe = new Recipe();
       strawberryrecipe.Type = "asd";
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show(strawberryrecipe.Type);  //Here i get the error, and blueberry recipe.type = null.
    }
humudu
  • 501
  • 5
  • 13
  • 1
    There are hundreds of question on this. Have you searched why it's happening – Ehsan Sajjad Mar 26 '15 at 14:08
  • edited it back to strawberry, sorry :) it's because i have a lot of recipes declared, just removed for simplicity in this thread – humudu Mar 26 '15 at 14:16
  • Somewhere in your class, `strawberryrecipe.Type` sets back to null – Kishore Kumar Mar 26 '15 at 14:20
  • This is where it's useful to provide a short but *complete* program demonstrating the problem - so you can work on it, check that it's still showing the error, then copy and paste it straight into SO without any other changes. – Jon Skeet Mar 26 '15 at 14:26

2 Answers2

3

You have it in two scopes... You want to remove the local scope and end up with this:

public partial class Production_Optimization_Window : Window
{
    Ingredient strawberry;
    Recipe strawberryrecipe;

    public Production_Optimization_Window()
    {
        InitializeComponent();

        // This will use the field "strawberryrecipe"
        strawberryrecipe = new Recipe();
        strawberryrecipe.Type = "asd";
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(strawberryrecipe.Type);
    }
}

Also, I have renamed blueberryrecipe to strawberryrecipe as you didn't make any reference to that in your code before the MessageBox?

Belogix
  • 7,851
  • 1
  • 23
  • 31
  • And it's strawberries, not blueberries :) – Terje Mar 26 '15 at 14:06
  • Yes, was a work in progress! :-) Hopefully that will help OP understand. Thanks for the notes though. – Belogix Mar 26 '15 at 14:08
  • Thank you very much Belogix, so the error was just "Recipe" in Recipe strawberryrecipe = new Recipe(); that should not have been there. – humudu Mar 26 '15 at 14:23
2

You have assigned instance of Recipe to strawberryrecipe variable in constructor, but trying to use blueberryrecipe variable that is unassigned.

So in Button_Click method blueberryrecipe is null - that's why you're getting exception when trying to access its field.

In fact, your code even should not compile since there is no blueberryrecipe variable declaration anywhere in your code at all.

Andy Korneyev
  • 25,238
  • 15
  • 65
  • 65
  • 2
    There's no such thing as "an instance of strawberry recipe" or "an instance of blueberryrecipe". The OP has created an instance of `Recipe`, and assigned a reference to that new instance to a local variable called `strawberryrecipe`. It's important to differentiate between variables, objects, references and types. – Jon Skeet Mar 26 '15 at 14:11
  • @JonSkeet reasonable, thank you. I've corrected the answer. – Andy Korneyev Mar 26 '15 at 14:16
  • actually the error was just the "Recipe" in Recipe strawberryrecipe = new Recipe(); – humudu Mar 26 '15 at 14:23