Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error on xml.Response

Error on xml.Response

Scheduled Pinned Locked Moved C#
xmlhelptutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bootzilla33
    wrote on last edited by
    #1

    I get an 'object reference not set to an instance of an object' error on the ' if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")' line. I know that is a vague error and If someone can see what the problem based off the code. I would also like to know where I can go to or how to get a more specific error message if possible. And also I know there is an xmlResolver that can be set to null that can be used but not sure how to use it in this code.

    using System;

    namespace Shipment_WS_Test_App
    {

    class Class1
    {
    	/// /// The main entry point for the application.
    	/// 
    	\[STAThread\]
    	static void Main(string\[\] args)
    	{
    		try
    		{
                string flg = string.Empty;
                while (flg != "q")
                {
    
                    System.Text.StringBuilder request = new System.Text.StringBuilder();
                    saia.shipment.Shipment shipment = new saia.shipment.Shipment();
                    shipment.Timeout = 120000;
                    request.Append("");
                    request.Append("userid");
                    request.Append("password");
                    request.Append("N");
                    request.Append("983579110");
                    request.Append("");
    
    
                    string xmlreq = "AMZN123454.0123456789 ";
                 
    
                    //string response = shipment.ProcessXML(request.ToString());
    
                    string response = shipment.ProcessXML(xmlreq);
                    System.Xml.XmlDocument xmlResponse = new System.Xml.XmlDocument();
                    
                    xmlResponse.LoadXml(response);
    
                    // Note: These examples use SelectSingleNode() which accepts a string 
                    // containing a XPath expression. This is a common way to retrieve a node 
                    // from a small XML Document. 
    
                    if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")
                    {
                        // Add error handling code in case Saia responds 
                        // with an Erro
    
    OriginalGriffO P Richard DeemingR 3 Replies Last reply
    0
    • B Bootzilla33

      I get an 'object reference not set to an instance of an object' error on the ' if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")' line. I know that is a vague error and If someone can see what the problem based off the code. I would also like to know where I can go to or how to get a more specific error message if possible. And also I know there is an xmlResolver that can be set to null that can be used but not sure how to use it in this code.

      using System;

      namespace Shipment_WS_Test_App
      {

      class Class1
      {
      	/// /// The main entry point for the application.
      	/// 
      	\[STAThread\]
      	static void Main(string\[\] args)
      	{
      		try
      		{
                  string flg = string.Empty;
                  while (flg != "q")
                  {
      
                      System.Text.StringBuilder request = new System.Text.StringBuilder();
                      saia.shipment.Shipment shipment = new saia.shipment.Shipment();
                      shipment.Timeout = 120000;
                      request.Append("");
                      request.Append("userid");
                      request.Append("password");
                      request.Append("N");
                      request.Append("983579110");
                      request.Append("");
      
      
                      string xmlreq = "AMZN123454.0123456789 ";
                   
      
                      //string response = shipment.ProcessXML(request.ToString());
      
                      string response = shipment.ProcessXML(xmlreq);
                      System.Xml.XmlDocument xmlResponse = new System.Xml.XmlDocument();
                      
                      xmlResponse.LoadXml(response);
      
                      // Note: These examples use SelectSingleNode() which accepts a string 
                      // containing a XPath expression. This is a common way to retrieve a node 
                      // from a small XML Document. 
      
                      if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")
                      {
                          // Add error handling code in case Saia responds 
                          // with an Erro
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself. Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable. It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night. We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket! Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't. But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values. But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • B Bootzilla33

        I get an 'object reference not set to an instance of an object' error on the ' if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")' line. I know that is a vague error and If someone can see what the problem based off the code. I would also like to know where I can go to or how to get a more specific error message if possible. And also I know there is an xmlResolver that can be set to null that can be used but not sure how to use it in this code.

        using System;

        namespace Shipment_WS_Test_App
        {

        class Class1
        {
        	/// /// The main entry point for the application.
        	/// 
        	\[STAThread\]
        	static void Main(string\[\] args)
        	{
        		try
        		{
                    string flg = string.Empty;
                    while (flg != "q")
                    {
        
                        System.Text.StringBuilder request = new System.Text.StringBuilder();
                        saia.shipment.Shipment shipment = new saia.shipment.Shipment();
                        shipment.Timeout = 120000;
                        request.Append("");
                        request.Append("userid");
                        request.Append("password");
                        request.Append("N");
                        request.Append("983579110");
                        request.Append("");
        
        
                        string xmlreq = "AMZN123454.0123456789 ";
                     
        
                        //string response = shipment.ProcessXML(request.ToString());
        
                        string response = shipment.ProcessXML(xmlreq);
                        System.Xml.XmlDocument xmlResponse = new System.Xml.XmlDocument();
                        
                        xmlResponse.LoadXml(response);
        
                        // Note: These examples use SelectSingleNode() which accepts a string 
                        // containing a XPath expression. This is a common way to retrieve a node 
                        // from a small XML Document. 
        
                        if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")
                        {
                            // Add error handling code in case Saia responds 
                            // with an Erro
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Griff has given you good advice. I will say that the reason you are seeing this error is probably because of a fundamental assumption you're making here - that you have /Response/Code as a single node. If you retrieve this into a separate variable, I bet you'll see that you don't actually have this as a response. BTW - quick hint, if you wrap the instantiation of shipment into a using statement, you'll automatically get a Dispose on it, so you won't need to worry about having to manually call Dispose yourself.

        This space for rent

        1 Reply Last reply
        0
        • B Bootzilla33

          I get an 'object reference not set to an instance of an object' error on the ' if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")' line. I know that is a vague error and If someone can see what the problem based off the code. I would also like to know where I can go to or how to get a more specific error message if possible. And also I know there is an xmlResolver that can be set to null that can be used but not sure how to use it in this code.

          using System;

          namespace Shipment_WS_Test_App
          {

          class Class1
          {
          	/// /// The main entry point for the application.
          	/// 
          	\[STAThread\]
          	static void Main(string\[\] args)
          	{
          		try
          		{
                      string flg = string.Empty;
                      while (flg != "q")
                      {
          
                          System.Text.StringBuilder request = new System.Text.StringBuilder();
                          saia.shipment.Shipment shipment = new saia.shipment.Shipment();
                          shipment.Timeout = 120000;
                          request.Append("");
                          request.Append("userid");
                          request.Append("password");
                          request.Append("N");
                          request.Append("983579110");
                          request.Append("");
          
          
                          string xmlreq = "AMZN123454.0123456789 ";
                       
          
                          //string response = shipment.ProcessXML(request.ToString());
          
                          string response = shipment.ProcessXML(xmlreq);
                          System.Xml.XmlDocument xmlResponse = new System.Xml.XmlDocument();
                          
                          xmlResponse.LoadXml(response);
          
                          // Note: These examples use SelectSingleNode() which accepts a string 
                          // containing a XPath expression. This is a common way to retrieve a node 
                          // from a small XML Document. 
          
                          if (xmlResponse.SelectSingleNode("/Response/Code").InnerText != "")
                          {
                              // Add error handling code in case Saia responds 
                              // with an Erro
          
          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Since it looks like you're writing new code, rather than maintaining existing code, you might want to consider using LINQ to XML instead: LINQ to XML Overview (C#) | Microsoft Docs[^]

          using System;
          using System.Xml.Linq;
          using System.Xml.XPath;

          namespace Shipment_WS_Test_App
          {
          static class Class1
          {
          private static readonly XNamespace XsiNamespace = "www.w3.org/2001/XMLSchema-instance";

              /// <summary>
              /// The main entry point for the application.
              /// </summary>
              \[STAThread\]
              static void Main(string\[\] args)
              {
                  try
                  {
                      using (var shipment = new saia.shipment.Shipment())
                      {
                          shipment.Timeout = 120000;
                          
                          var request = new XDocument(
                              new XElement("AmazonTrackingRequest",
                                  new XAttribute(XNamespace.Xmlns + "xsi", XsiNamespace),
                                  new XAttribute(XsiNamespace + "noNamespaceSchemaLocation", "AmazonTrackingRequest.xsd"),
                                  new XElement("Validation",
                                      new XElement("UserID", "AMZN"),
                                      new XElement("Password", "12345")
                                  ),
                                  new XElement("APIVersion", "4.0"),
                                  new XElement("TrackingNumber", "123456789")
                              )
                          );
                          
                          string response = shipment.ProcessXML(request.ToString());
                          if (string.IsNullOrEmpty(response))
                          {
                              Console.WriteLine("No response.");
                          }
                          else
                          {
                              XDocument xmlResponse = XDocument.Parse(response);
                              if (xmlResponse.Root == null)
                              {
                                  Console.WriteLine("No response. ({0})", response);
                              }
                              else
                              {
                                  XElement responseNode = xmlResponse.Root.Element("Response");
          

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups