1

I built a login/register system, and I want that when you create a username it checks if email exists, it works fine and it shows the message box "Email exists", but when it is a new user and there is no email that exists, it crashes.

Here is the exception message:

(System.NullReferenceException) Message=The object reference was not set to an object instance

Code:

FirebaseResponse response = await client.GetTaskAsync("Information/" + Emailtextbox.TextName);
Data result = response.ResultAs<Data>();
if (Emailtextbox.TextName == result.Email)
{
    MessageBox.Show("Email exists");
   
} else
{
    
    var data = new Data
    {
        Email = Emailtextbox.TextName,
        Fullname = Fullnametextbox.TextName,
        Password = EncryptSHA.GetShaData(PasswordTextbox.TextName)
    }
        
};
RetroCoder
  • 2,597
  • 10
  • 52
  • 81
Asaf M
  • 11
  • 3
  • 2
    It's hard to say what variable is `Null` here, but I recommend following this pattern to debug it: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Frank van Puffelen Sep 24 '21 at 14:29

1 Answers1

0

Updating this based on the screenshot of the error as well as the information provided in the following comments.

It looks like your error has to do with what's being returned from your client.GetTaskAsync("Information/" + Emailtextbox.Textname); call.

My recommendation would be to try and understand what it is you're receiving from that call (what's stored in your response object). With the latest screenshot I see that the Body is null, and that might be part of the problem. Try expanding what you see in the Response object in your response and see if you're even receiving any kind of data you can use and go from there.

Rrrrrr
  • 50
  • 5
  • its not worked, I am using c# 7.3 can you please give me another sultion, I tried our and cant fix that :( – Asaf M Sep 24 '21 at 17:56
  • Is there any way you'd be able to share more of the code? It'd help to see where `Emailtextbox`, `Fullnametextbox`, and `PasswordTextbox` are coming from. Also, if you could set a break point in the code to see where it throws the error, that would be helpful too. – Rrrrrr Sep 24 '21 at 18:24
  • Also @AsafM, I just updated my answer. In the `( ... is not null)` sections, check if the objects themselves are null (instead of their TextName properties). – Rrrrrr Sep 24 '21 at 18:42
  • Hey you can see it here: https://codeshare.io/zygE47 I marked when it crash with //////////////////////////// and scrol down to see Data class Emailtextbox,Fullnametextbox and Passwordtextbox, came from the form – Asaf M Sep 24 '21 at 18:43
  • Hey @AsafM, thanks for sharing the codeshare.io link. Are you making sure that `EmailTextbox` isn't null? Where you have `if (Emailtextbox.TextName != result.Email)`, before that try adding `if (Emailtextbox is null) { MessageBox.Show("Emailtextbox is null"); }` – Rrrrrr Sep 24 '21 at 18:50
  • Hey, thank you for help, but I tried it and it didn't work :( – Asaf M Sep 24 '21 at 19:03
  • Ah, shoot. So, if you remove everything in the `if(Conn.CheckInternetConnection())` block except for your two `FirebaseResponse response = ...` and `Data result = ...` lines, does the crash still happen? – Rrrrrr Sep 24 '21 at 19:07
  • Hey, check again : https://codeshare.io/zygE47 scroll down to see – Asaf M Sep 24 '21 at 19:15
  • Added to the bottom of it, hopefully you can see my edit @AsafM – Rrrrrr Sep 24 '21 at 19:27
  • Thank you for the help I really appreciate it, unfortunately it crashed again :( – Asaf M Sep 24 '21 at 19:45
  • When it crashes, can you take a screenshot of what you see? Or copy and paste the error text into that codeshare link? – Rrrrrr Sep 24 '21 at 19:47
  • Here is the screenshot : https://ibb.co/rHmqdCV – Asaf M Sep 24 '21 at 20:06
  • Thanks for the screenshot @AsafM, that helps a lot actually. So the issue is something within the result which is causing issues. If you place breakpoints like I do here: https://ibb.co/ZVTYsgm What do you see for your response object? – Rrrrrr Sep 24 '21 at 20:52
  • Hey, you mean this? https://ibb.co/SXs4NpX – Asaf M Sep 24 '21 at 21:02
  • Yes! Would you be able to expand the "response" object, to see what all is inside of it? – Rrrrrr Sep 24 '21 at 21:12
  • Hey, here it is : https://ibb.co/bvV0zwb – Asaf M Sep 24 '21 at 21:25
  • Hey @AsafM, I updated the answer and will leave it at that for now, hopefully it's helpful! It seems like it has something to do with the framework you're using to deal with firebase, and that `GetTaskAsync` call you're making. – Rrrrrr Sep 24 '21 at 21:43