Search code examples
c#web-servicesups

UPS API - LabelLinksIndicator - Object Reference not set to an instance of an object


Below is some code for generating a UPS shipping label. It is based on the .net sample provided in the UPS developer kit. My problem is with the line highlighted in bold below. It throws and error as follows "System.NullReferenceException: Object reference not set to an instance of an object.".

What I know is this LabelLinksIndicator is required if you want the UPS API to return a link to a label. Otherwise it returns details on the shipment but no label. The relevant section of the UPS documentation is at the bottom of my question.

What do I need to do to overcome this error? It is not clear to me from the documentation what value I should be passing for LabelLinksIndicator. I've tried passing a 1, true and and an empty string. Same error in every case. The error message seems to indicate that there is an object I need to instantiate that I am not currently. However, I can't figure out what to instantiate. The available information on the web and on UPS.com is unfortunately sparse.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Foo.Web.UPSWebReference;


namespace Foo.Web.Auth
{
    public partial class UPS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                try
                {
                    ShipService shpSvc = new ShipService();
                    ShipmentRequest shipmentRequest = new ShipmentRequest();
                    UPSSecurity upss = new UPSSecurity();
                    UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
                    upssSvcAccessToken.AccessLicenseNumber = "foo";
                    upss.ServiceAccessToken = upssSvcAccessToken;
                    UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
                    upssUsrNameToken.Username = "foo";
                    upssUsrNameToken.Password = "foo";
                    upss.UsernameToken = upssUsrNameToken;
                    shpSvc.UPSSecurityValue = upss;
                    RequestType request = new RequestType();
                    String[] requestOption = { "nonvalidate" };
                    request.RequestOption = requestOption;
                    shipmentRequest.Request = request;
                    ShipmentType shipment = new ShipmentType();
                    shipment.Description = "Ship webservice example";
                    ReturnServiceType rtn = new ReturnServiceType();
                    rtn.Code = "8";
                    rtn.Description = "Description";

                    ShipperType shipper = new ShipperType();
                    shipper.ShipperNumber = "foo";
                    PaymentInfoType paymentInfo = new PaymentInfoType();
                    ShipmentChargeType shpmentCharge = new ShipmentChargeType();
                    BillShipperType billShipper = new BillShipperType();
                    billShipper.AccountNumber = "foo";
                    shpmentCharge.BillShipper = billShipper;
                    shpmentCharge.Type = "01";
                    ShipmentChargeType[] shpmentChargeArray = { shpmentCharge };
                    paymentInfo.ShipmentCharge = shpmentChargeArray;
                    shipment.PaymentInformation = paymentInfo;
                    foo.Web.UPSWebReference.ShipAddressType shipperAddress = new foo.Web.UPSWebReference.ShipAddressType();
                    String[] addressLine = { "301 166th Street" };
                    shipperAddress.AddressLine = addressLine;
                    shipperAddress.City = "Jersey City";
                    shipperAddress.PostalCode = "07310";
                    shipperAddress.StateProvinceCode = "NJ";
                    shipperAddress.CountryCode = "US";
                    shipperAddress.AddressLine = addressLine;
                    shipper.Address = shipperAddress;
                    shipper.Name = "ABC Associates";
                    shipper.AttentionName = "ABC Associates";
                    ShipPhoneType shipperPhone = new ShipPhoneType();
                    shipperPhone.Number = "1234567890";
                    shipper.Phone = shipperPhone;
                    shipment.Shipper = shipper;
                    ShipFromType shipFrom = new ShipFromType();
                    foo.Web.UPSWebReference.ShipAddressType shipFromAddress = new foo.Web.UPSWebReference.ShipAddressType();
                    String[] shipFromAddressLine = { "100 82nd Street" };
                    shipFromAddress.AddressLine = addressLine;
                    shipFromAddress.City = "New York";
                    shipFromAddress.PostalCode = "10024";
                    shipFromAddress.StateProvinceCode = "NY";
                    shipFromAddress.CountryCode = "US";
                    shipFrom.Address = shipFromAddress;
                    shipFrom.AttentionName = "Mr.ABC";
                    shipFrom.Name = "ABC Associates";
                    shipment.ShipFrom = shipFrom;
                    ShipToType shipTo = new ShipToType();
                    ShipToAddressType shipToAddress = new ShipToAddressType();
                    String[] addressLine1 = { "Some Street" };
                    shipToAddress.AddressLine = addressLine1;
                    shipToAddress.City = "Roswell";
                    shipToAddress.PostalCode = "30076";
                    shipToAddress.StateProvinceCode = "GA";
                    shipToAddress.CountryCode = "US";
                    shipTo.Address = shipToAddress;
                    shipTo.Address.ResidentialAddressIndicator = "1"; //dan
                    shipTo.AttentionName = "DEF";
                    shipTo.Name = "DEF Associates";
                    ShipPhoneType shipToPhone = new ShipPhoneType();
                    shipToPhone.Number = "1234567890";
                    shipTo.Phone = shipToPhone;
                    shipment.ShipTo = shipTo;
                    ServiceType service = new ServiceType();
                    service.Code = "03"; 
                    service.Description = "Ground";
                    shipment.Service = service;
                    PackageType package = new PackageType();
                    package.Description = "Deliver to Warehouse";
                    PackageWeightType packageWeight = new PackageWeightType();
                    packageWeight.Weight = "10";
                    ShipUnitOfMeasurementType uom = new ShipUnitOfMeasurementType();
                    uom.Code = "LBS";
                    packageWeight.UnitOfMeasurement = uom;
                    package.PackageWeight = packageWeight;
                    PackagingType packType = new PackagingType();
                    packType.Code = "02";
                    package.Packaging = packType;

                    PackageType[] pkgArray = { package };
                    shipment.Package = pkgArray;
                    LabelSpecificationType labelSpec = new LabelSpecificationType();

                    LabelImageFormatType labelImageFormat = new LabelImageFormatType();
                    labelImageFormat.Code = "GIF";
                    labelSpec.LabelImageFormat = labelImageFormat;
                    string userAgent = Request.UserAgent; 
                    labelSpec.HTTPUserAgent = userAgent; 
                    shipmentRequest.LabelSpecification = labelSpec;

                    **shipmentRequest.Shipment.ShipmentServiceOptions.LabelDelivery.LabelLinksIndicator = "1";**

                    shipmentRequest.Shipment = shipment;


                    System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

                    ShipmentResponse shipmentResponse = shpSvc.ProcessShipment(shipmentRequest);



                    Response.Redirect(shipmentResponse.ShipmentResults.LabelURL.ToString());

enter image description here


Solution

  • I figured out what the problem was. I needed first to new-up an instance of

    ShipmentTypeShipmentServiceOptions shipServOpt = new ShipmentTypeServiceOptions();
    

    and

    LabelDeliveryType labelDel = new LabelDeliveryType();
    

    Then set the LabelLinksIndicator element

    labeldel.LabelLinksIndicator = "";
    

    Then assign the options to my shipment instance

    shipment.ShipmentServiceOptions = shipServOpt;
    

    Note, ShipmentServiceOptionsType is NOT the same as ShipmentTypeServiceOptions. This tripped me up for a while.

    Dear UPS, if you are reading this please consider improving upon your documentation and providing a more complete set of code examples.