Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Error ArgumentNullException: String reference not set to an instance of a String. Parameter name: s

Discussion in 'Scripting' started by ricorigal, Dec 10, 2019.

  1. ricorigal

    ricorigal

    Joined:
    Jun 4, 2015
    Posts:
    4
    I have a problem and when executing unity I get this error

    ArgumentNullException: String reference not set to an instance of a String. Parameter name: s System.Text.Encoding.GetBytes (System.String s) (at :0) UnityEngine.WWWForm.AddField (System.String fieldName, System.String value, System.Text.Encoding e) (at C:/buildslave/unity/build/Modules/UnityWebRequest/Public/WebRequestUtils.cs:63) UnityEngine.WWWForm.AddField (System.String fieldName, System.String value) (at C:/buildslave/unity/build/Modules/UnityWebRequest/Public/WebRequestUtils.cs:55) Web+d_4.MoveNext () (at Assets/tt/Web.cs:91) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) d3:MoveNext() (at Assets/tt/item.cs:45) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) item:b1_0(String) (at Assets/tt/item.cs:17) d_5:MoveNext() (at Assets/tt/Web.cs:125) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    codigo item
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using UnityEngine.UI;
    6. using SimpleJSON;
    7. using System.IO;
    8.  
    9.  
    10. public class item : MonoBehaviour
    11. {
    12.     Action<string> _createItemsCallback;
    13.         // Start is called before the first frame update
    14.     void Start(){
    15.  
    16.         _createItemsCallback = (jsonArrayString)=>{
    17.           StartCoroutine(CreateItemsRoutine(jsonArrayString));
    18.         };
    19.  
    20.  
    21.  
    22.         CreateItems();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     public void CreateItems()
    27.     {
    28.          string iduser = Main.Instance.Userinfo.Iduser;
    29.          StartCoroutine(Main.Instance.Web.GetItemsIDs(iduser, _createItemsCallback));
    30.        
    31.     }
    32.     IEnumerator CreateItemsRoutine(string jsonArrayString){
    33.         JSONArray jsonArray = JSON.Parse(jsonArrayString) as JSONArray;
    34.         for (int i = 0; i < jsonArray.Count; i++){
    35.            bool isDone = false;
    36.             string idlugar = jsonArray[i].AsObject["idlugar"];
    37.             JSONObject itemInfoJson = new JSONObject();
    38.  
    39.             Action<string> getItemInfoCallback = (itemInfo) => {
    40.                 isDone = true;
    41.              JSONArray tempArray = JSON.Parse(itemInfo) as JSONArray;
    42.                 itemInfoJson = tempArray[0].AsObject;
    43.             };
    44.  
    45.            StartCoroutine(Main.Instance.Web.GetItem(idlugar, getItemInfoCallback));
    46.  
    47.             yield return new WaitUntil(() => isDone==true);
    48.  
    49.            GameObject item = Instantiate(Resources.Load("Prefabs/item") as GameObject);
    50.            item.transform.SetParent(this.transform);
    51.            item.transform.localScale = Vector3.one;
    52.            item.transform.localPosition = Vector3.zero;
    53.  
    54.            item.transform.Find("lugar").GetComponent<Text>().text = itemInfoJson["lugar"];
    55.          
    56.      }
    57.       yield return null;
    58.    }
    59.  
    60. }
    61.  
    codigo Web
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class Web: MonoBehaviour {
    8.     void Start() {
    9.         //StartCoroutine(GetUsers());
    10.        // StartCoroutine(Login("testear","123456"));
    11.        //StartCoroutine(Registro("testear","123456"));
    12.    
    13.         //StartCoroutine(GetItemsIDs());
    14.  
    15.     }
    16.     //public void ShowUserItems(){
    17.       //  StartCoroutine(GetItemsIDs(Main.Instance.Userinfo.Iduser));
    18.   // }
    19.     IEnumerator GetUsers() {
    20.         UnityWebRequest www = UnityWebRequest.Get("http://localhost/turismo/GetUsers.php");
    21.         yield return www.Send();
    22.         if(www.isNetworkError) {
    23.             Debug.Log(www.error);
    24.         }
    25.         else {
    26.             // Show results as text
    27.             Debug.Log(www.downloadHandler.text);
    28.             // Or retrieve results as binary data
    29.             byte[] results = www.downloadHandler.data;
    30.         }
    31.     }
    32.  
    33.     public IEnumerator Login(string login, string pass)
    34.     {
    35.         WWWForm form = new WWWForm();
    36.         form.AddField("nameuser", login);
    37.         form.AddField("passuser", pass);
    38.  
    39.         using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/login.php", form))
    40.         {
    41.             yield return www.SendWebRequest();
    42.  
    43.             if (www.isNetworkError || www.isHttpError)
    44.             {
    45.                 Debug.Log(www.error);
    46.             }
    47.             else
    48.             {
    49.               Debug.Log(www.downloadHandler.text);
    50.               Main.Instance.Userinfo.SetCredentials(login, pass);
    51.               Main.Instance.Userinfo.SetID(www.downloadHandler.text);
    52.  
    53.              if(www.downloadHandler.text.Contains("Wrong Credentials") || www.downloadHandler.text.Contains("no existe")){
    54.                Debug.Log("try agan");
    55.              }
    56.             else {
    57.                 Main.Instance.UserProfile.SetActive(true);
    58.                Main.Instance.Loginn.gameObject.SetActive(false);
    59.              }
    60.           }
    61.     }
    62.     }
    63.    
    64.  
    65.     public IEnumerator Registro(string login, string pass)
    66.     {
    67.         WWWForm form = new WWWForm();
    68.         form.AddField("nameuser", login);
    69.         form.AddField("passuser", pass);
    70.  
    71.         using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/registro.php", form))
    72.         {
    73.             yield return www.SendWebRequest();
    74.  
    75.             if (www.isNetworkError || www.isHttpError)
    76.             {
    77.                 Debug.Log(www.error);
    78.             }
    79.             else
    80.             {
    81.               Debug.Log(www.downloadHandler.text);
    82.             }
    83.         }
    84.     }
    85.  
    86.     public  IEnumerator GetItem(string idlugar, System.Action<string> callback) {
    87.         WWWForm form = new WWWForm();
    88.         form.AddField("idlugar", idlugar);
    89.        
    90.       using  (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/GetItem.php", form))
    91.       {
    92.         yield return www.Send();
    93.         if (www.isNetworkError || www.isHttpError){
    94.             Debug.Log(www.error);
    95.         }
    96.         else {
    97.             // Show results as text
    98.             Debug.Log(www.downloadHandler.text);
    99.    
    100.              string jsonArray = www.downloadHandler.text;
    101.              callback(jsonArray);
    102.         }
    103.       }
    104.     }
    105.  
    106.     public    IEnumerator GetItemsIDs(string iduser, System.Action<string> callback) {
    107.         WWWForm form = new WWWForm();
    108.         form.AddField("iduser", iduser);
    109.        
    110.       using  (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/GetItemsIDs.php", form)){
    111.         yield return www.Send();
    112.         if (www.isNetworkError || www.isHttpError){
    113.             Debug.Log(www.error);
    114.         }
    115.         else {
    116.             // Show results as text
    117.             Debug.Log(www.downloadHandler.text);
    118.         string jsonArray = www.downloadHandler.text;
    119.  
    120.         callback(jsonArray);
    121.  
    122.         }
    123.       }
    124.     }
    125.  
    126.  
    127. }

    espero su ayuda
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,075
    Sounds like your variable "idlugar" is null (that is, it has no proper value assigned to it). Looks like its value is coming from item line 36. I'm not familiar with the particular JSON library you are using, but I would guess that a null return value means the field you are trying to access does not exist or is not a string.

    You can either fix your input data so that you don't get nulls, or you can add some error-handling code to check if the variable is null and do something different when it is. (And you may want to do both.)
     
  3. wenjie7788

    wenjie7788

    Joined:
    Apr 9, 2020
    Posts:
    1
    Hi, have you solved this error?
     
unityunity