Reputation: 997
I'm having an issue for C# (I'm new to it), when trying to fix a Null value. Therefore I have a variable "verif" (String verif = String.Empty;), which I used it to read some key from Windows Registry. My code works if the key exists, but when it doesn't I got the error"NullReferanceException was unhandled". I tried several ways, to catch the exception, to put an "If" statement but I failed miserable. My code is something like this:
RegistryKey key_user;
RegistryKey key_pwd;
String code = String.Empty;
String tara = String.Empty;
String tot = String.Empty;
String pwd_mdw = String.Empty;
String user_mdw = String.Empty;
String user_uca = String.Empty;
String pwd_uca = String.Empty;
String verif = String.Empty;
private void button1_Click(object sender, EventArgs e)
{tot = listBox1.SelectedValue.ToString();
//MessageBox.Show(tot);
tara = tot.Substring(tot.Length - 2, 2);
//MessageBox.Show(tara);
code = listBox1.SelectedValue.ToString().Substring(0, 2);
user_mdw = textBox1.Text;
//MessageBox.Show(user_mdw);
pwd_mdw = textBox2.Text;
//MessageBox.Show(pwd_mdw);
if (code == "CC")
{
verif = Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials").GetValue("user_mdw_" + tara + "_CC").ToString();
MessageBox.Show("Verif",verif);
MessageBox.Show(user_mdw, "user_mdw");
if (verif==null)
{
key_user = Registry.CurrentUser.CreateSubKey("Software\\TDCredentials");
key_user.SetValue("user_mdw_" + tara + "_CC", user_mdw);
key_user.Close();
key_pwd = Registry.CurrentUser.CreateSubKey("Software\\TDCredentials");
key_pwd.SetValue("pass_mdw_" + tara + "_CC", pwd_mdw);
key_pwd.Close();
MessageBox.Show("User and Password inserted successfully!");
textBox1.Clear();
textBox2.Clear();
}
else
{...
Any hints? Many thanks in advance, Bogdan.
Upvotes: 2
Views: 2834
Reputation: 180917
Looking at what you're trying to do, this line is most likely (one of) your problems;
verif = Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials")
.GetValue("user_mdw_" + tara + "_CC").ToString();
If the key does not exist, OpenSubKey
will return null, and you call GetValue()
on it without checking.
You can change the line to add a check, something like;
var key = Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials");
var value = key != null ? key.GetValue("user_mdw_" + tara + "_CC") : null;
verif = value != null ? value.ToString() : null;
if(verif == null) {
...
Upvotes: 4
Reputation: 5706
You're trying to do too much in one line, without checking the results as you go.
First of all, as others have already said, you need to check that OpenSubKey
doesn't return null. You also need to make sure that the key is closed when you're finished, with a using
statement:
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials"))
{
if (key == null)
{
// Couldn't open the key, now what?
// You need to make a decision here.
// If you read the documentation for CreateSubKey,
// you'll see that it can *also* return null, so don't rely on it.
}
else
{
// OK, opened the key, and the using statement will close it.
// Now we can try reading values. See the next part of the answer.
}
}
If you successfully open the key, you can try to read the value. Even if you successfully open the key, the value might not exist, or it might not be a string (it could be a DWORD, for instance, or a binary value).
GetValue
returns null, so calling ToString
without checking will throw NullReferenceException
.ToString
on it is the wrong thing to do, because it might not be a string value in the registry. If it's a binary value, for example, calling ToString
on it will give you the string System.Byte[]
. You need to check that it is actually a string.else
{
// OK, opened the key, and the using statement will close it.
// Now we can try reading values.
string verif = key.GetValue("user_mdw_" + tara + "_CC") as string;
if (verif == null)
{
// The value does not exist, or is not the type you expected it to be (string).
// Now what? You need to make a decision here.
}
else
{
// OK, do something with verif.
}
}
Make sure to read the documentation for these methods, and handle the special cases they mention, especially the circumstances under which they return null:
Upvotes: 0
Reputation: 211
Try checking for NULL on OpenSubKey() & GetValue() methods prior to using ToString() method.
Upvotes: 0
Reputation: 8818
Try the following check to test if Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials")
is not null
else it will bomb:
if (code == "CC")
{
if (Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials") != null)
{
verif =
Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials").GetValue("user_mdw_" + "Test" + "_CC").
ToString();
}
Upvotes: 0
Reputation: 32681
First of all you need to check
Registry.CurrentUser.OpenSubKey(@"Software\TDCredentials")
that this is not null. Then call the getvalue
method. Beause if the above key is null then the following getvalue will throw exception.
Upvotes: 3