locked
C# System.NullReferenceException RRS feed

  • Question

  • Hello everyone

    I have a problem and I don't know what to do with it, none of the answers worked.

    As the title says, it's a System.NullReferenceException

    Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Game
    {
        class Launcher:Form
        {
            private static IniFile inif = new IniFile("game.ini");
    
            [DllImport("user32.dll")]
            public static extern bool EnumDisplaySettings(
                  string deviceName, int modeNum, ref DEVMODE devMode);
            const int ENUM_CURRENT_SETTINGS = -1;
            private ListBox listBox1;
            private Button button1;
    
            const int ENUM_REGISTRY_SETTINGS = -2;
    
            [StructLayout(LayoutKind.Sequential)]
            public struct DEVMODE
            {
    
                private const int CCHDEVICENAME = 0x20;
                private const int CCHFORMNAME = 0x20;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
                public string dmDeviceName;
                public short dmSpecVersion;
                public short dmDriverVersion;
                public short dmSize;
                public short dmDriverExtra;
                public int dmFields;
                public int dmPositionX;
                public int dmPositionY;
                public ScreenOrientation dmDisplayOrientation;
                public int dmDisplayFixedOutput;
                public short dmColor;
                public short dmDuplex;
                public short dmYResolution;
                public short dmTTOption;
                public short dmCollate;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
                public string dmFormName;
                public short dmLogPixels;
                public int dmBitsPerPel;
                public int dmPelsWidth;
                public int dmPelsHeight;
                public int dmDisplayFlags;
                public int dmDisplayFrequency;
                public int dmICMMethod;
                public int dmICMIntent;
                public int dmMediaType;
                public int dmDitherType;
                public int dmReserved1;
                public int dmReserved2;
                public int dmPanningWidth;
                public int dmPanningHeight;
    
            }
    
            public static void Main(string[] args)
            {
                using (Launcher launchf = new Launcher())
                {
                    launchf.LoadData();
                    launchf.Show();
                }
            }
    
            private void LoadData()
            {
                List<string> reslist = new List<string>();
                DEVMODE vDevMode = new DEVMODE();
                int i = 0;
                while (EnumDisplaySettings(null, i, ref vDevMode))
                {
                    reslist.Add(vDevMode.dmPelsWidth + "x" + vDevMode.dmPelsHeight);
                    i++;
                }
                listBox1.DataSource = reslist;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                inif.DeleteKey("resx", "Display");
                inif.DeleteKey("resy", "Display");
                string res = listBox1.GetItemText(listBox1.SelectedItem);
                string resx = res.Split('x').First();
                string resy = res.Split('y').Last();
                inif.Write("resx", resx, "Display");
                inif.Write("resy", resy, "Display");
            }
    
            private void InitializeComponent()
            {
                this.listBox1 = new System.Windows.Forms.ListBox();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // listBox1
                // 
                this.listBox1.FormattingEnabled = true;
                this.listBox1.Location = new System.Drawing.Point(492, 12);
                this.listBox1.Name = "listBox1";
                this.listBox1.Size = new System.Drawing.Size(120, 199);
                this.listBox1.TabIndex = 0;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(537, 406);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 2;
                this.button1.Text = "Save";
                this.button1.UseVisualStyleBackColor = true;
                // 
                // Launcher
                // 
                this.ClientSize = new System.Drawing.Size(624, 441);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.listBox1);
                this.Name = "Launcher";
                this.ResumeLayout(false);
    
            }
        }
    }
    

    The exception is trown at "listBox1.DataSource = reslist;", while the array reslist has 63 members.

    Can someone please help me with it?

    • Moved by Amanda Zhu Friday, November 29, 2013 8:54 AM C# more related
    Thursday, November 28, 2013 7:44 PM

Answers

  • reslist cannot be inside the function.  It must be a static variable outside the function.  When a variable is inside a function it will get dispose as soon as the function completes so the listbox will never seen that data. 

               List<string> reslist = new List<string>();
     
           private void LoadData()
            {
                DEVMODE vDevMode = new DEVMODE();
                int i = 0;
                while (EnumDisplaySettings(null, i, ref vDevMode))
                {
                    reslist.Add(vDevMode.dmPelsWidth + "x" + vDevMode.dmPelsHeight);
                    i++;
                }
                listBox1.DataSource = reslist;
            }
    


    jdweng

    • Proposed as answer by HomeGrownCoder Thursday, November 28, 2013 10:05 PM
    • Marked as answer by Sander Wolswijk Friday, November 29, 2013 6:35 PM
    Thursday, November 28, 2013 9:09 PM

All replies

  • The usual way of launching a form is:

    public static void Main( string[] args )

    {

        Application.Run( new Launcher() );

    }

    Then you can perform the initialization (LoadData) inside the Load event of form.

    Maybe it is better to create a new valid project with a form, then add the button and listbox.

    Thursday, November 28, 2013 8:50 PM
  • reslist cannot be inside the function.  It must be a static variable outside the function.  When a variable is inside a function it will get dispose as soon as the function completes so the listbox will never seen that data. 

               List<string> reslist = new List<string>();
     
           private void LoadData()
            {
                DEVMODE vDevMode = new DEVMODE();
                int i = 0;
                while (EnumDisplaySettings(null, i, ref vDevMode))
                {
                    reslist.Add(vDevMode.dmPelsWidth + "x" + vDevMode.dmPelsHeight);
                    i++;
                }
                listBox1.DataSource = reslist;
            }
    


    jdweng

    • Proposed as answer by HomeGrownCoder Thursday, November 28, 2013 10:05 PM
    • Marked as answer by Sander Wolswijk Friday, November 29, 2013 6:35 PM
    Thursday, November 28, 2013 9:09 PM
  • Thanks, Viorel and Joel

    It worked sort of, an other problem was that I forgot to call the InitializeComponent function, but without the reply of Viorel it would exit after it started and without the reply of Joel the error was still the same.

    Friday, November 29, 2013 6:34 PM