0

first time doing this so i hope its not too bad, but i got a really annoying error that i dont know how to fix, im trying to make a tool for a game that i play, and im trying to make a sort of save file and i want to save the contents of my combo box to a notepad but i get an error, i have tried quite alot of things, but before i didnt get an error, but it didnt write anything to the file, its really annoying me!!! Thx!

add button code

StreamWriter sw = new StreamWriter("Current.txt");
{            
    sw.WriteLine(cboCondition.SelectedValue.ToString());
    sw.Close();
}

program combo box http://gyazo.com/61c8628a0d266e5d2672fd1d9c58e22e

Error is this

An unhandled exception of type 'System.NullReferenceException' occurred in ZyTrading Tool.exe

Additional information: Object reference not set to an instance of an object.

stefankmitph
  • 3,038
  • 2
  • 16
  • 22
Zyak
  • 21
  • 3
  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Mar 02 '15 at 14:52
  • Take out the { }? (I've never coded a StreamWriter this way) – Tim Mar 02 '15 at 14:52
  • @Tim Thanks for the reply Tim, i have removed them, but i was just going as a reference from a video i watched on streamwriter as i am new to it, It didnt fix the error but ima go and have a look for more other errors like this :) – Zyak Mar 02 '15 at 14:57
  • Try sw.AppendText instead of WriteLine http://stackoverflow.com/questions/10383053/create-file-if-file-does-not-exist (your text file probably doesn't exist) – Tim Mar 02 '15 at 15:01
  • @Tim Thankyou for the help tim, i found the problem, while i was using .SelectedValue it would give me a error, but if i used SelectedItem then it did it perfectly :) – Zyak Mar 02 '15 at 15:48
  • @Tim Althought, if i do more then one item it gives me a crash, how would i go about doing 3? Do you know? – Zyak Mar 02 '15 at 15:50
  • So you'd have something like a list of items? You could loop through the selected items and write strings for each, or loop through them and build a string, then write the concatenated string in one shot. You can probably do it other ways as well, but if you're just getting started there's nothing wrong with a loop. (You should understand them before you move on to the advanced stuff) – Tim Mar 02 '15 at 15:52
  • Thanks for the help once again, i found what did wrong :3 i accidentally put selectedValue instead of item, but this is what i dont understand, i want this to be like a save and load item, i know how to write them to the notepad but what i dont understand is how to get it so that i can read them and load it into some labels, would it be possible to, lets say reference them while there in the notepad so they can be read as a combo box item? – Zyak Mar 02 '15 at 15:55

0 Answers0