• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by IKarmaUK · Apr 11, 2017 at 11:22 AM · errornullreferenceexceptionnullnull reference exceptionenumerate

Why am I getting a null reference error?

I have a database/login setup in Unity, and have this piece of code that runs when a correct password and username is entered.

However, I keep getting a NullReferenceException error midway through the login process. Here is my code:

 IEnumerator sendLoginRequest(string username, string password)
     {
 
         if (isDatabaseSetup == true)
         {
 
             IEnumerator e = DatabaseControl.DCF.Login(username, password);
             while (e.MoveNext())
             {
                 yield return e.Current;
             }
 
             WWW returned = e.Current as WWW;
 
             if (returned.text == "incorrectUser")
             {
                 //Account with username not found in database
                 login_error.text = "Username not found";
                 part = 0; //back to login UI
             }
             if (returned.text == "Success")
             {
                 //Password was correct
                 blankErrors();
                 part = 2; //show logged in UI
 
                 //blank username field
                 input_login_username.text = ""; //password field is blanked at the end of this function, even when error is returned
 
                 UserAccountManager.instance.LogIn(username, password);
             }
             if (returned.text == "incorrectPass")
             {
                 //Account with username found, but password incorrect
                 part = 0; //back to login UI
                 login_error.text = "Incorrect Password";
             }
             if (returned.text == "ContainsUnsupportedSymbol")
             {
                 //One of the parameters contained a - symbol
                 part = 0; //back to login UI
                 login_error.text = "Unsupported Symbol '-'";
             }
             if (returned.text == "Error")
             {
                 //Account Not Created, another error occurred
                 part = 0; //back to login UI
                 login_error.text = "Database Error. Try again later.";
             }
 
             //blank password field
             input_login_password.text = "";
 
 
         }
     }

And here is the error message I get:

NullReferenceException: Object reference not set to an instance of an object LoginMenu+c__Iterator0.MoveNext () (at Assets/Scripts/LoginMenu.cs:160)

The specific line it mentions (160) is this:

  if (returned.text == "incorrectUser")

Any idea how to get rid of this error?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by ComradeVanti · Apr 11, 2017 at 01:15 PM

It seems like "returned" is null.

This means that e.Current is some type that cannot be cast to WWW. If you cast something that cannot be cast it simply defaults to null. I would check what e.Current even is and go from there.

Try:

 Debug.Log(e.Current.GetType());
Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IKarmaUK · Apr 11, 2017 at 02:23 PM 0
Share

I tried what you suggested, and the debug log returns 'System.String.' I don't know what that means for the error?

avatar image ComradeVanti IKarmaUK · Apr 14, 2017 at 12:29 PM 0
Share

Im not really familiar with WWW but can you cast a string to a WWW? If so i dont understand the error either. But if you cant cast a string to a WWW that makes more sense.

Its like doing this:

 SpriteRenderer renderer = (SpriteRenderer)myPolygonCollider2D;

C#/Unity cant handle that and makes it null.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Space Shooter Tutorial] NullReferenceException: Object reference not set to an instance of an object PlayerController.FixedUpdate () 1 Answer

null reference exception help 1 Answer

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

Need some help with networking, Object reference not set to an instance of an object. 0 Answers

NullRefrenceException: Object refrence not set to an instance of an object. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges