Search Unity

Registering to Database but get this error: Object reference not set to an instance of an object

Discussion in 'Scripting' started by lynts, Oct 17, 2017.

  1. lynts

    lynts

    Joined:
    Feb 14, 2014
    Posts:
    4
    I commented the lines 32 and 127 where it says this error takes place cant figure it out



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DataInserter : MonoBehaviour
    {

    public GameObject username;
    public GameObject email;
    public GameObject password;

    private string inputUserName;
    private string inputPassword;
    private string inputEmail;
    private bool EmailValid = false;
    private string[] Characters =
    {
    "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
    "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
    "1","2","3","4","5","6","7","8","9","0","_","-" };

    string CreateUserURL = "http://hotbaevids.com/DataStackinCams/InsertUser.php";

    public void RegisterButton()
    {
    bool EM = false;
    bool PW = false;

    if (inputEmail != "")
    {
    EmailValidation();
    if (EmailValid) //this line is error line
    {
    if (inputEmail.Contains("@"))
    {
    if (inputEmail.Contains("."))
    {
    EM = true;
    }
    else
    {
    Debug.LogWarning("Email is Incorrect");
    }
    }
    else
    {
    Debug.LogWarning("Email is Incorrect");
    }
    }
    else
    {
    Debug.LogWarning("Email is Incorrect");
    }
    }
    else
    {
    Debug.LogWarning("Email Field Empty");
    }
    if (inputPassword != "")
    {
    if (inputPassword.Length > 5)
    {
    PW = true;
    }
    else
    {
    Debug.LogWarning("Password Must Be at Least 6 Characters Long");
    }
    }
    else
    {
    Debug.LogWarning("Password Field Empty");
    }
    if (EM == true && PW == true)
    {
    bool Clear = true;
    int i = 1;
    foreach (char c in inputPassword)
    {
    if (Clear)
    {
    inputPassword = "";
    Clear = false;
    }
    i++;
    char Encrypted = (char)(c * i);
    inputPassword += Encrypted.ToString();

    }
    }
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Space)) CreateUser(inputUserName, inputPassword, inputEmail);

    if (Input.GetKeyDown(KeyCode.Return))
    {
    CreateUser(inputUserName, inputPassword, inputEmail);

    if (inputUserName != "" && inputEmail != "" && inputPassword != "")
    {
    RegisterButton();
    }
    }

    }

    public void CreateUser(string username, string password, string email)
    {
    WWWForm form = new WWWForm();
    form.AddField("usernamePost", username);
    form.AddField("passwordPost", password);
    form.AddField("emailPost", email);

    WWW www = new WWW(CreateUserURL, form);

    }
    void EmailValidation()
    {
    bool SW = false;
    bool EW = false;
    for (int i = 0; i < Characters.Length; i++)
    {
    if (inputEmail.StartsWith(Characters))
    { //error line
    SW = true;
    }
    }
    for (int i = 0; i < Characters.Length; i++)
    {
    if (inputEmail.EndsWith(Characters))
    {
    EW = true;
    }
    }
    if (SW == true && EW == true)
    {
    EmailValid = true;
    }
    else
    {
    EmailValid = false;
    }
    }
    }
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Hi Lynts, please use Code Tags when pasting code on this forum; https://forum.unity.com/threads/using-code-tags-properly.143875/

    With regards to the error, your "Characters" array has been passed as an argument where it's expecting a string. You need to properly access the array item in your loop...
    Code (CSharp):
    1. inputEmail.StartsWith(Characters[i]))
    2. inputEmail.EndsWith(Characters[i]))
    However, there are better ways to validate strings in C# via the RegEx class. There's a good article covering email validation along with a copy and paste code example, here; Validate Email
     
    Last edited: Oct 18, 2017
  3. lynts

    lynts

    Joined:
    Feb 14, 2014
    Posts:
    4
    thanks man ill look at this right now