Form Application registry key

-1

I have

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using Microsoft.Win32;

namespace Just_play_sounds_2
{
    public partial class Form1 : Form
    {

        private SoundPlayer soundPlayer;
        private RegistryKey reg;

        public Form1()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Minimized;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Hide();

            reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurentVersion\\Run", true);
            reg.SetValue("Play sounds", Application.ExecutablePath.ToString());
        }
    }
}

and it is flagging this line of code

reg.SetValue("Play sounds", Application.ExecutablePath.ToString());

I have tried everything to get this to work but it just will not work. I have search everything that I can find I could really use some help with this. If you reply to this can you tell my what I did wrong with my registry code.

error code

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Just play sounds 2
  StackTrace:
   at Just_play_sounds_2.Form1.Form1_Load(Object sender, EventArgs e) in D:\Projects\Just play sounds 2\Just play sounds 2\Form1.cs:line 37
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Just_play_sounds_2.Program.Main() in D:\Projects\Just play sounds 2\Just play sounds 2\Program.cs:line 19
c#
.net
windows
forms
asked on Stack Overflow Aug 14, 2020 by AMTitan • edited Aug 14, 2020 by AMTitan

1 Answer

0

With your edit, I can see now that your question is a duplicate of What is a NullReferenceException, and how do I fix it?. Most likely because the reg variable is null, which in turn is most likely due to the typographical error I already mentioned to you. – Peter Duniho

answered on Stack Overflow Aug 15, 2020 by AMTitan

User contributions licensed under CC BY-SA 3.0