C
C#10mo ago
Mekasu0124

✅ Not Quite Understanding Model Usage

public class UserModel
{
public int Id {get; set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Email {get;set;}
public string Username {get;set;}
public string Password {get;set;}
public int TosAgree {get;set;}
public UserCustomSettingsModel CustomSettings {get;set;}
}

public UserCustomSettingsModel
{
public UserStylesModel UserStyles {get;set;}
public UserSettingsModel UserSettings {get;set;}
}

public UserSettingsModel { public int UseOnlineStorage {get;set;} }

public UserStylesModel
{
public string FontSize {get;set;}
public string FontStyle {get;set;}
public string FontColor {get;set;}
public string BackgroundColor {get;set;}
public string BorderColor {get;set;}
}
public class UserModel
{
public int Id {get; set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Email {get;set;}
public string Username {get;set;}
public string Password {get;set;}
public int TosAgree {get;set;}
public UserCustomSettingsModel CustomSettings {get;set;}
}

public UserCustomSettingsModel
{
public UserStylesModel UserStyles {get;set;}
public UserSettingsModel UserSettings {get;set;}
}

public UserSettingsModel { public int UseOnlineStorage {get;set;} }

public UserStylesModel
{
public string FontSize {get;set;}
public string FontStyle {get;set;}
public string FontColor {get;set;}
public string BackgroundColor {get;set;}
public string BorderColor {get;set;}
}
public partial class TosViewModel : ViewModelBase
{
public void SetupUI()
{
FontSize = User.CustomSettings.UserStyles.FontSize;
FontStyle = User.CustomSettings.UserStyles.FontStyle;
Foreground = User.CustomSettings.UserStyles.FontColor;
Background = User.CustomSettings.UserStyles.BackgroundColor;
Border = User.CustomSettings.UserStyles.BorderColor;
Border2 = "#CDCDCD";
}
}
public partial class TosViewModel : ViewModelBase
{
public void SetupUI()
{
FontSize = User.CustomSettings.UserStyles.FontSize;
FontStyle = User.CustomSettings.UserStyles.FontStyle;
Foreground = User.CustomSettings.UserStyles.FontColor;
Background = User.CustomSettings.UserStyles.BackgroundColor;
Border = User.CustomSettings.UserStyles.BorderColor;
Border2 = "#CDCDCD";
}
}
In my view model, I am trying to use the styles that are attached to the user model, however, I keep getting the error on UserCustomSettings.UserStyles.FontSize saying 'Object reference not set to an instance of an object'. Like I understand that means I have to do UserStylesModel userStyles = new() but I don't want to overwrite what's pulled from the database so how do I fix this? Thanks.
4 Replies
bighugemassive3
bighugemassive310mo ago
Pass the value of User.CustomSettings.UserStyles, when it's ready, to the SetupUI method Or call SetupUI when UserStyles is ready or when their values change
Mekasu0124
Mekasu0124OP10mo ago
public partial class TosViewModel : ViewModelBase
{
private UserModel _user = new();
private UserStylesModel _userStyles = new();

public TosViewModel(UserModel user)
{
_user = user;
_userStyles = user.CustomSettings.UserStyles;
SetupUI();
}
// SetupUI() code here

public UserModel User
{
get => _user;
set => this.RaiseAndSetIfChanged(ref _user, value);
}
public UserStylesModel UserStyles
{
get => _userStyles;
set => this.RaiseAndSetIfChanged(ref _userStyles, value);
}
}
public partial class TosViewModel : ViewModelBase
{
private UserModel _user = new();
private UserStylesModel _userStyles = new();

public TosViewModel(UserModel user)
{
_user = user;
_userStyles = user.CustomSettings.UserStyles;
SetupUI();
}
// SetupUI() code here

public UserModel User
{
get => _user;
set => this.RaiseAndSetIfChanged(ref _user, value);
}
public UserStylesModel UserStyles
{
get => _userStyles;
set => this.RaiseAndSetIfChanged(ref _userStyles, value);
}
}
well see I use this method of updating the changes. Before this view model shows, the MainWindowViewModel calls the default user's information from the database
public partial class MainWindowViewModel : ViewModelBase
{
private UserModel _user = new();

public MainWindowViewModel(...)
{
User = Db.GetDefaultUser();
}

public UserModel User
{
get => _user;
set => this.RaiseAndSetIfChanged(ref _user, value);
}
}
public partial class MainWindowViewModel : ViewModelBase
{
private UserModel _user = new();

public MainWindowViewModel(...)
{
User = Db.GetDefaultUser();
}

public UserModel User
{
get => _user;
set => this.RaiseAndSetIfChanged(ref _user, value);
}
}
public TosViewModel(UserModel user)
{
- _user = user;
- _userStyles = user.CustomSettings.UserStyles;

+ User = user;
+ UserStyles = User.CustomSettings.UserStyles;
}
public TosViewModel(UserModel user)
{
- _user = user;
- _userStyles = user.CustomSettings.UserStyles;

+ User = user;
+ UserStyles = User.CustomSettings.UserStyles;
}
even tried this and still got the same error
User = user;
SetupUI(User.CustomSettings.UserStyles);
User = user;
SetupUI(User.CustomSettings.UserStyles);
seems to have fixed it. tyvm https://pastebin.com/mm09JnEQ I'm getting the same error in this function as well, but it's a different use case. I don't have a function to pass it off to like I did in the view model so how do I fix the 'Object reference not set to an instance of an object' error here on line UserSettingsModel defaultUserSettings = defaultUser.CustomSettings.UserSettings;?
bighugemassive3
bighugemassive310mo ago
Either defaultUser, CustomSettings or UserSettings is null
Mekasu0124
Mekasu0124OP10mo ago
I'm using breakpoints, and everything that is supposed to have a value has it's correct value. It's just saying that UserSettingsModel defaultUserSettings = defaultUser.CustomSettings.UserSettings; is not set to an instance of an object. This was the problem in the view model and when I passed the defaultUser.CustomSettings.UserSettings through SetupUI()'s parameters, it solved that issue, however, now I'm in my Database file and I don't have any functions to pass this off to and I don't know how to fix it unfortunately unless I need to create a get/set for the User again. let me try that cant' do that since the class isn't a view model base, reactive ui is useless actually it doesn't matter. I just realized my Helpers class has a GetDefaultUser() function in it that returns the default user profile in a UserModel already. Thanks though!
Want results from more Discord servers?
Add your server