Search Unity

Error - Get text for XML and cause error "NullreferenceException: Object reference not set to....."

Discussion in 'Scripting' started by hijodelaluna, Jul 15, 2018.

  1. hijodelaluna

    hijodelaluna

    Joined:
    Jul 15, 2018
    Posts:
    3
    Hello Everybody!

    My name is Ares and i tryng creat an app, but have a problem... Before testing, get one exception:
    NullReferenceException: Object reference not set to an instance of an object.

    Im using unity 5.6 and EasyAr for my project.


    When starting the program, it brings all the information from the server, however after this it generates this error and hangs. I added a validation in the "GetText" to just know if it has information and the label is filled normally, but the error persists.

    My Code:


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using System.Xml;
    7. using System.Text;
    8.  
    9.  
    10. public class texto_Aries : MonoBehaviour {
    11.  
    12.     public Text myText;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update() {
    20.  
    21.         if (getText() != null) {
    22.             string hoscopo = getText();
    23.             myText.text = hoscopo;
    24.         } else {
    25.             myText.text = "Consultando os astros! Em breve teremos informações para você!";
    26.         }
    27.     }
    28.  
    29.     public string getText()
    30.     {
    31.         XmlDocument rssXmlDoc = new XmlDocument();
    32.         string descricao = "";
    33.  
    34.         rssXmlDoc.Load("http://www.bemdesaude.com/rss/horoscope/aries.html");
    35.  
    36.         // Parse the Items in the RSS file
    37.         XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
    38.  
    39.         StringBuilder rssContent = new StringBuilder();
    40.  
    41.         // Iterate through the items in the RSS file
    42.         foreach (XmlNode rssNode in rssNodes)
    43.         {
    44.             XmlNode rssSubNode = rssNode.SelectSingleNode("description");
    45.             descricao = rssSubNode != null ? rssSubNode.InnerText : "";
    46.         }
    47.  
    48.         return descricao;
    49.     }
    50. }


    Help me please!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Well, first off, doing this in Update is not a good idea. You're hitting your server once a frame by doing this. I doubt that is what you want or even need to do.

    Otherwise, if you're getting null, check where it's failing with debug.log statements to print out values.
     
  3. hijodelaluna

    hijodelaluna

    Joined:
    Jul 15, 2018
    Posts:
    3
    really is not the goal. However, in Start also the problem persists. I tested this method in visual studio and it worked normally, I can not get the null object.
     
  4. hijodelaluna

    hijodelaluna

    Joined:
    Jul 15, 2018
    Posts:
    3
    I am trying to instantiate the Text class. When debugging, I noticed that the "myText.text" is null. But it's not working ...

    Code (CSharp):
    1.     void Start () {
    2.             myText =  GetComponent<UnityEngine.UI.Text>();
    3.            
    4.             if (!string.IsNullOrEmpty(getText())) {
    5.                 horoscopo = getText();
    6.                 myText.text = horoscopo;
    7.             }
    8.        
    9.     }
     

    Attached Files:

    • 1111.png
      1111.png
      File size:
      92.3 KB
      Views:
      622