-1

I have an issue with Null Reference on local variable that set by var argument inside start() method

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using K3DBHandler;

public class Splash : MonoBehaviour {
    private int jmlUser;
    private DataService ds = new DataService("dbK3.sqlite");

    void Start()
    {
       var user = ds.CekUser();
        Hitung(user);
        if (jmlUser == 0)
        {
            StartCoroutine(ToLogin());
        }
        else
        {
            StartCoroutine(ToHome());
        }
    }

  IEnumerator ToHome()
  {
        yield return new WaitForSeconds(5);
        SceneManager.LoadScene("Home");
  }

    IEnumerator ToLogin()
    {
        yield return new WaitForSeconds(5);
        SceneManager.LoadScene("Login");
    }
    private void Hitung(IEnumerable<User> UserCount)
    {
        var c = 0;
        foreach (var a in UserCount)
        {
            c++;
        }
        jmlUser = c;
    }
}

This code work well in Unity Editor but when I build it to Android, I got error like this: Null Reference Exception

Please help me.

*Note: I use Unity 2017.3.1f1

Supriya
  • 290
  • 4
  • 15
  • 1
    If you double-click on that error from the Editor, it will take you to the line of code that is causing that. – Programmer Apr 03 '18 at 07:08

1 Answers1

0

Unity is telling you that one of the things in the Start method doesn't exist.

I have organized your code, so it will be easier to detect this object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using K3DBHandler;

public class Splash : MonoBehaviour {
    private int jmlUser = 0;
    private DataService ds = null;

    void Start()
    {
       ds = new DataService("dbK3.sqlite");
       var user = ds.CekUser();
       Hitung(user);
       if (jmlUser == 0) StartCoroutine(ToLogin());
       else StartCoroutine(ToHome());
    }

    IEnumerator ToHome()
    {
        yield return new WaitForSeconds(5);
        SceneManager.LoadScene("Home");
    }

    IEnumerator ToLogin()
    {
        yield return new WaitForSeconds(5);
        SceneManager.LoadScene("Login");
    }

    private void Hitung(IEnumerable<User> UserCount)
    {
        int c = 0;
        foreach (var a in UserCount) c++;
        jmlUser = c;
    }
}
Héctor M.
  • 2,302
  • 4
  • 17
  • 35