Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ArgumentNullException: argument cannot be null ?

Discussion in 'Scripting' started by veleno94, Dec 8, 2015.

  1. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    When I run my game I get this message: "ArgumentNullException: argument cannot be null". Where am I wrong?
    This is my script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6. using System.IO;
    7.  
    8.  
    9. public class test : MonoBehaviour {
    10.     string[] word = new string[20];
    11.     public InputField iField;
    12.     private string inputLetter;
    13.     public Text showWord;
    14.     private int times = 0;
    15.    
    16.  
    17.     private string text;
    18.     private string finalWord;
    19.  
    20.  
    21.     void Start(){
    22.         text = File.ReadAllText (@"Assets\myWordlist.txt");
    23.  
    24.     }
    25.  
    26.     public void OnClick_GO(){
    27.         times = times + 1;
    28.         inputLetter = iField.text;
    29.         word [times] = inputLetter;
    30.         Debug.Log(word[times]);
    31.         finalWord = string.Join ("", word);
    32.         Debug.Log ("Word: " + finalWord);
    33.         showWord.text = finalWord;
    34.     }
    35.  
    36.  
    37.  
    38.     public void OnClick_V(){
    39.         if (text.Contains(finalWord))
    40.             Debug.Log ("Word exists");
    41.         else
    42.             Debug.Log ("No");
    43.     }
    44.  
    45.  
    46. }
     
  2. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    Which line? :)
     
  3. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    Unfortunately only comes out this message, it does not tell me in which line.:) After it says "parameter name: value".
     
  4. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    If you open the Console and click on the error message you'll get a more detailed description.
     
  5. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    ArgumentNullException: Argument cannot be null.
    Parameter name: value
    System.String.IndexOf (System.String value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/String.cs:1185)
    System.String.Contains (System.String value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/String.cs:1429)
    prova.OnClick_V () (at Assets/test.cs:44)
    UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:149)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:626)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:766)
    UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:54)
    UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
    UnityEngine.EventSystems.EventSystem:Update()
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    looks like "finalWord" is null at the point when the OnClick_V function is being called...
     
  7. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    As @LeftyRighty said, the argument passed to this method is null.
     
  8. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    Is there a way I can use the variable "final word" in the OnClick_V() function?
     
  9. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    It looks like "finalWord" is initialized in OnClick_GO but I don't know when that method is invoked in your project.

    Try also:

    Code (CSharp):
    1. if (!string.IsNullOrEmpty(finalWord) && text.Contains(finalWord))
    2.     Debug.Log ("Word exists");
    3. else
    4.     Debug.Log ("No");
     
  10. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    I tried with this:
    but it returns me "No" also if the word is present in the wordlist.
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    He's been doing some sort of word game, functions are called via UI button event calls.
     
  12. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Simplest solution is likely to be setting the string as "" in start and just checking if !="" in you button's function. It'll always be something even if you call that function before the other one.
     
  13. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    @veleno94 Are you sure OnClick_GO gets executed?

    Edit:
    Please give more details about the project. Also specify when OnClick_GO and OnClick_V are executed.
     
  14. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    First of all thank you for advices you are giving me.

    @ung OnClick_GO is performed correctly. In simple terms, I insert a word in the inputfield and, pressing the button "GO", the word appears on the screen. When I press the "V" button, it should check whether the word is present in the wordlist.

    @LeftyRighty I tried your way. It results that the variable "finalWord" remains empty. Then when I call the function OnClick_GO the variable "finalWord" is changed only within the function, and not generally in the script instead.
     
  15. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    that would suggest you're manipulating a local variable within the function and not the class variable... although i don't see that in the code you posted above :confused: