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

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using SimpleJSON;
using System.IO;


public class item : MonoBehaviour
{
    Action<string> _createItemsCallback;
        // Start is called before the first frame update
    void Start(){

        _createItemsCallback = (jsonArrayString)=>{
          StartCoroutine(CreateItemsRoutine(jsonArrayString));
        };



        CreateItems();
    }

    // Update is called once per frame
    public void CreateItems()
    {
         string iduser = Main.Instance.Userinfo.Iduser;
         StartCoroutine(Main.Instance.Web.GetItemsIDs(iduser, _createItemsCallback));
       
    }
    IEnumerator CreateItemsRoutine(string jsonArrayString){
        JSONArray jsonArray = JSON.Parse(jsonArrayString) as JSONArray;
        for (int i = 0; i < jsonArray.Count; i++){
           bool isDone = false;
            string idlugar = jsonArray[i].AsObject["idlugar"];
            JSONObject itemInfoJson = new JSONObject();

            Action<string> getItemInfoCallback = (itemInfo) => {
                isDone = true;
             JSONArray tempArray = JSON.Parse(itemInfo) as JSONArray;
                itemInfoJson = tempArray[0].AsObject;
            };

           StartCoroutine(Main.Instance.Web.GetItem(idlugar, getItemInfoCallback));

            yield return new WaitUntil(() => isDone==true);

           GameObject item = Instantiate(Resources.Load("Prefabs/item") as GameObject);
           item.transform.SetParent(this.transform);
           item.transform.localScale = Vector3.one;
           item.transform.localPosition = Vector3.zero;

           item.transform.Find("lugar").GetComponent<Text>().text = itemInfoJson["lugar"];
          
     }
      yield return null;
   }
 
}

codigo Web

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class Web: MonoBehaviour {
    void Start() {
        //StartCoroutine(GetUsers());
       // StartCoroutine(Login("testear","123456"));
       //StartCoroutine(Registro("testear","123456"));
   
        //StartCoroutine(GetItemsIDs());

    }
    //public void ShowUserItems(){
      //  StartCoroutine(GetItemsIDs(Main.Instance.Userinfo.Iduser));
  // }
    IEnumerator GetUsers() {
        UnityWebRequest www = UnityWebRequest.Get("http://localhost/turismo/GetUsers.php");
        yield return www.Send();
        if(www.isNetworkError) {
            Debug.Log(www.error);
        }
        else {
            // Show results as text
            Debug.Log(www.downloadHandler.text);
            // Or retrieve results as binary data
            byte[] results = www.downloadHandler.data;
        }
    }

    public IEnumerator Login(string login, string pass)
    {
        WWWForm form = new WWWForm();
        form.AddField("nameuser", login);
        form.AddField("passuser", pass);

        using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/login.php", form))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
              Debug.Log(www.downloadHandler.text);
              Main.Instance.Userinfo.SetCredentials(login, pass);
              Main.Instance.Userinfo.SetID(www.downloadHandler.text);

             if(www.downloadHandler.text.Contains("Wrong Credentials") || www.downloadHandler.text.Contains("no existe")){
               Debug.Log("try agan");
             }
            else {
                Main.Instance.UserProfile.SetActive(true);
               Main.Instance.Loginn.gameObject.SetActive(false);
             }
          }
    }
    }
   

    public IEnumerator Registro(string login, string pass)
    {
        WWWForm form = new WWWForm();
        form.AddField("nameuser", login);
        form.AddField("passuser", pass);

        using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/registro.php", form))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
              Debug.Log(www.downloadHandler.text);
            }
        }
    }

    public  IEnumerator GetItem(string idlugar, System.Action<string> callback) {
        WWWForm form = new WWWForm();
        form.AddField("idlugar", idlugar);
       
      using  (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/GetItem.php", form))
      {
        yield return www.Send();
        if (www.isNetworkError || www.isHttpError){
            Debug.Log(www.error);
        }
        else {
            // Show results as text
            Debug.Log(www.downloadHandler.text);
   
             string jsonArray = www.downloadHandler.text;
             callback(jsonArray);
        }
      }
    }

    public    IEnumerator GetItemsIDs(string iduser, System.Action<string> callback) {
        WWWForm form = new WWWForm();
        form.AddField("iduser", iduser);
       
      using  (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/GetItemsIDs.php", form)){
        yield return www.Send();
        if (www.isNetworkError || www.isHttpError){
            Debug.Log(www.error);
        }
        else {
            // Show results as text
            Debug.Log(www.downloadHandler.text);
        string jsonArray = www.downloadHandler.text;

        callback(jsonArray);

        }
      }
    }


}

espero su ayuda

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.)

Hi, have you solved this error?

1 Like

Hi there, have you found a solution for this yet?

1 Like

Hi, i have the identical issue. Please tell us if and how you solved it.

Please don’t reply to a one-year-old post. This was almost certainly just a plain old null reference.

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.