-18

I have been working on a simple android app using Xamarin.android for a while, I've just started on mobo development so it's pretty new to me, the thing is my app is supposed to be a login and registration form. I use a class to get and organize the data in List<T>, thing is I get null exception here is my code

It is most likely a simple mistake, the weirdest thing is that it was working at some point when I tried to add sth else, didn't work deleted it and then this happened

ty in advance

using System;
using System.Collections.Generic;
using System.Linq;

namespace DemoAppXamarinStu {

public class Datos
{
    public static List<string> UsuarioL;//= new List<string>()
    static List<string> NombreL;//= new List<string>()
    static List<string> ApellidoL;//= new List<string>()
    static List<string> EmailL;// = new List<string>()
    public static List<string> ContraseñaL;// = new List<string>();

    public Datos()
    {
        UsuarioL = new List<string>();
        NombreL = new List<string>();
        ApellidoL = new List<string>();
        EmailL = new List<string>();
        ContraseñaL = new List<string>();

        UsuarioL.Add("admin");
        ContraseñaL.Add("admin");
        NombreL.Add("administrador");
        ApellidoL.Add("administrador");
        EmailL.Add("admin@admin.com");              
    }
    public static void agregar(string value1, string value2, string value3, string value4, string value5) 
    {
        //Datos d = new Datos();
        UsuarioL.Add(value1);
        NombreL.Add(value2);
        ApellidoL.Add(value3);
        EmailL.Add(value4);
        ContraseñaL.Add(value5);
    }
}
}

oh and also check this out an an exception on a comment

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
SoulBlack
  • 41
  • 8

4 Answers4

2

You are trying to add object to the list that was not instanced. You are using static method so constructor is not called so your list was not created.

kriss
  • 976
  • 17
  • 27
2

That's because you are using static method.

When static methods are called the constructor is not called (and your list is not initialized). Static methods are called on class but constructor come into picture when you are making an object.

Do-It-Yourself: Applying break points on both method and constructor and would see that constructor one would never hit.

To resolve this, initialize them like this (basically uncomment your code and it will work fine.)

public static List<string> FooBar = new List<string>();
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
2

You are accessing a static field from a static method and you instantiate your list in the constructor of the class. I don't thing that is what you want to do.

Each time you instantiate your class, you will re-instantiate your static field. And you need to instantiate your class once for instantiate the static field...

Why not uncomment the // new List<String>() and remove in the constructor?

Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
1

You're using static data member in agregar() method and that time you not initialized your all your List. You're initializing all static data member in Datos constructor. Constructor never called.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65