NullReferenceException (Updated)
Hi guys, I am new in game development. I am attempting to send an email through input fields but I have been receiving NullReferenceException when I click the Submit button.
Here are my codes:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
[RequireComponent(typeof(InputField))]
public class FeedbackForm : MonoBehaviour {
InputField emailInputField;
InputField subjectInputField;
InputField messageInputField;
void Start()
{
emailInputField = GetComponent();
subjectInputField = GetComponent();
messageInputField = GetComponent();
}
void SendTheMail(string text)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("censoredmail@gmail.com");
mail.To.Add("censored@gmail.com");
mail.Subject = "Test Mail";
mail.Body = text;
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("censored@gmail.com", "censored") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpServer.Send(mail);
Debug.Log("success");
}
public void OnBackToMainMenuClick()
{
SceneManager.LoadScene("Mainpage");
}
void OnSubmitButtonClick()
{
SendTheMail(messageInputField.text);
}
}
Can you guys help me to point out where is the mistake and how to fix that? Your help is greatly appreciated. Thank you
Answer by merkaba48 · Jul 04, 2017 at 06:36 PM
The error should mention on what line the exception is being raised; that's your starting point.
The NullReferenceException is caused by trying to perform actions on an object that is 'null', i.e. hasn't been initialized yet, or the initialization failed.
As you are doing networking stuff, this is out of my area of expertise, but it's possible that something in your SendTheMail function takes a little bit of time to 'retrieve' an object if it has to communicate over the network before it can create the object. That's just speculation, but something to think about.
The first thing to do is to find out where it's failing; so check the error message, double-click it, and it'll take you to the line of code where it failed (if its in your scripts). The next thing to do, if you still don't know exactly what's wrong, is to debug the script by putting a breakpoint in at a point you think should definitely work OK, attaching to Unity & running, then stepping through the code and confirming the values are what you expect them to be.
Follow this Question
Related Questions
How do I make a script wait X time? 1 Answer
Problem with Score/Highscore script 0 Answers
How to assign GameObject to Script? 1 Answer
InvalidOperationException thrown 0 Answers
how do I rezolve "look rotation viewing vector is zero"? 1 Answer