I am using SkiaSharp to develop a small note app in .net Maui. I want to store the data in LiteDB, which works. However, when loading the data from the database, I get the following error message.
System.InvalidCastException: Null object cannot be converted to a value type.
This is the class that I store in the database.
public class NSketch: NotebookItem
{
public List<Stroke> Strokes { get; set; }
[BsonIgnoreAttribute]
public override string Description
{
get { return "Skizze"; }
}
public NSketch()
{
Strokes = new List<Stroke>();
}
}
public class Stroke
{
public SKPaint Paint { get; set; }
public List<SKPoint> Points { get; set; }
public Stroke()
{
Paint = new SKPaint();
Points = new List<SKPoint>();
}
}
and this is the code that causes the error.
//Methode um alle Noitzen in einem bestimmten Notizbuch zu laden und als liste zurückzugeben.
public IList<NSketch> GetByNotebook(int notebookId)
{
using var db = new LiteDatabase(DatabaseName);
var notes = db.GetCollection<NSketch>("nsketch");
try
{
var documents = notes.Find(x => x.NotebookId == notebookId);
// Handle the documents as needed
return documents.ToList();
}
catch (Exception ex)
{
// Handle the exception thrown by the target method (Find)
if (ex.InnerException != null)
{
Console.WriteLine("Inner Exception Message: " + ex.InnerException.Message);
Console.WriteLine("Inner Exception Stack Trace: " + ex.InnerException.StackTrace);
}
// Optionally, rethrow the exception if needed
// throw;
return new List<NSketch>(); // Return an empty list or handle the exception appropriately
}
}
The problem is in this line return documents.ToList();
Can anyone tell me the cause and if possible a solution?
Thanks a lot
I test it with several other classes, and it works fine. I assume it is related to the SKiaSharp class.