3

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

hughesdan
  • 3,019
  • 12
  • 57
  • 80
  • How did you manage to get __using Foo.Web.UPSWebReference;__ work? I'm using vs2010 and .net project under api samples needs converting, after done that not working. please share your foo.web – Cem Nov 04 '12 at 23:26

3 Answers3

4

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.

hughesdan
  • 3,019
  • 12
  • 57
  • 80
2

shipmentRequest.Shipment appears to be null, since you aren't assigning anything to it until the next line. So, you can't do shipmentRequest.Shipment.ANYTHING. Switch them around, so that you have

shipmentRequest.Shipment = shipment;    
shipmentRequest.Shipment.ShipmentServiceOptions.LabelDelivery.LabelLinksIndicator = "1"; 
Egor
  • 1,622
  • 12
  • 26
  • Thanks. I thought that might be the issue for a moment. But unfortunately I have the same problem even after switching those two lines around. – hughesdan Aug 22 '11 at 21:17
2

The element needs to get passed as <LabelLinksIndicator/> in the XML. In your sample you'd be sending it as <LabelLinksIndicator>1</LabelLinksIndicator> which is invalid.

andyknas
  • 1,939
  • 2
  • 15
  • 29
  • I'm now passing string.empty rather than 1, which I think should accomplish what you are suggesting. However, this does not solve my issue. – hughesdan Aug 23 '11 at 21:04
  • Have you tried viewing the request with a packet viewer? use a different URL (non-https) and then packet sniff with something like AnalogX. This really helps to troubleshoot issues with XML. Or, your error is coming before it even creates the request? – andyknas Aug 24 '11 at 01:03
  • Good suggestion. I was using Fiddler actually. But I'm going to try out AnalogX also based on your suggestion. +1 for introducing me to a new tool. – hughesdan Aug 24 '11 at 14:02