0

I am facing a problem with a tutorial. I have been working with hardcoded data (working fine) and now I am trying to work with live data from firebase.

I already have some data in my database and I am trying to do a fetch request (get request) to update my RecyclerView. Currently, my RecyclerView is filled with the hardcoded data. What I want to achieve is to add the data in my firebase to update my RecyclerView. basically.

Once the app is deployed on the phone and I try to save data, the function to save data works fine. I am able to see the data saved in real time in my firebase. But an Exception is thrown when it gets to the updateGameDataList function in my GameActivity.cs.

The exception points to this line: recycle.SetAdapter(adpater); and throws a System.NullReferenceException.

What could I be missing or doing so wrong? Thank you

Games.cs

public class Games
{
    private string GameName;
    private int GameImage;

    public string Name
    {
        get { return GameName; }
        set { GameName = value; }
    }

    public int Image
    {
        get { return GameImage; }
        set { GameImage = value; }
    }
}

AllGames

// this is where I hardcoded my data for practice.
public class AllGames 
{
    public static JavaList<Games> GetAllGames()
    {
        JavaList<Games> getGames = new JavaList<Games>();

        Games adventureGames = new Games();

        adventureGames.Name = "Heroes";
        adventureGames.Image = Resource.Drawable.game1;

        getGames.Add(adventureGames);
    }
}

GameFragment

public class GameFragment : SupportFragment
{    
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // GameDataList contains recycleView to display all games
        // inflated into List_AllGames

        RecyclerView recyclerView = inflater.Inflate(Resource.Layout.GameDataList, container, false) as RecyclerView;

        GameRecyclerView(recyclerView);

        return recyclerView;
    }

    private void GameRecyclerView(RecyclerView recyclerView)
    {
        var bindGames = AllGames.GetAllGames();

        recyclerView.SetLayoutManager(new LinearLayoutManager(recyclerView.Context));

        var Adapter = new GameAdapter(AllGames.GetAllGames());

        recyclerView.SetAdapter(Adapter);

        Adapter.NotifyDataSetChanged();

        recyclerView.SetItemClickListener((rv, position, view) =>
        {
            Context context = view.Context;
            Intent intent = new Intent(context, typeof(GamesAttributesActivity));
            context.StartActivity(intent);
        });
    }

// Game Adapter here - this is the same adapter I used
// to display the hard coded data 
public class GameAdapter : RecyclerView.Adapter
{
    private JavaList<Games> myGames;

    public EventAdapter(JavaList<Games> myGames)
    {
        this.myGames = myGames;
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {            
        GameHolder adventure = holder as GameHolder;
        adventure.GameName.Text = myGames[position].Name;
        adventure.GameImage.SetImageResource(peaceEvents[position].Image);
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View games = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.List_AllGames, parent, false);
        GameHolder holder = new GameHolder(games);

        return holder;
    }

    public override int ItemCount
    {  
        get { return myGames.Size(); }
    }
}

//Same holder used to hold my hardcoded data    
public class GameHolder : RecyclerView.ViewHolder
{    
    public TextView GameName;
    public ImageView GameImage;

    public GameHolder(View itemView) : base(itemView)
    {        
        //binding to List_AllGames inflated in GameFragment
        GameImage = itemView.FindViewById<ImageView>(Resource.Id.displayGameImage);
        GameName = itemView.FindViewById<TextView>(Resource.Id.displayGameText);
    }
}

GameActivity

private RecyclerView recycle;
JavaList<Games> NewGames = new JavaList<Games>();

private async void CreateNewGames()
{
    //code to add games      
    var firebase = new FirebaseClient(FirebaseUrl);

    //add item
    var item = await firebase.Child("Games").PostAsync<PeaceEvents>(addGames);

    await UpdateGameDataList();
}

//where recycleView must update new games added to the list
private async Task UpdateGameDataList()
{
    var firebase = new FirebaseClient(FirebaseUrl);
    var items = await firebase.Child("Games").OnceAsync<PeaceEvents>();

    NewGames.Clear();

    GameAdapter adapter;

    foreach(var item in items)
    {
        Games fetchGames = new Games();
        fetchGames.Name = item.Object.Name;
        NewGames.Add(fetchGames);
    }

    //fetching the id of the GameDataList recycleView
    recycle = FindViewById<RecyclerView>(Resource.Id.recyclerview);

    adapter = new GameAdapter(NewGames);
    recycle.SetAdapter(adapter);
    recycle.SetLayoutManager(new LinearLayoutManager(this));
    adapter.NotifyDataSetChanged();
}
Massimiliano Kraus
  • 3,214
  • 5
  • 20
  • 40
XamarinDevil
  • 773
  • 4
  • 13
  • 32
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Paul Karam Jun 08 '17 at 08:33
  • By the looks of it, the method FindViewById is returning null in the method UpdateGameDataList. Therefore setting the variable "recycle" to null meaning you are unable to call the SetAdapter method. – TheTerribleProgrammer Jun 08 '17 at 08:35
  • @TheTerribleProgrammer is there a way i could reach the GameDataList recyclerView which happens to be in my Layout from GameActivity Page? – XamarinDevil Jun 08 '17 at 09:08
  • @PaulKaram, Thanks for the reference. But any help as to how i could reach the id from the layout folder? I have been trying to reach it but still null – XamarinDevil Jun 08 '17 at 10:08
  • @Beginner are you sure it's FindViewById (Resource.Id.recyclerview); and not FindViewById (Resource.Id.recyclerView); ? – TheTerribleProgrammer Jun 08 '17 at 13:38
  • @TheTerribleProgrammer i am very sure of it. I am using few number of recycleviews so i have id's for all of them. Now the recycleView i am trying to access doesn't belong to the activity that i am trying to reach its id from – XamarinDevil Jun 08 '17 at 14:23
  • FindViewByID looks in the main view of the current activity, so if your RecyclerView is not in the main View of the current activity, then you will get null. – jgoldberger - MSFT Jun 23 '17 at 00:20

0 Answers0