Search Unity

NullReferenceException by StartCoroutine();

Discussion in 'Scripting' started by wdpower, Dec 14, 2019.

  1. wdpower

    wdpower

    Joined:
    Nov 11, 2014
    Posts:
    6
    Hi People,

    So I want to create a class called RequestHandler. This class make use of WWW.
    I know there is UnityWebRequest but for now I want to use WWW.

    The class RequestHandler looks like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class RequestHandler : MonoBehaviour
    5. {
    6.     string url;
    7.     string[] fieldNames, fieldValues;
    8.     WWW www;
    9.     WWWForm form;
    10.     public RequestHandler(string urlLink, string[] formFieldNames, string[] formFieldValues){
    11.         Debug.Log("RequestHandler()");
    12.         url = urlLink;
    13.         fieldNames = formFieldNames;
    14.         fieldValues = formFieldValues;
    15.         form = new WWWForm();
    16.         AddFields();
    17.     }
    18.  
    19.     private void Start() {
    20.  
    21.     }
    22.  
    23.     private void AddFields(){
    24.         Debug.Log("AddFields()");
    25.         for(int i = 0; i < fieldNames.Length; i++){
    26.             form.AddField(fieldNames[i], fieldValues[i]);
    27.         }
    28.     }
    29.  
    30.     public void Get(){
    31.         Debug.Log("Get()");
    32.         StartCoroutine("Request");
    33.     }
    34.  
    35.     IEnumerator Request(){
    36.         using(www = new WWW(url)){
    37.             Debug.Log("test");
    38.             yield return www;
    39.         }
    40.     }
    41. }
    42.  
    I have created another script for testing. This script contains a Start method that looks like this:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.  
    4.         string[] fieldNames = new string[]{"loginid", "loginmethod"};
    5.         string[] fieldValues = new string[]{"23232332", "facebook"};
    6.         RequestHandler request = new RequestHandler("unity.local/getchests.php", fieldNames, fieldValues);
    7.         request.Get();
    8.     }
    But when I run the script this NullReferenceException shows up.


    NullReferenceException
    UnityEngine.MonoBehaviour.StartCoroutine (System.String methodName, System.Object value) (at C:/buildslave/unity/build/Runtime/Export/MonoBehaviour.bindings.cs:76)
    UnityEngine.MonoBehaviour.StartCoroutine (System.String methodName) (at C:/buildslave/unity/build/Runtime/Export/MonoBehaviour.bindings.cs:67)
    RequestHandler.Get () (at Assets/RequestHandler.cs:32)
    requesttest.Start () (at Assets/requesttest.cs:11)


    How can I solve this? I read about that you can't use the new keyword with MonoBehaviour but when I look in examples they all use the new keyword including MonoBehaviour.

    I hope someone can help me,
    Greets,
    John
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    WWW class uses unitywebrequest under the hood. That has been a pass through for ages now.
    UnityWebRequest.Post(url, form) is an easy replacement.

    also your class derives from monobehaviour. You can’t use new RequestHandler with that. You’d need a pure C# class for that.

    even more... you forget to send the request...
    Not sure what it was with WWW out of mind. But there should be a Send method. Read the documentation / search examples
     
  3. wdpower

    wdpower

    Joined:
    Nov 11, 2014
    Posts:
    6
    Solved! I created an new game object with AddComponent();
    Then set the url variable and calling AddFields() from the GameObject. After that I called Get() with a succes request.

    Updated code:
    RequestHandler
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class RequestHandler : MonoBehaviour
    5. {
    6.     public string url;
    7.     public string[] fieldNames, fieldValues;
    8.     WWWForm form;
    9.  
    10.     public void AddFields(string[] formFieldNames, string[] formFieldValues){
    11.         form = new WWWForm();
    12.         fieldNames = formFieldNames;
    13.         fieldValues = formFieldValues;
    14.         Debug.Log("AddFields()");
    15.         for(int i = 0; i < fieldNames.Length; i++){
    16.             form.AddField(fieldNames[i], fieldValues[i]);
    17.         }
    18.     }
    19.  
    20.     public void Get(){
    21.         Debug.Log("Get()");
    22.         StartCoroutine("Request");
    23.     }
    24.  
    25.     IEnumerator Request(){
    26.         using(WWW www = new WWW(url, form)){
    27.             yield return www;
    28.             Debug.Log(www.text);
    29.         }
    30.     }
    31. }
    32.  
    requesttest
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class requesttest : MonoBehaviour
    6. {
    7.     private void Start() {
    8.         string[] fieldNames = new string[]{"loginid", "loginmethod"};
    9.         string[] fieldValues = new string[]{"35355", "facebook"};
    10.         RequestHandler requestHandler = (new GameObject("RequestHandler").AddComponent<RequestHandler>());
    11.         requestHandler.url = "unity.local/getchests.php";
    12.         requestHandler.AddFields(fieldNames, fieldValues);
    13.         requestHandler.Get();
    14.     }
    15.  
    16. }
    17.  
     
    Last edited: Dec 14, 2019
  4. wdpower

    wdpower

    Joined:
    Nov 11, 2014
    Posts:
    6
    Is this available for unity 2018? I can't install unity 2019 for some reason. After a while when I try installing it, it abrupt
    automatically. I thought UWR is for unity 2019.

    I'm sending a request with the Request() method. But it was never called because the new RequestHandler
     
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    it has been available for such a long time. All the way back to unity 2017? Maybe even further back. Not sure.
    Unitywebrequest has always been the replacement of WWW. I’m surprised unity still hasn’t removed it. Atleast it’s marked as obsolete.

    Ah yeah my bad. It’s been a while since I used WWW apparently that one directly starts when creating it.
    UnityWebRequest however you need to call SendWebRequest() method to run the request.
     
  6. wdpower

    wdpower

    Joined:
    Nov 11, 2014
    Posts:
    6
    Thank you for your information.
    I will rewrite the class for using UnityWebRequest.