-2

I got this function which belongs to a Windows Form app.

This code works perfectly fine on some machines, but there are specific ones where this function returns a NullReferenceException. I don't know why. Any idea?

public Object ConsultaSimpleBD(SqlConnection conexion, String sql)
{              
     object valor = null;
     SqlCommand cmd = null;
     SqlDataReader reader = null;

     try
     {
         cmd = new SqlCommand(sql, this.getConexionBD());
         reader = cmd.ExecuteReader();

         if (reader.Read())
         {
             valor = reader[0];
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
     finally
     {
         reader.Close();
     }

     return valor;
 }

ERROR:

************* Exception Text *************

System.NullReferenceException: Object reference not set to an instance of an object. at ControlesGO.Generales.SentenciasBD.ConsultaSimpleBD(SqlConnection conexion, String sql)

Mochoa
  • 153
  • 1
  • 1
  • 7
  • 5
    Why are you catching an `Exception` then `throw new Exception` ? Have you debugged the code? What if `reader` is `null` and in the `final` block, an attempt to call `Close` on the reader. No reference to `connexion` except `this.getConexionBD()` what is that ? – t0mm13b Jun 01 '17 at 23:10
  • 1
    `throw new Exception(e.Message);` is a horrible line of code. Firstly you are removing a lot of information that you previously had in `e`. Secondly you are removing a lot of information that you previously had in `e`. I know technically that is the same point twice but it is such a massive point that it is worth repeating. You have just lost the original exception type, the original stack trace, all inner exception details, where it was originally thrown and possibly some other things I've forgotten. You are better off just removing that catch block entirely. – Chris Jun 01 '17 at 23:12
  • Secondly when presenting code with an error always say what line the error was thrown on. If you tell us where that exception is thrown then we have a better chance of working out what your problem is. I appreciate that in this case your catch block does make this much harder for you to do... – Chris Jun 01 '17 at 23:14
  • If `reader = cmd.ExecuteReader();` fails, your reference `reader` will be `null` and you'll get an NRE in the `finally` block. – spender Jun 01 '17 at 23:21
  • ok i explain you the code is from a custom library, i'm working on SQL Server 2008 and Visual Studio 2008 and i dont really know where is exactly occurs the exception because i run the main projeect that cosume this library, and the code it's not mine i'm workin in it – Mochoa Jun 01 '17 at 23:34
  • Are you certain it is a `NullPointerExcepction`? Or is it a `NullReferenceExcepction`? If the former you possibly got an issue in the library you use and it is not related to your provided code. – Quality Catalyst Jun 01 '17 at 23:38
  • See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box. ************* Exception Text ************* System.NullReferenceException: Object reference not set to an instance of an object. at ControlesGO.Generales.SentenciasBD.ConsultaSimpleBD(SqlConnection conexion, String sql) – Mochoa Jun 01 '17 at 23:46
  • sorry i wrote wrong the exception – Mochoa Jun 01 '17 at 23:46
  • but this occurs only in an specific one machine – Mochoa Jun 01 '17 at 23:47

1 Answers1

0

Change your finally block to this:

finally
{
    reader?.Close(); // reader can be null so cater for it
}
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62