Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have problems when displaying data by taking data from JSON (arrays in the array). That is when the gridview selected, then immediately exit the application and will display an error message, as below: error message Code:

private BukuAudio itemDetail = null;

public async void StoreAll()
{
     try
    {
        var client = new Windows.Web.Http.HttpClient();
        string urlPath = "website";
        var values = new List<KeyValuePair<string, string>>
        {

        };
        var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values));
        response.EnsureSuccessStatusCode();
        string jsonText = await response.Content.ReadAsStringAsync();
        JsonObject jsonObject = JsonObject.Parse(jsonText);
        JsonArray jsonData1 = jsonObject["data"].GetArray();

        foreach (JsonValue groupValue in jsonData1)
        {
                JsonObject groupObject = groupValue.GetObject();
                string nid = groupObject["sku"].GetString();
                string title = groupObject["judul"].GetString();
                string deskripsi = groupObject["deskripsi"].GetString();
                string tipe = groupObject["tipe"].GetString();
                var bundleObj = groupObject["bundle"];
                if (bundleObj.ValueType == JsonValueType.Array)
                {
                    JsonArray bundle = bundleObj.GetArray();
                    foreach (JsonValue groupValue1 in bundle)
                {

                    JsonObject groupObject1 = groupValue1.GetObject();
                        string bundleName = groupObject1["bundle_file"].GetString();
                        string pathFile = groupObject1["path_file"].GetString();
                        BukuAudio file1 = new BukuAudio();
                    file1.BundleName = bundleName;
                                file1.Tipe = tipe1;
                                if (file1.Tipe == "0")
                                {
                                    file1.BundlePath = pathFile + bundleName + ".pdf";
                                }
                                else if (file1.Tipe == "1")
                                {
                                    file1.BundlePath = pathFile + bundleName + ".mp3";
                                }
                }
            }

            BukuAudio file = new BukuAudio();
            file.SKU = nid;
            file.Judul = title;
            file.Deskripsi = deskripsi;
            file.Tipe = tipe;

            if (bundleObj.ValueType == JsonValueType.Array)
            {
                datasource.Add(file);
           }
        }

        if (jsonData1.Count > 0)
        {
            itemGridView.ItemsSource = datasource;
        }
    }
    catch
    {
    }

private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
    ProductDetail.IsOpen = true;
    itemDetail = e.ClickedItem as BukuAudio;
    DetailSKU.Text = itemDetail.SKU;
    DetailJudul.Text = itemDetail.Judul;
    DetailDeskripsi.Text = itemDetail.Deskripsi;
    DetailBundleName.Text = itemDetail.BundleName;
    DetailTipe.Text = itemDetail.Tipe;
}

I've debug file1.bundleName and data is not empty, but if it is put on the data becomes null itemDetail.BundleName

How to handle it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
223 views
Welcome To Ask or Share your Answers For Others

1 Answer

Which means that itemDetail.BundleName is null you can solve this issue by the following code:

DetailBundleName.Text = itemDetail.BundleName==null?"":itemDetail.BundleName;

Which will check whether BundleName is null or not, if it is null the assign "" to the DetailBundleName.Text else it assign the value of the BundleName.

Note :- If there is a change for getting null in each property then its a wate effort to check every property every time when you access the value from it. Instead of doing that Handle it in the Property getter; The following code will help you:

class BukuAudio
{
    private string _BundleName;

    public string MyProperty
    {
        get
        {
            if (_BundleName == null) return "";

            return _BundleName;
        }
        set { _BundleName = value; }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...