-1

Debugging error on Nullreference

I get this error when debugging my program:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Code below is the one that throws the error and I totally have no idea how to fix it. Anyone can help? Thanks a lot.

return cmd.ExecuteScalar().ToString();
Sunil
  • 3,404
  • 10
  • 23
  • 31
user548682
  • 29
  • 1
  • 7
  • 1
    It's not totally obvious to me what language this is or what you're trying to do- if it's c#, consider adding that to the tags, if it's sql, you might mention that as well. Also, have you checked to see if cmd>executeSccalar() is returning null? If so, that would be a possible cause. – Joshua R. Jan 03 '18 at 01:52
  • It is c#. Can you give me the proper code to test the value of executescalar? Like this? return cmd.ExecuteScalar; – user548682 Jan 03 '18 at 01:59
  • using (OracleConnection cn = new OracleConnection(OracleHelper.connINT_STRING)) { cn.Open(); using (OracleCommand cmd = new OracleCommand(@"select qty from qtyIDTab where qtyid = :qtyidt", cn)) { cmd.CommandType = CommandType.Text; OracleParameter hmlidParam = new OracleParameter("hml", OracleDbType.Varchar2, 16); qtyidtParam.Value=qtyidt; cmd.Parameters.Add(qtyidtParam); return cmd.ExecuteScalar().ToString(); } } – user548682 Jan 03 '18 at 02:09
  • I'm don't really speak C#, but can you inspect in a debugger or log it to console? – Joshua R. Jan 03 '18 at 02:11
  • 2
    @user548682 please add the code to your question not in the comments, so the question will be clear and complete. – Shiko Jan 03 '18 at 02:36
  • Could you change your query from `select qty from qtyIDTab where qtyid = :qtyidt` to `select count(*) from qtyIDTab where qtyid = :qtyidt` as a test? If there's no exception you know the problem is in the query result (i.e. no rows returned), if there is, it's likely in the setup of the connection. – Joshua R. Jan 03 '18 at 03:14
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sunil Jan 03 '18 at 03:28

2 Answers2

1

I can only think of two possibilities:

  1. cmd is null;

  2. cmd.ExecuteScalar() returns null.

You could add temporary variables to debug.

Han Zhao
  • 1,932
  • 7
  • 10
0

Is it possible that your result set has no records in it?

According to the docs on SqlCommand.ExecuteScalar (https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx)

Return Value
Type: System.Object
The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty. Returns a maximum of 2033 characters.

If no records are returned, it evaluates to a null reference, which would be consistent with your result.

Joshua R.
  • 2,282
  • 1
  • 18
  • 21
  • Can you use SQL Management Studio (or a similar tool) to verify that your result returns a row? You might also consider posting your SQL if you're not sure how to construct a query such that you always get a result row. – Joshua R. Jan 03 '18 at 02:08
  • Hi Joshua, I did try to query in database before I post this question. The result is not null from database. Need help. – user548682 Jan 03 '18 at 02:22
  • Have you successfully used SqlCommand.ExecuteScalar elsewhere in your code? Could you add how you're setting up the `cmd.commandText` to your question? – Joshua R. Jan 03 '18 at 02:29