2

I have a tricky question that has been befuddling me for a while. I have the following code declaration...

namespace ESEGURCI.WEB.BusinessLogicLayer.Commons
{
    public static class ParameterUtilities
    {
        public enum ParameterEnum
        {
            MAX_LOGIN_ATTEMPTS,
            AUDIT_MODIFICATIONS
        }
    }
}

and I call the code like so "ParameterUtilities.ParameterEnum.MAX_LOGIN_ATTEMPTS" Problem is once every full moon I get the error "object reference not set to an instance of an object" on this line... It's like the code only works 99.9% of the time...

The only thing that occurs to me is that since the enum is a value type that there can be a chance that the enum is null when the static class is called... But I can't find any documentation on this behavior...

Can someone enlighten me why this happens? I know I should probably remove the enum out of the static class, and declare the enum as standalone but I'd like to know why this is happening first...

Thanks, S

Update

Ok, to everyone who asked for more code, the following is the full function where the error occurs...

    public static int GetPageSize(int companyId)
    {
        int pageSize = 0;

        // error happens bellow this line

        ESEGURCI.WEB.BusinessLogicLayer.Entities.Parameter parameter = ESEGURCI.WEB.BusinessLogicLayer.Entities.Parameter.GetParameter(ParameterUtilities.ParameterEnum.AUDIT_MODIFICATIONS.ToString(), companyId);

        // error happens above this line

        int.TryParse(parameter.Value, out pageSize);

        return pageSize;
    }
bastos.sergio
  • 6,684
  • 4
  • 26
  • 36
  • 1
    Why not take the `Enum` definition outside of the class? – Oded Sep 04 '12 at 10:57
  • 2
    This line can't throw with NullReference, are you sure that error is not in other statements? please show more code how are you using enum value – Arsen Mkrtchyan Sep 04 '12 at 10:58
  • The exception is originated from something else in the offending line, enums cannot be null. Can you show us the actual code block where the exception occurs ? – Alex Sep 04 '12 at 11:03
  • This can not throw null ref exception. By the line that you have mentioned, it means that you are fetching one of the 2 possible values for that type. This line throwing null ref exception is similar to the number 1 or string "C# is a language" is throwing null ref exception. Also, "object reference not set to an instance of an object" means that you are trying to access a ref type object which has not been initialized or assigned a value. Post some code. – Shakti Prakash Singh Sep 04 '12 at 11:19
  • There might be issue in GetParameter. Put this statement in a try block and catch the exception. Also, when you get the exception, check the value of the enum and you will know that the enum is not the issue. – Shakti Prakash Singh Sep 04 '12 at 11:28
  • I guess you could try Dennis's soln. It'll help to trace the issue. – Shakti Prakash Singh Sep 04 '12 at 11:38
  • Its not possible for `ParameterEnum` to be `NULL` since its an integer and the default value is `0`. As other people explain the entire problem is `GetParameter` the simple solution is to return a default value instead of what appears to be a `NULL` string. – Security Hound Sep 04 '12 at 11:45

4 Answers4

7

ParameterUtilities.ParameterEnum.MAX_LOGIN_ATTEMPTS won't ever throw a null reference exception, no matter what the Moon looks like. The error is probably triggered by an other instruction on the same line (assignment to a variable?).

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
2

An enum can't be null.

Split up the line as in the listing below and see which statement throws the exception. I bet it happens somewhere in Parameter.GetParameter():

using ESEGURCI.WEB.BusinessLogicLayer.Entities;

// ...

var auditModifications = 
    ParameterUtilities.ParameterEnum.AUDIT_MODIFICATIONS.ToString();
var parameter = Parameter.GetParameter(auditModifications, companyId);
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • I bet it's the 3rd because if you look at the OP's code `ESEGURCI.WEB.BusinessLogicLayer.Entities.Parameter` is a type (see my answer). – James Sep 04 '12 at 11:36
  • @James if `Parameter.GetParameter()` wasn't static it wouldn't work at all. – Dennis Traub Sep 04 '12 at 11:38
  • yeah, I kinda realised that after re-reading the question! Still think the problem is coming from the GetParameter method. – James Sep 04 '12 at 11:39
  • Sorry, ESEGURCI.WEB.BusinessLogicLayer.Entities is a namespace... I updated the question accordingly... – bastos.sergio Sep 04 '12 at 11:39
  • @bastos.sergio that makes it even easier to find. I updated my answer accordingly. – Dennis Traub Sep 04 '12 at 11:57
  • Sorry, but like I replied to @James the stacktrace that I'm logging shows the exception as originating in the GetPageSize method... The code never enters the GetParameter method... – bastos.sergio Sep 04 '12 at 12:12
1

Enum (and any other type) cannot have null value, because it isn't a value it is a type.

The exception is thrown by something else.

Richard
  • 106,783
  • 21
  • 203
  • 265
Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50
  • Made this more explicit -- a type is not a value (you would need an instance, static or not, for that). – Richard Sep 04 '12 at 11:00
1

As already stated your enum will not be where the error is coming from. Based on your update, I would say the NRE is most likely coming from your GetParameter method.

James
  • 80,725
  • 18
  • 167
  • 237
  • Like I said the code works 99.9% of the time... There's no reason for the GetParameter method, which is static, to not work... If this is not due to the enum variable, then do you think this could be attributed to the declaration of the class Parameter? The class is declared as partial... more specifically "public partial class Parameter" – bastos.sergio Sep 04 '12 at 11:36
  • @bastos.sergio "*do you think this could be attributed to the declaration of the class Parameter*" - Yes, more specifically the `GetParameter` method. Have a look at the code in there and check if there are any scenarios where an NRE could be thrown. – James Sep 04 '12 at 11:38
  • That's the thing... The stacktrace from my logs does not show the error as originating inside the Parameter class... So what you said makes no sense to me... The error begins at the function GetPageSize and propagates down from there... – bastos.sergio Sep 04 '12 at 11:46
  • @bastos.sergio The following line won't throw an NRE - `ParameterUtilities.ParameterEnum.AUDIT_MODIFICATIONS.ToString()` therefore there is only 2 other possibilities, an NRE in `GetParameter` or it returns `null` which you then attempt to use later in your code block. You are adamant that the error is happening on that line specifically so that leaves me to thinking it's an error in `GetParameter`. – James Sep 04 '12 at 11:55
  • Like I said the stacktrace is very clear, the exception begins at that line... I guess the only way to properly resolve this will be to add more auditing code and wait for the error to reappear... – bastos.sergio Sep 04 '12 at 12:03