-1

This will probably get marked as dublicate or get closed, but I need to ask. What is illegal here?

private void btnFind_Click(object sender, EventArgs e)
{
    connection.Open();
    OleDbCommand command1 = new OleDbCommand();
    command1.Connection = connection;
    command1.CommandText = "select antonym from antonymsdb where word ='" + txtWord.Text + "'";
    string antonymdoc = command1.ExecuteScalar().ToString();
    lblAntonym.Text = antonymdoc;

    OleDbCommand command2 = new OleDbCommand();
    command2.Connection = connection;
    command2.CommandText = "select word from antonymsdb where antonym = '" + txtWord.Text + "'";
    string antonymdocx = command2.ExecuteScalar().ToString();
    lblAntonym.Text = antonymdocx;
    connection.Close();
}

I'm trying to read both columns word and antonym and return the word of the same row, just different column. It keeps showing error and I tried searching the Internet, but found nothing helpful...

YowE3K
  • 23,852
  • 7
  • 26
  • 40
  • `It keeps showing error` What is the error text? – Victor Zakharov May 21 '17 at 01:36
  • 1
    **Use a debugger**. – Eric Lippert May 21 '17 at 01:41
  • I'm not sure why this is a duplicate. The question isn't what a NullReferenceException is. The question is why is this code throwing one. Although as mentioned, this is the sort of thing more easily found in the debugger. Unless it's production code and all you've got is the method throwing the exception. Then we have to read the code and play "find the null." – Scott Hannen May 21 '17 at 01:49
  • @ScottHannen it is a duplicate for the "How do I fix it" portion. The way you fix a NRE is the same way every time and it would add little value to the site to keep repeating over and over "Break apart chained invocations and find the specific part that is null then backtrack in your program and find out why that variable is null". Now if the question was "I have found that `command1.ExecuteScalar()` will return null causing `.ToString()` to throw a NRE, I don't understand why I am not getting a value from `ExecuteScalar()`" That would not be a duplicate question. – Scott Chamberlain May 21 '17 at 01:58

1 Answers1

1

This is a suspect:

string antonymdoc = command1.ExecuteScalar().ToString();

If the query returns no records then ExecuteScalar returns null. If you try to call .ToString() on null you'll get a NullReferenceException.

Usually I would first check the result to see if it's null so that I know if it returned a result.

If you just wanted to get an empty string when there's no result you could do

string antonymdoc = command1.ExecuteScalar()?.ToString() ?? string.Empty;

which is shorthand for

var output = command1.ExecuteScalar();
string antonymdoc = output != null ? output.ToString() : string.Empty;

which is shorthand for

var output = command1.ExecuteScalar();
string antonymdoc;
if(output!=null)
{
    antonymdoc - output.ToString(); 
}
else
{
    antonymdoc = string.Empty;
}
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62