WPF NET. 3.5 C # Object reference not set to object instance. How can I solve it?

I have a project programmed in C # NET 3.5 in WPF that starts out very well when debugging. Having a low level with everything that affects "WPF" I wish I could be successful on this project, to tell me that I was successful. I cannot send you mine in RootWindow.xaml.cs

full because it has over 9000 lines and it can be tricky.

Anyway, my problem occurs when I click a button in my project, Visual Studio sends me back to the line that tells me null

, but I don't know how to fix this very common error according to my research.

Here is the error in question:

An unhandled exception of type "System.NullReferenceException" occurred at EWIN_THEME_MAKER.exe.

Additional information: Object reference not set to object instance.

So, when I click on the button, the object that is called is null

highlighted by VS, so this:

 public void StartPreview_Theme()
    {
        this.setTheme();
        this.previewMaker.MakePreview(ref this.theme);
    }

      

this.previewMaker = null

However, I have a very good attitude towards my class at the very beginning of my class RootWindow.xaml.cs

    private PreviewMaker previewMaker;

      

So, I have searched in my class PreviewMaker

, but I don't see what could be null

. Moreover, it seems complicated because several classes are mentioned at the end. There are: PreviewMaker

, Theme

,LayoutManager

In mine, PreviewMaker

this code corresponds to the action that happens when I click the button:

 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);
        }
    }

      

I also point out that there is an error on this line that says null

:

MessageBox.Show(this.rootWindow, Resources.Dialog_Err_Convert, Resources.Dialog_Err_Header);

      

Where did this problem come from?

I am not posting my classes because they are too long, however if needed I can tell if he can help me and help others.

PreviewMaker.cs:

    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 < eRecommendRowType.Row1 || theme.RecommendRowNum > 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;
        }
    }
}

      

+3


source to share


2 answers


Make sure you create an instance of the class PreviewMaker

i.e. try changing this line to RootWindow.xaml.cs

:

private PreviewMaker previewMaker;

      



... to that:

private PreviewMaker previewMaker = new PreviewMaker();

      

+2


source


you declared previewMaker, but you don't initialize it before trying to access it.

In other words:



private PreviewMaker previewMaker;

      

will cause previewMaker to be null.
this line does NOT create an object of type PreviewMaker

+1


source







All Articles