Writing to txt file using textfield problem

Hallo everybody,

I’m trying to store written Strings from a TextField into an external file (.txt file) and then read those strings from the .txt file and load them into an Array for in game usage. While the reading from file function works perfectly i can’t seem to make the write function work. When i build the scene with the textField contained i get an NullReferenceException, but i just can’t seem to find why.
Here is my code:

manualKeyboard Script:

private var currentWordLabel: wordControl;

var stringToEdit : String;
var myStyle : GUIStyle;
var delay = 0;

function Start ()
{
	currentWordLabel = FindObjectOfType(wordControl);
}

function OnGUI () {	
	stringToEdit = GUI.TextField(Rect(100,100,200,20), stringToEdit, 25, myStyle);
		    
    //if (currentWordLabel != null)
    //{
    	currentWordLabel.AddToFile(stringToEdit);
    //}
}

wordControl script:

import System.IO;

var sr : StreamReader;
var sw : StreamWriter;

var wordArray : String[];
var currentWord :String;
var maxAmount : int = 30;
var currentAmount : int = 0;

function Start () {}

function changeWord () {	
	var randomInt = Random.Range(0,currentAmount);		
	currentWord = wordArray[randomInt];	
	guiText.material.color = Color.red;		
	guiText.text = currentWord;	
}

function LoadFile () {
	try {
		// Create an instance of StreamReader to read from a file.
		sr = new StreamReader("words_english.txt");				
		// Count amount of words in file
		currentAmount = 0;		
		while (sr.ReadLine() != null && currentAmount < maxAmount) {
			currentAmount++;
		}
		Debug.Log("Size is:" + currentAmount.ToString());
		wordArray = new String[currentAmount];
				
		sr = new StreamReader("words_english.txt");
		// Fill array with words from file
		var i : int = 0;
		var line : String = sr.ReadLine();
		while (line != null && i < maxAmount) {	
			wordArray *= line;*
  •  	Debug.Log("Added word: " + line);*
    
  •  	line = sr.ReadLine();			*
    
  •  	i++;*
    
  •  }*
    
  •  sr.Close();*
    
  • }*

  • catch (e) {*

  •  // Let the user know what went wrong.*
    
  •  print("The file could not be read:");*
    
  •  print(e.Message);*
    
  • }*
    }

function AddToFile (word : String) {

  • try {*

  •  // Create an instance of StreamWriter to write text to a file.*
    
  •  sw = new StreamWriter("words_english.txt");	* 
    
  •  // Add some text to the file.* 
    
  •  //sw.Write("The date is: ");*
    
  •  sw.WriteLine(word);	* 
    
  •  sw.Close();*
    
  • }*

  • catch (e) {*

  •  // Let the user know what went wrong.*
    
  •  print("File coudn't be accessed:");*
    
  •  print(e.Message);*
    
  • }*

  • LoadFile();*
    }
    Am i doing something wrong or isn’t it possible to write to txt files using textfields?
    Thanks in advance

It looks like currentWordLabel could be null.

You need to make sure it's not null and that it's initialized.

Ironically it looks like you commented out the code that checks for null...

currentWordLabel = FindObjectOfType(wordControl);

if ( currentWordLabel == null ) {
    print("Something is wrong.");
}

Work with word files by the use of fix documents word

Learn given item http://www.fixdoconline.fixdocfiles.com/