Lỗi System.NullReferenceException khi code Xamarin.Android (liên quan GameSparks)

Em mới 14 tuổi với lại có ít tài liệu liên quan đến GameSparks nên khi gõ đến đây thì không biết lỗi đó bắt nguồn từ đâu và sửa như thế nào

Lỗi:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. occurred

CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

using GameSparks.Api.Requests;

namespace GreenHub
{
    [Activity(Label = "LoginActivity", ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
    public class LoginActivity : Activity
    {
        public TextView tip_username;
        public TextView tip_password;

        public string username;
        public string password;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_login);
            new _GreenHub().InitGS();

            FindViewById<TextView>(Resource.Id.tip_username).Text = Resources.GetString(Resource.String.tip_username);
            FindViewById<TextView>(Resource.Id.tip_password).Text = Resources.GetString(Resource.String.tip_password);


            FindViewById<Button>(Resource.Id.button_login).Click += LoginActivity_Click; ;
        }

        private void LoginActivity_Click(object sender, EventArgs e)
        {
            username = FindViewById<EditText>(Resource.Id.username).Text;
            password = FindViewById<EditText>(Resource.Id.password).Text;

            if (username != null && username.Length >= 3 && password != null && password.Length >= 8)
            {
                new AuthenticationRequest()
                    .SetUserName(username)
                    .SetPassword(password)
                    .Send((response) =>
                    {
                        if (response.HasErrors)
                        {
                            FindViewById<EditText>(Resource.Id.debug).Append(System.Environment.NewLine + response.ScriptData.GetGSData("error").GetString("DETAILS")); //Cứ thêm dòng này vào là bị lỗi
                        }
                    });
            }
            else
            {
                FindViewById<EditText>(Resource.Id.debug).Append(System.Environment.NewLine + "Vui lòng nhập đủ độ dài");
            }
        }
    }
}

Bạn nên cho thêm thông tin về toàn bộ thông báo lỗi, cụ thể là dòng nào để tiện giải thích cho bạn.
Lỗi xảy ra khi bạn làm gì?


Khi một trường có giá trị null mà bạn gọi đến thuộc tính/phương thức của nó thì xảy ra ngoại lệ này.

3 Likes

Sau khi run app lên debug ý, thì nó hoạt động bình thường, mình cố tình nhập sai thông tin đăng nhập để lấy lỗi không tồn tại thì nó bị vậy

Mình nghĩ lỗi là do dòng này:

FindViewById<EditText>(Resource.Id.debug).Append(System.Environment.NewLine + response.ScriptData.GetGSData("error").GetString("DETAILS"));

Cứ thêm nó vào là bị vậy :frowning_face:
Nếu bạn biết gì về GameSparks thì nói cho mình hiểu nha! Cảm ơn bạn trước :slight_smile:

Phân tích: có 2 vị trí có khả năng gây lỗi:
#1

FindViewById<EditText>(Resource.Id.debug).Append(...);

#2

response.ScriptData.GetGSData("error").GetString("DETAILS");

#1

Khó có khả năng gây lỗi, vì bạn đã thiết kế, và id được tự tạo.

#2

Bạn có chắc là dữ liệu trả về chứa những dữ liệu đó không?

response
ScriptData
GetGSData("error")

Nếu có bất cứ giá trị nào phía trên trả về/bằng null thì ngoại lệ xảy ra.
Bạn nên xét từng giá trị trên khác null.

2 Likes

Ok, Cảm ơn bạn nha! Dữ liệu trả về của mình bị là null

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?