0

Everytime I try to submit the form to create a new boat I get the System.NullReferenceException: 'Object reference not set to an instance of an object.' error.

This is the code in question, the boat model:

using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiAVDApp2.Models
{
    public class Boat
    {
        [PrimaryKey, AutoIncrement]
        public int? BoatId { get; set; }
        public string? BoatName { get; set; }
        public string? BoatDescription { get; set; }
        public string? BoatCity { get; set; }
        public BoatType BoatType { get; set; }
        public string? BoatManufacturer { get; set; }
        public string? BoatModel { get; set; }
        public string? BoatImage { get; set; }
        public int? BoatSeats { get; set; }
        public double? BoatPrice { get; set; }
        public Boolean? BoatWithSkipper { get; set; }
        public Boolean? BoatWithLicense { get; set; }
        public double? BoatLatitude { get; set; }
        public double? BoatLongitude { get; set; }
        public double? BoatDistance { get; set; }
        //public Host BoatHost { get; set; }
    }
}

The boatservice:

using MauiAVDApp2.Models;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiAVDApp2.Services
{
    internal class BoatService : IBoatService
    {
        private SQLiteAsyncConnection _dbConnection;

        public BoatService()
        {

        }

        private async void SetupDb()
        {
            if (_dbConnection == null)
            {
                string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "BoatApplication.db3");
                _dbConnection = new SQLiteAsyncConnection(dbPath);
                await _dbConnection.CreateTableAsync<Boat>();
            }
        }
        public async Task<int> AddBoat(Boat boat)
        {
            return await _dbConnection.InsertAsync(boat);
        }

        public async Task<int> DeleteBoat(Boat boat)
        {
            return await _dbConnection.DeleteAsync(boat);
        }

        public async Task<List<Boat>> GetBoatListAsync(Boat boat)
        {
            var boatList = await _dbConnection.Table<Boat>().ToListAsync();
            return boatList;
        }

        public async Task<int> UpdateBoat(Boat boat)
        {
            return await _dbConnection.UpdateAsync(boat);
        }
    }
}

The iboatservice:

using MauiAVDApp2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiAVDApp2.Services
{
    internal interface IBoatService
    {
        Task<List<Boat>> GetBoatListAsync(Boat boat);
        Task<int> AddBoat(Boat boat);
        Task<int> UpdateBoat(Boat boat);
        Task<int> DeleteBoat(Boat boat);

    }
}

The blazor component:

@page "/add-boat"
@using MauiAVDApp2.Services
@using MauiAVDApp2.Models
@inject IBoatService boatService

<h3>Add Boat</h3>

<div class="container">
    <div class="row">
        <form @onsubmit="FormSubmit">
            <div class="mb-3">
                <label for="exampleInputName1" class="form-label">Name:</label>
                <input type="text" class="form-control" id="exampleInputName1" placeholder="Bautista" aria-describedby="nameHelp" @bind="boat.BoatName">
            </div>
            <div class="mb-3">
                <label for="form-select" class="form-label">Choose boat type</label>
                <select class="form-select" id="form-select" aria-label="Default select example" @bind="boat.BoatType">
                    <option value="0">Choose your boat type</option>
                    @foreach (var boatType in Enum.GetValues(typeof(BoatType)))
                    {
                        if ((int)boatType == (int)boat.BoatType)
                        {
                            <option value="@((int)boatType)" selected>@boatType</option>
                        }
                        else
                        {
                            <option value="@((int)boatType)">@boatType</option>
                        }
                    }
                </select>

            </div>


            <div class="mb-3">
                <label for="exampleInpuDescription1" class="form-label">Description:</label>
                <input type="text" class="form-control" id="exampleInputDescription1" placeholder="Bestes boat ever" @bind="boat.BoatDescription">
            </div>
            <div class="mb-3">
                <label for="exampleInputCity1" class="form-label">City:</label>
                <input type="text" class="form-control" placeholder="Amsterdam" id="exampleInputCity1" @bind="boat.BoatCity">
            </div>
            <div class="mb-3">
                <label for="exampleInpuDescription1" class="form-label">Manufacturer:</label>
                <input type="text" class="form-control" placeholder="Yamaha" id="exampleInputDescription1" @bind="boat.BoatManufacturer">
            </div>
            <div class="mb-3">
                <label for="exampleInpuDescription1" class="form-label">Model</label>
                <input type="text" class="form-control" placeholder="GXR" id="exampleInputDescription1" @bind="boat.BoatModel">
            </div>
            <div class="mb-3">
                <label for="exampleInpuDescription1" class="form-label">Image</label>
                <input type="text" class="form-control" placeholder="https//:www.google.com/ray-bautista" id="exampleInputDescription1" @bind="boat.BoatImage">
            </div>
            <div class="mb-3">
                <label for="exampleInpuDescription1" class="form-label">Seats</label>
                <input type="number" class="form-control" placeholder="3" id="exampleInputDescription1" @bind="boat.BoatSeats">
            </div>
            <div class="mb-3">
                <label for="exampleInpuDescription1" class="form-label">Price</label>
                <input type="number" class="form-control" placeholder="3.00" id="exampleInputDescription1" @bind="boat.BoatPrice">
            </div>
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckChecked" @bind="boat.BoatWithSkipper">
                <label class="form-check-label" for="flexSwitchCheckChecked">With Skipper</label>
            </div>
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckChecked" @bind="boat.BoatWithLicense">
                <label class="form-check-label" for="flexSwitchCheckChecked">Customer needs license</label>
            </div>
            <button type="submit" class="btn btn-primary mt-3 d-block">Submit</button>
        </form>
    </div>
</div>

@code {
    Boat boat = new Boat();

    private async void FormSubmit()
    {
        var response = await boatService.AddBoat(boat);
        if (response > 0)
        {
            boat.BoatSeats = 0;
            boat.BoatPrice = 0.00;
            boat.BoatWithLicense = boat.BoatWithSkipper = false;
            boat.BoatCity = boat.BoatManufacturer = boat.BoatImage = boat.BoatDescription = boat.BoatModel = boat.BoatName = string.Empty;
            await App.Current.MainPage.DisplayAlert("form submit invoked", "form submitted", "OK");
        }
        else
        {
            await App.Current.MainPage.DisplayAlert("Boat was not created", "Something went wrong, try again", "OK");
        }
    }
}

I've seen similar questions about System.NullReferenceException: 'Object reference not set to an instance of an object.'

But never about an on submit case, hence why I am asking this question now.

All help is much appreciated!

0 Answers0