1

I have a LINQ Statement to check if someone is entering the URL for a voucher Redemption. The way i get this voucher is by using a regular url, with an extra hashed CampaignID, Which is called my VoucherRedemption, as shown below.

if (Request.QueryString["voucherRedemption"] != null)
{

    String VoucherRemption = Request.QueryString["voucherRedemption"];
    MSCDatabaseDataContext MSCDB2 = new MSCDatabaseDataContext();
    var getCampaign = from campaign in MSCDB2.Tbl_Campaigns
                      where campaign.Link.Contains(VoucherRemption) 
                      select campaign;

    var VoucherCampaign = getCampaign.FirstOrDefault();


    campaignName.Value = VoucherCampaign.CampaignName;
    campaignDescription.Value = VoucherCampaign.CampaignDescription;
    txtStartDate.Text = VoucherCampaign.StartDate.ToString();
    txtDateEnd.Text = VoucherCampaign.EndDate.ToString();
    campaignAudience.Value = VoucherCampaign.Target.ToString();
    txtDiscount.Text = VoucherCampaign.Discount.ToString();
    txtTsCs.Text = VoucherCampaign.TermsConditions;
    txtTsCs.ReadOnly = true;
    CalendarExtender1.Enabled = false;
    CalendarExtender2.Enabled = false;
    txtStartDate.ReadOnly = true;
    txtDateEnd.ReadOnly = true;
    txtDiscount.ReadOnly = true;
    txtEmail.Visible = true;
}

Now i keep getting a:

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

But the weird thing is, is that yesterday it was working. But only yesterday. The other days it wasn't. Not its gone back to being broken. Is there somehow I can fix this?

Edit: I have checked that article, and still cant seem to find the problem. It was working yesterday

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
Kei Francois
  • 145
  • 14
  • `getCampaign.FirstOrDefault()` might return null. Then `VoucherCampaign.StartDate.ToString()` should throw that null reference exception. – Amit Kumar Ghosh Nov 05 '15 at 08:50
  • @AmitKumarGhosh But it was working fine yesterday. Now i get the exception at "campaignName.Value = VoucherCampaign.CampaignName;" And there arent any Null values in the DB for this. – Kei Francois Nov 05 '15 at 08:51
  • `VoucherCampaign.CampaignName` shouldn't throw . – Amit Kumar Ghosh Nov 05 '15 at 08:53
  • Run it with Debugger (F5) and it will highlight the row where the exception was thrown. You can also inspect values of the variables by hovering over them. – Mark Toman Nov 05 '15 at 08:54
  • 1
    Do you have same data as yesterday at DB? – Alexander Nov 05 '15 at 08:54
  • @AmitKumarGhosh `VoucherCampaign.CampaignName` can throw if `VoucherCampaign` is `null` – Alexander Nov 05 '15 at 08:56
  • 1
    @Alexander YES. Thats why it is throwing. But how can VocherCampaign Be Null, If it worked yesterday? just doesnt make sense. – Kei Francois Nov 05 '15 at 08:58
  • My bad, of course it could. @Alexander – Amit Kumar Ghosh Nov 05 '15 at 08:59
  • @Alexander Pretty much, i just added a few campaigns so i could do some testing. – Kei Francois Nov 05 '15 at 08:59
  • @KeiFrancois it looks like your have some problem with data. Are you sure that you really have any Camping for requested VoucherRemption now? – Alexander Nov 05 '15 at 09:07
  • Dude if i could send a picture i would. At this part "where campaign.Link.Contains(VoucherRemption) ", In the variable VoucherRemption, I can clearly see the hashed campaign ID. It has a value. Only when it gets to "var VoucherCampaign = getCampaign.FirstOrDefault();", inside VoucherCampaign, it has null. But i dont get how if its worked before, and my other values have values. @Alexander – Kei Francois Nov 05 '15 at 09:13

1 Answers1

1

You don't have a null check after

 var VoucherCampaign = getCampaign.FirstOrDefault();

this is very dangerous as it can return a default (aka null) at any time. The database connection could have failed, you got a timeout, there was no result from your query or you simply are not allowed to access the database. All would result in a null pointer when you try to use voucherCampaign. Try changing it to something like this: .

var VoucherCampaign = getCampaign.FirstOrDefault();
if (VoucherCampaign == null) {
//print a error message here so you know soemthing is wrong
return;
}
//rest of your code

It wont resolve the reason why you get a null. But at least your application won't crash on a null pointer in this function. If you want to know why you don't get a return change the firstrodefault to a first and put a try catch around it. The exception in your catch block will hold more information about why your query did not work.

Nick Otten
  • 702
  • 7
  • 17
  • Hey @NickOtten, Thanks for the help and your reply. But i figured out the problem. When the URL Reads certain charachtars like + and / , it replaces them with their real value. So i just had to do a simple replace on my VoucherRedemption Querystring. But thanks for the Null check, it also stops my App from crashing. And You helped me solve the problem, so thanks. – Kei Francois Nov 06 '15 at 09:46