NullReferenceException Error how to solve?

Hello everyone.

I need your help,I don´t know why this error appers, because my game works same with the error.
The error is here:

NullReferenceException: Object reference not set to an instance of an object
Monetario.Update () (at Assets/Script/Botoes/Monetario.cs:18)

My code is here:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Monetario : MonoBehaviour {

public static int coins = 0;
public UnityEngine.UI.Text moneytxt;
// Use this for initialization
void Start () {
	//Guarda o money
	coins = PlayerPrefs.GetInt("coins");
}

// Update is called once per frame
void Update () {
	coins = PlayerPrefs.GetInt("coins");
	moneytxt.text = "Coins: " + coins.ToString();
}

}

Thank you very much

the script seem to be find one thing I would like to correct is
you can directly use

public Text moneytxt;

and you have to drag and drop the "Text " in inspector as it is not getting the text and it is giving null value .

hope this will solve your problem

Hi, You have the moneytext to be a public variable that holds UI.Text. Have you assigned the object for this variable in the editor.

If you are manually creating the object then check if you have instantiated the object.

Attach the process to unity and put a break point in the update and check if the money is not null.
Else simply you can assign the value to the moneytext.text only if the value is not null like this

void Update () {
coins = PlayerPrefs.GetInt(“coins”);
if(moneytxt != null)
moneytxt.text = "Coins: " + coins.ToString();
}