From 493b0842db7a33c27d91680ce0015c59af212e0b Mon Sep 17 00:00:00 2001 From: halityurttas Date: Sat, 11 Apr 2020 13:58:21 +0300 Subject: [PATCH] Resolve null referance exception from non existing path --- CFStarter/MainForm.cs | 3 ++- CFStarter/XmlSerializationHelper.cs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CFStarter/MainForm.cs b/CFStarter/MainForm.cs index aac5d6f..10b8c03 100644 --- a/CFStarter/MainForm.cs +++ b/CFStarter/MainForm.cs @@ -45,7 +45,8 @@ private void MainForm_Load(object sender, EventArgs e) txtCfPath.Text = ConfigurationManager.AppSettings["cfpath"]; var setting = XmlSerializationHelper.Deserialize(ConfigurationManager.AppSettings["cfpath"] + "\\runtime\\conf\\server.xml"); - txtAppPath.Text = setting.Service.Engine.Host.Context.DocBase; + if (setting != null) + txtAppPath.Text = setting.Service.Engine.Host.Context.DocBase; } } diff --git a/CFStarter/XmlSerializationHelper.cs b/CFStarter/XmlSerializationHelper.cs index eb12aab..3cb2062 100644 --- a/CFStarter/XmlSerializationHelper.cs +++ b/CFStarter/XmlSerializationHelper.cs @@ -16,10 +16,17 @@ public static void Serialize(string filename, T obj) public static T Deserialize(string filename) { - XmlSerializer xs = new XmlSerializer(typeof(T)); - using (StreamReader rd = new StreamReader(filename)) + try + { + XmlSerializer xs = new XmlSerializer(typeof(T)); + using (StreamReader rd = new StreamReader(filename)) + { + return (T)xs.Deserialize(rd); + } + } + catch (System.Exception) { - return (T)xs.Deserialize(rd); + return default(T); } } }