2

I have a bit of C# code calling a DAL file to add something to the database. I have the following line of code to call the method:

string ticket = new PromotionObject().AddPromotionEntry(formID, user, submission) 

// string, AccountUsers object from Session, string

Within PromotionObject class, I have at top:

private PromotionDAL db = new PromotionDAL(); // invokes the dal object

The AddPromotionEntry method is:

public string AddPromotionEntry(string formID, AccountUsers user, string submission) {
    try {
        return db.AddPromotionEntr(Convert.ToInt64(formID), user.Id, submission);
        // calls the method using db variable in form long, long, string; 
        // The user.Id gets the id of the user object to use in database
    }
    catch (NullReferenceException ex) {
        string msg = ex.Message; return string.Empty;
    }
}

Now the problem:

When I run this code for the first time by clicking a button on the page, there is a NullReferenceException trying to access the return db.AddPromotionEntry(...) method, identified by debugging and entering the catch. When putting a breakpoint on this line, none of the variables are null (all populated as expected) and the db variable is also not null.

When I run it again (not a page refresh) by the button click, this method call passes OK and works as expected.

I am lost at why the first time it throws a NullReferenceException but not on the second time - it is like the first time invoke is doing a catch up and by the seond invoke, it has caught up.

Any Ideas?? }

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
  • 3
    Are you compiling in debug or in release? Also disable "just my code" for the debugger. There are chances that `NullReferenceException` isn't in that line but _inside_ `db.AddPromotionEntr()` and you don't see it or its code is inlined. – Adriano Repetti Sep 20 '17 at 08:19
  • Where your db variable is declared ? in ctor ? – GGO Sep 20 '17 at 08:19
  • maybe `user` is `null`? – MakePeaceGreatAgain Sep 20 '17 at 08:19
  • Why did you catch NullReferenceException only and no other kind of exception. You knew that your code will throw NullReferenceException? – Amit Kumar Singh Sep 20 '17 at 08:20
  • It is in debug mode to track the progress. – Fraser Mathieson Sep 20 '17 at 08:28
  • The db variable is declared at the top of the PromotionObject() (where the AddPromotionEntry() method is used. I'm only catching the NullReference because that is what the error is generating and this is where theline number is saying in the stack trace. User is definately not null - I hover over it and it is fully populated – Fraser Mathieson Sep 20 '17 at 08:30
  • Just to add it is not a duplicated question as indicated by Lasse V. Karlsen - thats a generic question, but this only happens on the first time load of the page, then not if same process is then followed – Fraser Mathieson Sep 20 '17 at 08:36
  • Lasse V. Karlsen has colsed the question for the wrong reason. Try to create a [mcve]. If it is working, you should compare the result to your working code. If it is not working, edit your question. – Christian Gollhardt Sep 20 '17 at 09:07
  • Solved it, turned out the ajax was running async so sorted this and nullreference was resolved (Session was being destroyed in second ajax call which completed before first) Should have noticed this, sorry for the basic mistake – Fraser Mathieson Sep 20 '17 at 09:23

0 Answers0