1

I am trying to get the list of items inside of a quickbooks database. I have the following snippet of code:

                    IItemServiceRet itemServiceRet = itemRet.ItemServiceRet;

                    TestItem item = new TestItem();
                    item.Name = itemServiceRet.Name.GetValue();
                    item.Desc = itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();
                    item.Rate = itemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue().ToString();
                    item.ItemType = "Service";
                    item.QBID = itemServiceRet.ListID.GetValue();
                    item.EditSeq = itemServiceRet.EditSequence.GetValue();

The code fails on the line:

item.Desc = itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();
"Object reference not set to an instance of an object"

Because that particular service item in the QB database does not have a description. I was curious if there was a 'clean' way to check for null values without having to have each line include an if statement checking if GetValue() returns null?

Edit: After trying Andrew Cooper's solution of:

item.Desc = itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc == null ? null : itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();

It still throws the exception:

Object reference not set to an instance of an object

Its as if GetValue() returns nothing at all if there is no description, which wouldn't make much sense.

Giardino
  • 1,367
  • 3
  • 10
  • 30
  • item.Desc is looking for a value. So you would have to do put empty string instead of null. Also you might have to use a simple If Then, and not the Ternary operator. – user890332 Aug 27 '15 at 20:44

2 Answers2

3

You could use the ternary operator like this:

item.Desc = itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc == null ? null : itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();

But that's pretty ugly.

Best bet is to create a helper method that takes whatever type Desc and wraps the above logic.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • I tried that and I still get "Object Reference not set to an instance of an object" Its as if GetValue() is not returning null if nothing is there, but is returning nothing at all. – Giardino Jun 23 '14 at 17:03
  • 1
    It's the property `Desc` that is null. Calling `.GetValue()` on a null reference is the source of the exception. If `Desc` has a non-null value then `.GetValue()` will work okay. – Andrew Cooper Jun 23 '14 at 17:11
1

I don't think there's any better solution, as you can't access the functions of a null object. I typically create a constructor for my items that does the checking so I only have to do that once. So in your example:

TestItem item = new TestItem(itemRet.ItemServiceRet);

The constructor would look like this:

public TestItem(IItemServiceRet i)
{
    if(i.Name != null) this.Name = i.Name.GetValue();
    if(i.ORSalesPurchase != null)
        if(i.ORSalesPurchase.SalesOrPurchase != null)
        {
            if(i.ORSalesPurchase.SalesOrPurchase.Desc != null) this.Desc = i.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();
            if(i.ORSalesPurchase.SalesOrPurchase.ORPrice != null)
                if(i.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null) this.Price = i.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue();
        }
}
Keep in mind that this does work well for strings, as an empty string is usually the same as a blank field, but for number fields in QuickBooks this may cause problems. For example, an Invoice line can have a blank quantity field (which is a double). A double will default to the value of 0.0, but a blank quantity field in QuickBooks is treated as a quantity of 1.0. This can also cause problems with dates, as QuickBooks can have null date fields.
Hpjchobbes
  • 1,309
  • 1
  • 8
  • 11