title: WPF NET. 3.5 C# Object reference not set to an instance of an object. How do I resolve it?
body: <p>I have a project programmed in C # NET 3.5 in WPF which start very well in debug.
  Having a low level with everything that affects the "WPF" I wish I could still succeed in this project to tell me that I succeeded.
  I can not send you my <code>RootWindow.xaml.cs</code> completely because it has more than 9000 lines and it can be complicated.</p>
  
  <p>Anyway, my problem occurs when I click on a button in my project, <strong>Visual Studio</strong> sends me back to the line that tells me <code>null</code> but I do not know how to remedy this very frequent error according to my research.</p>
  
  <p>Here is the error in question:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.NullReferenceException' occurred in EWIN_THEME_MAKER.exe. </p>
    
    <p>Additional information: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>So, when I click on the button, the object that is called <code>null</code> is highlighted by VS, so this is:</p>
  
  <pre><code> public void StartPreview_Theme()
      {
          this.setTheme();
          this.previewMaker.MakePreview(ref this.theme);
      }
  </code></pre>
  
  <blockquote>
    <p>this.previewMaker = null</p>
  </blockquote>
  
  <p>Yet I refer my class well at the very beginning of my class <code>RootWindow.xaml.cs</code></p>
  
  <pre><code>    private PreviewMaker previewMaker;
  </code></pre>
  
  <p>So I searched in my <code>PreviewMaker</code> class but I do not see what might be<code>null</code>. Especially since it seems complicated because several classes are referenced in the end. There are: <code>PreviewMaker</code>, <code>Theme</code>, <code>LayoutManager</code> </p>
  
  <p>In my <code>PreviewMaker</code> this code corresponds to the action that happens when I click the button:</p>
  
  <pre><code> public void MakePreview(ref Theme theme)
      {
          this.layoutManager.rootWindow = this.rootWindow;
          this.layoutManager.setThemeInfo(theme);
          if (this.layoutManager.changeLayout())
          {
              this.ConvertLayout(ref theme);
              this.ReloadPreviewImage();
              theme.IsFirstConvert = false;
              theme.setChangeFix();
          }
          else
          {
              MessageBox.Show(this.rootWindow, Resources.Dialog_Err_Convert, Resources.Dialog_Err_Header);
          }
      }
  </code></pre>
  
  <p>I also point out that there is an error in this line, which says <code>null</code>:</p>
  
  <pre><code>MessageBox.Show(this.rootWindow, Resources.Dialog_Err_Convert, Resources.Dialog_Err_Header);
  </code></pre>
  
  <p>Where could this problem come from?</p>
  
  <p>I do not send my classes because they are too long, nevertheless if it is necessary I can put it if it can help me and help others.</p>
  
  <p><code>PreviewMaker.cs:</code></p>
  
  <pre><code>    using System;
  using System.Diagnostics;
  using System.IO;
  using System.Runtime.InteropServices;
  using System.Threading;
  using System.Windows;
  using EWIN_THEME_MAKER.Properties;
  using EWIN_THEME_MAKER.ThemeMaker.Views;
  
  namespace EWIN_THEME_MAKER.ThemeMaker
  {
      public class PreviewMaker
      {
          private static readonly IntPtr HWND_TOP = new IntPtr(0);
          private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
          private LayoutManager layoutManager = new LayoutManager();
          private const uint SWP_NOSIZE = 1;
          private const uint SWP_NOMOVE = 2;
          private const int WM_QUIT = 18;
          private const int WM_COPYDATA = 74;
          private string convertPath;
          private string lytviewerPath;
          private string layoutFolderPath;
          private string layoutDName;
          private string layoutDPlayName;
          private string layoutUName;
          private string layoutUPlayName;
          private string outputDFolderPath;
          private string outputDPlayFolderPath;
          private string outputUFolderPath;
          private string outputUPlayFolderPath;
          public RootWindow rootWindow;
          private IntPtr hWnd;
  
          public PreviewMaker()
          {
              this.convertPath =  ThemeBinaryConverter.App.tmakeRootPath + "tools\\LayoutConverter\\NW4C_LayoutConverter.exe";
              this.lytviewerPath =  ThemeBinaryConverter.App.tmakeRootPath + "tools\\lytviewer\\lytviewer.exe";
              this.layoutFolderPath =  ThemeBinaryConverter.App.tmakeRootPath + "resources\\clyt\\Menu\\layout_out\\";
              this.layoutDName = this.layoutFolderPath + "DummyMenu_D_00";
              this.layoutDPlayName = this.layoutFolderPath + "DummyMenu_D_00_play";
              this.layoutUName = this.layoutFolderPath + "DummyMenu_U_00";
              this.layoutUPlayName = this.layoutFolderPath + "DummyMenu_U_00_play";
              string tempPath = Path.GetTempPath();
              this.outputDFolderPath = tempPath + "menu_d";
              this.outputDPlayFolderPath = tempPath + "menu_anim_d";
              this.outputUFolderPath = tempPath + "menu_u";
              this.outputUPlayFolderPath = tempPath + "menu_anim_u";
          }
  
          private void ExecConvertLayout(string inputCLYTFilePath, string inputCLANFilePath, string outputFolderPath)
          {
              string str = "-u -g --bake-infinity \"" + inputCLYTFilePath + "\" \"" + inputCLANFilePath + "\" \"" + outputFolderPath + "\"";
              Stopwatch stopwatch = new Stopwatch();
              stopwatch.Start();
              Util.Execute(this.convertPath, str, true);
              stopwatch.Stop();
              Console.Write("{0} コンパイル時間(ms)", (object)outputFolderPath);
              Console.WriteLine(" . . . {0}", (object)stopwatch.ElapsedMilliseconds);
          }
  
          private void ConvertUpLayout()
          {
              this.ExecConvertLayout(this.layoutUName + ".clyt", this.layoutUName + ".clan", this.outputUFolderPath);
              this.ExecConvertLayout(this.layoutUPlayName + ".clyt", this.layoutUPlayName + ".clan", this.outputUPlayFolderPath);
          }
  
          private void ConvertDownLayout()
          {
              this.ExecConvertLayout(this.layoutDName + ".clyt", this.layoutDName + ".clan", this.outputDFolderPath);
              this.ExecConvertLayout(this.layoutDPlayName + ".clyt", this.layoutDPlayName + ".clan", this.outputDPlayFolderPath);
          }
  
          private void ConvertLayout(ref Theme theme)
          {
              Stopwatch stopwatch = new Stopwatch();
              stopwatch.Start();
              WaitWindow waitWindow = new WaitWindow();
              waitWindow.Owner = Application.Current.MainWindow;
              waitWindow.Show();
              waitWindow.setPercentLabel("Wait：0/2");
              if (theme.IsFirstConvert || theme.IsDownChanged)
                  this.ConvertDownLayout();
              waitWindow.setPercentLabel("Wait：1/2");
              if (theme.IsFirstConvert || theme.IsUpChanged)
                  this.ConvertUpLayout();
              waitWindow.setPercentLabel("Wait：2/2");
              waitWindow.Close();
              stopwatch.Stop();
              Console.WriteLine("Please Wait(ms) = {0}", (object)stopwatch.ElapsedMilliseconds);
          }
  
          [DllImport("user32.dll", CharSet = CharSet.Auto)]
          private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  
          [DllImport("user32.dll", CharSet = CharSet.Auto)]
          private static extern uint SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref PreviewMaker.COPYDATASTRUCT lParam);
  
          [DllImport("user32.dll", CharSet = CharSet.Auto)]
          public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  
          [DllImport("user32.dll", SetLastError = true)]
          public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
  
          [DllImport("user32.dll")]
          [return: MarshalAs(UnmanagedType.Bool)]
          private static extern bool SetForegroundWindow(IntPtr hWnd);
  
          [DllImport("user32.dll")]
          [return: MarshalAs(UnmanagedType.Bool)]
          private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint flags);
  
          [DllImport("user32.dll")]
          private static extern int GetWindowRect(IntPtr hwnd, ref PreviewMaker.RECT lpRect);
  
          public void OpenPreview()
          {
              this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
              if (!this.hWnd.Equals((object)IntPtr.Zero))
                  return;
              try
              {
                  new Process()
                  {
                      StartInfo = {
                          FileName = this.lytviewerPath,
                          Arguments = (" --upper \"" + this.outputUFolderPath + "\" --lower \"" + this.outputDFolderPath + "\"")
                      }
                  }.Start();
                  Thread.Sleep(1000);
                  this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
              }
              catch (Exception ex)
              {
                  int num = (int)MessageBox.Show((Window)this.rootWindow, ex.ToString(), Resources.Dialog_Err_Header);
              }
          }
  
          public void ActivePreview()
          {
              this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
              if (this.hWnd.Equals((object)IntPtr.Zero))
                  return;
              PreviewMaker.SetForegroundWindow(this.hWnd);
          }
  
          public void ReloadPreviewWithDynamicLayout()
          {
              this.OpenPreview();
              PreviewMaker.COPYDATASTRUCT lParam = new PreviewMaker.COPYDATASTRUCT();
              PreviewMaker.LayoutProtocol layoutProtocol;
              layoutProtocol.Verb = "Open";
              layoutProtocol.Data = " -t upper " + this.outputUPlayFolderPath + " -t lower " + this.outputDPlayFolderPath;
              IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf((object)layoutProtocol));
              Marshal.StructureToPtr((object)layoutProtocol, ptr, false);
              lParam.lpData = ptr;
              lParam.cbData = 2112U;
              PreviewMaker.SendMessage(this.hWnd, 74, this.hWnd, ref lParam);
          }
  
          public void ReloadPreviewImage()
          {
              this.OpenPreview();
              PreviewMaker.COPYDATASTRUCT lParam = new PreviewMaker.COPYDATASTRUCT();
              PreviewMaker.LayoutProtocol layoutProtocol;
              layoutProtocol.Verb = "Open";
              layoutProtocol.Data = " -t upper " + this.outputUFolderPath + " -t lower " + this.outputDFolderPath;
              IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf((object)layoutProtocol));
              Marshal.StructureToPtr((object)layoutProtocol, ptr, false);
              lParam.lpData = ptr;
              lParam.cbData = 2112U;
              PreviewMaker.SendMessage(this.hWnd, 74, this.hWnd, ref lParam);
          }
  
          public void ChangePreviewParam(string paramData)
          {
              this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
              if (this.hWnd.Equals((object)IntPtr.Zero))
                  this.ReloadPreviewImage();
              PreviewMaker.COPYDATASTRUCT lParam = new PreviewMaker.COPYDATASTRUCT();
              PreviewMaker.LayoutProtocol layoutProtocol;
              layoutProtocol.Verb = "Change";
              layoutProtocol.Data = paramData;
              IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf((object)layoutProtocol));
              Marshal.StructureToPtr((object)layoutProtocol, ptr, false);
              lParam.lpData = ptr;
              lParam.cbData = 2112U;
              PreviewMaker.SendMessage(this.hWnd, 74, this.hWnd, ref lParam);
              int lpdwProcessId;
              PreviewMaker.GetWindowThreadProcessId(this.hWnd, out lpdwProcessId);
              Interaction.AppActivate(lpdwProcessId);
          }
  
          public void ChangePreviewAnimationParam(int animParam)
          {
              this.ChangePreviewParam(" -anim " + (object)animParam);
          }
  
          public void ChangePreviewVisiblePaneParam(string paneName)
          {
              this.ChangePreviewParam(" -panevisible N_BaseCtr_00");
          }
  
          public void ChangePreviewPositionPaneParam(string paneName, int x, int y)
          {
              this.ChangePreviewParam(" -panetrans " + paneName + " " + (object)x + " " + (object)y);
          }
  
          public void MakePreview(ref Theme theme)
          {
              this.layoutManager.rootWindow = this.rootWindow;
              this.layoutManager.setThemeInfo(theme);
              if (this.layoutManager.changeLayout())
              {
                  this.ConvertLayout(ref theme);
                  this.ReloadPreviewImage();
                  theme.IsFirstConvert = false;
                  theme.setChangeFix();
              }
              else
              {
                  int num = (int)MessageBox.Show((Window)this.rootWindow, Resources.Dialog_Err_Convert, Resources.Dialog_Err_Header);
              }
          }
  
          public void ClosePreview()
          {
              this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
              if (this.hWnd.Equals((object)IntPtr.Zero))
                  return;
              PreviewMaker.RECT lpRect = new PreviewMaker.RECT();
              PreviewMaker.GetWindowRect(this.hWnd, ref lpRect);
              Settings.Default.lytviewerX = (double)lpRect.left;
              Settings.Default.lytviewerY = (double)lpRect.top;
              PreviewMaker.PostMessage(this.hWnd, 18, 0, 0);
          }
  
          public void CaptureReportImage(ref Theme theme, string captureFolderPath)
          {
              int num = theme.RecommendRowNum &lt; eRecommendRowType.Row1 || theme.RecommendRowNum &gt; eRecommendRowType.Row6 ? 11 : 5 + 7 * (int)(theme.RecommendRowNum - 1) + 6;
              this.ClosePreview();
              string str = "" + " --upper \"" + this.outputUFolderPath + "\"" + " --lower \"" + this.outputDFolderPath + "\"" + " --screenshot " + (object)num + " 0 \"" + captureFolderPath + "\"";
              Process process = new Process();
              process.StartInfo.FileName = this.lytviewerPath;
              process.StartInfo.Arguments = str;
              process.Start();
              process.WaitForExit();
              this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
          }
  
          public void Capture(string captureFolderPath)
          {
              this.Capture(captureFolderPath, 0);
          }
  
          public void Capture(string captureFolderPath, int animationNum)
          {
              this.ClosePreview();
              string str1 = "";
              if (Settings.Default.CaptureTypeStr == "Both")
                  str1 = str1 + " --upper \"" + this.outputUFolderPath + "\"" + " --lower \"" + this.outputDFolderPath + "\"";
              else if (Settings.Default.CaptureTypeStr == "Up")
                  str1 = str1 + " --upper \"" + this.outputUFolderPath + "\"";
              else if (Settings.Default.CaptureTypeStr == "Down")
                  str1 = str1 + " --lower \"" + this.outputDFolderPath + "\"";
              string str2 = str1 + " --screenshot " + (object)animationNum + " 0 \"" + captureFolderPath + "\"";
              Process process = new Process();
              process.StartInfo.FileName = this.lytviewerPath;
              process.StartInfo.Arguments = str2;
              process.Start();
              process.WaitForExit();
              this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
          }
  
          private struct RECT
          {
              public int left;
              public int top;
              public int right;
              public int bottom;
          }
  
          public struct COPYDATASTRUCT
          {
              public IntPtr dwData;
              public uint cbData;
              public IntPtr lpData;
          }
  
          public struct LayoutProtocol
          {
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
              public string Verb;
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
              public string Data;
          }
      }
  }
  </code></pre>