Issue
I select the data from the meeting table and assign it an adapter. In doing so, I get a System.NullReferenceException: Object reference not set to an instance of an object.
private void LoadData()
{
listSource = DBConnection.selectTable();
var adapter = new ListViewAdapter(this, listSource);
lstViewData.Adapter = adapter;
}
static public List<Meeting> selectTable()
{
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(Folder, "Organizer.db")))
{
return connection.Table<Meeting>().ToList();
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return null;
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
EditText passwordEditText = FindViewById<EditText>(Resource.Id.passwordEditText);
lstViewData = FindViewById<ListView>(Resource.Id.listView);
}
Meeting table structure:
[Activity(Label = "AddEvent")]
public class Meeting
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int idLogin { get; set; }
public int idContact { get; set; }
public string Place { get; set; }
public string Comment { get; set; }
public string Date { get; set; }
public string Time { get; set; }
}
I do this by analogy with this project: https://www.c-sharpcorner.com/article/xamarin-android-sqlite-database/
Solution
First of all, why you add the Activity attribute above the Meeting, If Meeting is not a activity, please remove it.
Then I wrote a demo about it. Here is running screenshot.
If you get System.NullReferenceException: Object reference not set to an instance of an object.
Please notice your Listview if it is null, If so, You shoul move the FindViewById of ListView to above the LoadData method.
lstViewData = FindViewById<ListView>(Resource.Id.listView);
edtidLogin = FindViewById<EditText>(Resource.Id.edtidLogin);
edtidContact = FindViewById<EditText>(Resource.Id.edtidContact);
edtPlace = FindViewById<EditText>(Resource.Id.edtPlace);
var btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
db = new Database();
db.createDatabase();
Here is my mainActivity code.
public class MainActivity : AppCompatActivity
{
ListView lstViewData;
List<Meeting> listSource = new List<Meeting>();
Database db;
EditText edtidLogin;
EditText edtidContact;
EditText edtPlace;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
lstViewData = FindViewById<ListView>(Resource.Id.listView);
edtidLogin = FindViewById<EditText>(Resource.Id.edtidLogin);
edtidContact = FindViewById<EditText>(Resource.Id.edtidContact);
edtPlace = FindViewById<EditText>(Resource.Id.edtPlace);
var btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
db = new Database();
db.createDatabase();
// InsertData(db);
btnAdd.Click += BtnAdd_Click;
LoadData();
}
private void BtnAdd_Click(object sender, EventArgs e)
{
var meeting= new Meeting() { idLogin= Int32.Parse( edtidLogin.Text), idContact= Int32.Parse(edtidContact.Text) , Place= edtPlace.Text };
db.insertIntoTable(meeting);
LoadData();
}
private void InsertData(Database db)
{
//throw new NotImplementedException();
Meeting meeting = new Meeting() {
idContact = 1,
idLogin = 1,
Place="Place1"
};
db.insertIntoTable(meeting);
Meeting meeting2 = new Meeting()
{
idContact = 2,
idLogin = 2,
Place = "Place2"
};
db.insertIntoTable(meeting2);
LoadData();
}
private void LoadData()
{
listSource = db.selectTable();
var adapter = new ListViewAdapter(this, listSource);
lstViewData.Adapter = adapter;
}
Here is my demo. https://github.com/851265601/ListviewAndSqlite
Answered By - Leon Lu - MSFT

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.