1

I am an Android developer and new to Xamarin. What I am trying to do is, I am getting a JSON request from server and added it into a List(Object). Then I want to check some values inside the List and according to that added those data to a new List(same structure). I have done this using JAVA in Android but in c# that method not working. When I try to add values to the new List, loop breaks in the first place.

My service...

public async Task<List<FpRankData>> GetFpData(string accessToken)
{
    List<FpRankData> myFpList = null;
    try
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
        var json = await client.GetStringAsync("http://203.115.11.236:10455/MobileAuthWS/api/Agent/GetFP2018Rank");
        myFpList = JsonConvert.DeserializeObject<List<FpRankData>>(json);
    }
    catch (Exception e)
    {
        myFpList = null;
        Debug.WriteLine("fp_error : ", e);
    }
    return myFpList;

}

Method that do the check...

    public List<FpRankData> fpFullList;
    public List<FpRankData> fpTempList;
    public List<FpRankData> fpRankList

    public async Task getFpDataAsync(string accessToken)
    {
        IsBusy = true;

        fpFullList = await _apiServices.GetFpData(accessToken);

        foreach (FpRankData item in fpFullList) {

            if (item.agentCode1.Equals(Settings.agentCode))
            {

                item.AgentName = ExceptBlanks(item.AgentName);

                fpTempList.Add(item);

                if (item.rank == 1)
                {
                    List<FpRankData> finList1 = new List<FpRankData>(fpFullList.GetRange(1, 1));
                    fpTempList.AddRange(finList1);
                }
                else
                {
                    List<FpRankData> finList1 = new List<FpRankData>(fpFullList.GetRange(0, 1));
                    fpTempList.AddRange(finList1);
                }

                if (item.rank == 110)
                {
                    List<FpRankData> finList110 = new List<FpRankData>(fpFullList.GetRange(108, 1));
                    fpTempList.AddRange(finList110);
                }
                else
                {
                    List<FpRankData> finList110 = new List<FpRankData>(fpFullList.GetRange(109, 1));
                    fpTempList.AddRange(finList110);
                }

                if (item.rank != 30)
                {
                    List<FpRankData> finList30 = new List<FpRankData>(fpFullList.GetRange(29, 1));
                    fpTempList.AddRange(finList30);
                }
                if (item.rank != 60)
                {
                    List<FpRankData> finList60 = new List<FpRankData>(fpFullList.GetRange(59, 1));
                    fpTempList.AddRange(finList60);
                }

                Debug.WriteLine(item.AgentName, " AgentName");
                Debug.WriteLine("");

            }
            else {
                Debug.WriteLine("AgentName ", " AgentName");
            }

        }

        fpRankList = fpTempList;

        Debug.WriteLine("ListCount : ", fpTempList.Count.ToString());

        IsBusy = false;

    }

    public string ExceptBlanks(string str)
    {
        string pattern = "\\s+";
        string replacement = " ";                      
        Regex rx = new Regex(pattern);
        string result = rx.Replace(str, replacement);
        return result;
    }

I have save my agent's code using Settings and I compare it with the List.

public class FpRankData
{
    public string agentCode1 { get; set; }
    public string agentCode2 { get; set; }
    public string AgentName { get; set; }
    public string Role { get; set; }
    public string branchCode { get; set; }
    public string branchName { get; set; }
    public string region { get; set; }
    public int nop_count { get; set; }
    public double fp { get; set; }
    public string lastPersistencyYear { get; set; }
    public string persistencyValue { get; set; }
    public int rank { get; set; }
    public string acheivement { get; set; }
}

What I did in Java android...(this works)

    ArrayList<UserData> finList = new ArrayList<>();
    finList.clear();

    for (UserData customer : rList) {

        if (customer.getAgentCode().equals(rAgentCode)) {

            finList.add(customer);
            if (customer.getuPosition() == 1) {
                ArrayList<UserData> finList1 = new ArrayList<>(rList.subList(1, 2));
                finList.addAll(finList1);
            } else {
                ArrayList<UserData> finList1 = new ArrayList<>(rList.subList(0, 1));
                finList.addAll(finList1);
            }

            if (customer.getuPosition() == 110) {
                ArrayList<UserData> finList110 = new ArrayList<>(rList.subList(108, 109));
                finList.addAll(finList110);
            } else {
                ArrayList<UserData> finList110 = new ArrayList<>(rList.subList(109, 110));
                finList.addAll(finList110);
            }

            if (customer.getuPosition() != 30) {
                ArrayList<UserData> finList30 = new ArrayList<>(rList.subList(29, 30));
                finList.addAll(finList30);
            }

            if (customer.getuPosition() != 60) {
                ArrayList<UserData> finList60 = new ArrayList<>(rList.subList(59, 60));
                finList.addAll(finList60);
            }

    }
abatishchev
  • 98,240
  • 88
  • 296
  • 433
D.madushanka
  • 429
  • 6
  • 17

2 Answers2

2

Looks like you haven't instantiated the fpTempList and fpRankList probability you are getting the NullReferenceException while trying to add items to those lists. So either you can initialize them at the time of declaration as lkie the following or you can check for null and initialize them in the getFpDataAsync() method[before using them]:

 public List<FpRankData> fpTempList = new List<FpRankData>();
 public List<FpRankData> fpRankList = new List<FpRankData>();
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
1

You have to initialize fpTempList and fpRankList with new List as below:

public List<FpRankData> fpTempList = new List<FpRankData>();
public List<FpRankData> fpRankList = new List<FpRankData>();

You can't add any object to List without initialization, it will give you NullReferenceException.

Yogesh Patel
  • 818
  • 2
  • 12
  • 26