wpf - Object reference not set to an instance of an object during xml serialization and problem with selection of combobox item upon loading -


question 1. have issue of "object reference not set instance of object" when majorlabel empty , occurs after try save button click on xml serialization. how can fix this?

private void savebutton_click(object sender, routedeventargs e) {     string savepath;     savefiledialog dialogsave = new savefiledialog();     // default file extension     dialogsave.defaultext = "txt";     // available file extensions     dialogsave.filter = "xml file (*.xml)|*.xml|all files (*.*)|*.*";     // adds extension if user not     dialogsave.addextension = true;     // restores selected directory, next time     dialogsave.restoredirectory = true;     // dialog title     dialogsave.title = "where want save file?";     // startup directory     dialogsave.initialdirectory = @"c:/";     dialogsave.showdialog();     savepath = dialogsave.filename;     dialogsave.dispose();     dialogsave = null;      formsaving abc = new formsaving();     if (!string.isnullorempty(majorversionresultlabel.content.tostring()))     {         abc.majorversion = majorversionresultlabel.content.tostring();     }     abc.startzbuildfrom = startzbuildcombobox.selecteditem.tostring();      using (stream savestream = new filestream(savepath, filemode.create))     {         xmlserializer serializer = new xmlserializer(typeof(formsaving));         serializer.serialize(savestream, abc);     } } 

as recommended, here line of error:

        if (!string.isnullorempty(majorversionresultlabel.content.tostring()))     {         abc.majorversion = majorversionresultlabel.content.tostring();     } 

question 2. used line save combo box selection:

abc.startzbuildfrom = startzbuildcombobox.selecteditem.tostring(); 

and in load have line:

startzbuildcombobox.selecteditem = abc.startzbuildfrom 

why wont select combobox selection previously?

i suspect majorversionresultlabel null, or majorversionresultlabel.content null. statement

if (!string.isnullorempty(majorversionresultlabel.content.tostring())) 

will throw nullreferenceexception. try instead:

if (majorversionresultlabel != null && majorversionresultlabel.content != null && majorversionlabel.content.tostring() != string.empty) 

i bet nullreferenceexception go away.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -