Hello,

Im creating a serial com port. I want to be able to store the default settings in the registry such has baud, stop bits etc etc.

I have managed to do this but I just can't get my program to pull the stored settings out again. I have looked at lots of examples and to me what I am doing appears correct.

void Form1Load(object sender, EventArgs e)
		{	
	RegistryKey registry = Registry.CurrentUser.OpenSubKey( "Software\\ColourTool\\CommSetting");
MessageBox.Show((string)registry.GetValue("CommPort"));
		}

The above code will run but when I load this form I get..

System.NullReferenceException: Object reference not set to an instance of an object.
   at ColourTool.Form1.Form1Load(Object sender, EventArgs e) in c:\Documents and Settings\Jason\My Documents\SharpDevelop Projects\colourTool\colourTool\Form1.cs:line 46
   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.ContainerControl.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.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.Show()
   at ColourTool.MainForm.SetCommPortToolStripMenuItemClick(Object sender, EventArgs e) in c:\Documents and Settings\Jason\My Documents\SharpDevelop Projects\colourTool\colourTool\MainForm.cs:line 41
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.ToolStripDropDown.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.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   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 ColourTool.Program.Main(String[] args) in c:\Documents and Settings\Jason\My Documents\SharpDevelop Projects\colourTool\colourTool\Program.cs:line 27

Ultimatly I want the value to select the correct value in the drop down boxes in my app with something I assume will look similar to..

ComPortCombo.SelectedItem = key.GetValue( "CommPort" );

But seeing how I can't get a simple registry key value to pop up in a message box Im a way off that happening.

Appreciate any help

Thanks.

Ok I have tried the following but still get a "Object reference not set to an instance of an object." Error.

void Form1Load(object sender, EventArgs e)
		{
RegistryKey OurKey = Registry.Users;
OurKey = OurKey.OpenSubKey("Software\\ColourTool\\CommSetting",true);
MessageBox.Show(OurKey.GetValue("Parity").ToString());			
		}

Ok, Some further info. The code will work if I try and read from Registry.LocalMachine but I cannot store to Registry.LocalMachine. If I manually add data it will read back.

When using Registry.Users I can write to the registry but it would appear that I cannot read from the registry.

Is this a permissions thing?

Why not use a thing called Object Serialization so that you will have everything under your control, being undependant of some system permission settings?

If you're pulling it out of LocalMachine, it obviously had to get there some how. The Object reference not set in this case probably means that your RegistryKey object is null and that would mean that the key you're specifying to look up either doesn't exist or you do not have permissions. Run regedit and see if the key is there. If it is, then check the permissions. If you can write to the CurrentUsers registry but you cannot read from it, then your permissions are really screwed up. What's the point of writing a value if you can never retrieve it? Manually verify the keys are there and put a break point to check if the RegistryKey object is null.

The assumption that the key was NULL is correct. The key was there when I went in regedit. The original problem was I could write keys to CurrentUser but could not read back. I could however read from local machine which confused issues.

End result is the boolean true is for writeing if you don't use it then then you can only read. Either way user, local user and machine all act differently.

This article has been dead for over six months. Start a new discussion instead.