空引用未处理 C#
本文关键字:未处理 引用 | 更新日期: 2023-09-27 18:30:42
我的源代码有问题。我有一个文件,用于将我的设置保存到 ini 文件。但是当我想从主窗体保存设置时,我得到一个 NullRefrenceExpection 是未处理的异常。问题出在哪里?
编辑我在代码的这一部分得到异常:
private void SaveSettings()
{
//SettingsIniFile iniFile = new SettingsIniFile();
_settingsIniFile.Save(LoadTextBoxes());
}
代码主窗体:
using System.Windows.Forms;
using System.IO.Ports;
namespace Milk_Units
{
public partial class MainForm : Form
{
private Settings _settings;
private SettingsIniFile _settingsIniFile;
public MainForm()
{
InitializeComponent();
LoadSettings(false);
LoadTextBoxes();
LoadComportName();
}
private void LoadSettings(bool defaults)
{
SettingsIniFile iniFile = new SettingsIniFile();
_settings = iniFile.LoadSettings(defaults);
}
private void SaveSettings()
{
//SettingsIniFile iniFile = new SettingsIniFile();
_settingsIniFile.Save(LoadTextBoxes());
}
private Settings LoadTextBoxes()
{
//ACR
_startRemovalTextBox.Text = _settings.AcrStartRemoval;
_removalTimeTextBox.Text = _settings.AcrRemovalTime;
_removalDelayTextBox.Text = _settings.AcrRemovalDelay;
//CLEANING
_durCleaningTextbox.Text = _settings.CleanDurCleaning;
_timeValveOnTextbox.Text = _settings.CleanTimeValveOn;
_timeValveOffTextBox.Text = _settings.CleanTimeValveOff;
//CALIBRATE
_contentLeftTextBox.Text = _settings.CalibrateContentLeft;
_calibrateLeftTextBox.Text = _settings.CalibrateCalibrateLeft;
_contentRightTextBox.Text = _settings.CalibrateContentRight;
_calibrateRightTextBox.Text = _settings.CalibrateCalibrateRight;
//CONDUCTIVITY
_factorLeftTextBox.Text = _settings.ConductFactorLeft;
_offsetLeftTextBox.Text = _settings.ConductOffsetleft;
_factorRightTextBox.Text = _settings.ConductFactorRight;
_offsetRightTextBox.Text = _settings.ConductOffsetRight;
_levelLeftTextBox.Text = _settings.ConductLevelLeft;
_levelRightTextBox.Text = _settings.ConductLevelRight;
//GENERAL
_typeOfValveTextBox.Text = _settings.GeneralTypeOfValve;
_indicatorTextBox.Text = _settings.GeneralIndicator;
_inverseOutputTextBox.Text = _settings.GeneralInverseOutput;
_restartTimeTextBox.Text = _settings.GeneralRestartTime;
_waterTimeTextBox.Text = _settings.GeneralWaterTime;
_gateDelayTextbox.Text = _settings.GeneralGateDelay;
//PULSATION
_pulsationTextBox.Text = _settings.PulsationPulsationPm;
_ratioFrontTextBox.Text = _settings.PulsationSrRatioFront;
_ratioBackTextBox.Text = _settings.PulsationSrRatioBack;
_stimulationTextBox.Text = _settings.PulsationStimulationPm;
_stimFrontTextBox.Text = _settings.PulsationSrStimFront;
_stimBackTextBox.Text = _settings.PulsationSrStimBack;
_stimulationDurTextBox.Text = _settings.PulsationStimulationDur;
return _settings;
}
private void DefaultSettingButtonClick(object sender, System.EventArgs e)
{
LoadSettings(true);
LoadTextBoxes();
SaveSettings();
LoadComportName();
}
private void SendSettingsButtonClick(object sender, System.EventArgs e)
{
SaveSettings();
}
private void LoadComportName()
{
_comPortComboBox.DataSource = SerialPort.GetPortNames();
}
}
代码设置初始化文件
using System;
using System.Windows.Forms;
using Idento.Common.Utilities;
using Milk_Units;
namespace Milk_Units
{
public class SettingsIniFile
{
private const String FileNameCustom = "Data''Custom.ini";//Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), );
private const String FileNameDefault = "Data''Default.ini";//Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), );
public Settings LoadSettings(bool defaults = false)
{
String fileName = defaults ? FileNameDefault : FileNameCustom;
StringList input = new StringList().FromFile(fileName);
//Settings settings = null;
Settings settings = new Settings();
foreach (var item in input)
{
String line = item.Trim();
if (line.StartsWith("[") && line.EndsWith("]"))
continue;
int index = line.IndexOf('=');
if (index < 0)
continue;
String key = line.Substring(0, index).Trim();
String value = line.Substring(index + 1).Trim();
String comment = "";
index = value.IndexOf("//");
if (index > -1)
{
comment = value.Substring(index).Trim();
value = value.Substring(0, index).Trim();
}
// ACR
if (Utils.EqualsIgnoreCase(key, "10: Start Removal"))
settings.AcrStartRemoval = value;
else if (Utils.EqualsIgnoreCase(key, "11: Removal Time"))
settings.AcrRemovalTime = value;
else if (Utils.EqualsIgnoreCase(key, "12: Removal Delay"))
settings.AcrRemovalDelay = value;
// CLEANING
else if (Utils.EqualsIgnoreCase(key, "21: Dur. Cleaning"))
settings.CleanDurCleaning = value;
else if (Utils.EqualsIgnoreCase(key, "22: Time valve on"))
settings.CleanTimeValveOn = value;
else if (Utils.EqualsIgnoreCase(key, "23: Time valve off"))
settings.CleanTimeValveOff = value;
//CALIBRATE
else if (Utils.EqualsIgnoreCase(key, "31: Content left"))
settings.CalibrateContentLeft = value;
else if (Utils.EqualsIgnoreCase(key, "32: Calibrate left"))
settings.CalibrateCalibrateLeft = value;
else if (Utils.EqualsIgnoreCase(key, "33: Content right"))
settings.CalibrateContentRight = value;
else if (Utils.EqualsIgnoreCase(key, "34: Calibrate right"))
settings.CalibrateCalibrateRight = value;
//CONDUCTIVITY
else if (Utils.EqualsIgnoreCase(key, "41: Factor left"))
settings.ConductFactorLeft = value;
else if (Utils.EqualsIgnoreCase(key, "42: Offset left"))
settings.ConductOffsetleft = value;
else if (Utils.EqualsIgnoreCase(key, "43: Factor right"))
settings.ConductFactorRight = value;
else if (Utils.EqualsIgnoreCase(key, "44: Offset right"))
settings.ConductOffsetRight = value;
else if (Utils.EqualsIgnoreCase(key, "45: Level left"))
settings.ConductLevelLeft = value;
else if (Utils.EqualsIgnoreCase(key, "46: Level right"))
settings.ConductLevelRight = value;
//GENERAL
else if (Utils.EqualsIgnoreCase(key, "51: Type of valve"))
settings.GeneralTypeOfValve = value;
else if (Utils.EqualsIgnoreCase(key, "52: Indicator"))
settings.GeneralIndicator = value;
else if (Utils.EqualsIgnoreCase(key, "53: Inverse output"))
settings.GeneralInverseOutput = value;
else if (Utils.EqualsIgnoreCase(key, "54: Restart time"))
settings.GeneralRestartTime = value;
else if (Utils.EqualsIgnoreCase(key, "55: Water time"))
settings.GeneralWaterTime = value;
else if (Utils.EqualsIgnoreCase(key, "56: Gate delay"))
settings.GeneralGateDelay = value;
//PULSATION
else if (Utils.EqualsIgnoreCase(key, "61: Pulsation p/m"))
settings.PulsationPulsationPm = value;
else if (Utils.EqualsIgnoreCase(key, "62: S/r ratio front"))
settings.PulsationSrRatioFront = value;
else if (Utils.EqualsIgnoreCase(key, "63: S/r ratio back"))
settings.PulsationSrRatioBack = value;
else if (Utils.EqualsIgnoreCase(key, "64: Stimulation p/m"))
settings.PulsationStimulationPm = value;
else if (Utils.EqualsIgnoreCase(key, "65: S/r stim front"))
settings.PulsationSrStimFront = value;
else if (Utils.EqualsIgnoreCase(key, "66: S/r stim back"))
settings.PulsationSrStimBack = value;
else if (Utils.EqualsIgnoreCase(key, "67: Stimulation dur"))
settings.PulsationStimulationDur = value;
}
return settings;
}
public void Save(Settings settings)
{
StringList list = new StringList();
list.Add("[Settings]");
list.Add("");
list.Add("[ACR]");
list.Add("10: Start Removal =" + settings.AcrStartRemoval + " // ms");
list.Add("11: Removal Time =" + settings.AcrRemovalTime + " // commentary");
list.Add("12: Removal Delay =" + settings.AcrRemovalDelay + " // commentary");
list.Add("");
list.Add("[Cleaning]");
list.Add("21: Dur. Cleaning =" + settings.CleanDurCleaning + " //commentary");
list.Add("22: Time valve on =" + settings.CleanTimeValveOn + " //commentary");
list.Add("23: Time valve off =" + settings.CleanTimeValveOff + " //commentary");
list.Add("");
list.Add("[Calibrate]");
list.Add("31: Content left =" + settings.CalibrateContentLeft + "//commentary");
list.Add("32: Calibrate left =" + settings.CalibrateCalibrateLeft + "//commentary");
list.Add("33: Content right =" + settings.CalibrateContentRight + "//commentary");
list.Add("34: Calibrate right =" + settings.CalibrateCalibrateRight + "//commentary");
list.Add("");
list.Add("[Conductivity]");
list.Add("41: Factor left =" + settings.ConductFactorLeft + "//commentary");
list.Add("42: Offset left =" + settings.ConductOffsetleft + "//commentary");
list.Add("43: Factor right =" + settings.ConductFactorRight + "//commentary");
list.Add("44: Offset right =" + settings.ConductOffsetRight + "//commentary");
list.Add("45: Level left =" + settings.ConductLevelLeft + "//commentary");
list.Add("46: Level right =" + settings.ConductLevelRight + "//commentary");
list.Add("");
list.Add("[General]");
list.Add("51: Type of valve =" + settings.GeneralTypeOfValve + "//commentary");
list.Add("52: Indicator =" + settings.GeneralIndicator + "//commentary");
list.Add("53: Inverse output =" + settings.GeneralInverseOutput + "//commentary");
list.Add("54: Restart time =" + settings.GeneralRestartTime + "//commentary");
list.Add("55: Water time =" + settings.GeneralWaterTime + "//commentary");
list.Add("56: Gate delay =" + settings.GeneralGateDelay + "//commentary");
list.Add("");
list.Add("[Pulsation]");
list.Add("61: Pulsation p/m =" + settings.PulsationPulsationPm + "//commentary");
list.Add("62: S/r ratio front =" + settings.PulsationSrRatioFront + "//commentary");
list.Add("63: S/r ratio back =" + settings.PulsationSrRatioBack + "//commentary");
list.Add("64: Stimulation p/m =" + settings.PulsationStimulationPm + "//commentary");
list.Add("65: S/r stim front =" + settings.PulsationSrStimFront + "//commentary");
list.Add("66: S/r stim back =" + settings.PulsationSrStimBack + "//commentary");
list.Add("67: Stimulation dur =" + settings.PulsationStimulationDur + "//commentary");
try
{
list.ToFile(FileNameCustom);
}
catch (Exception exc)
{
MessageBox.Show("Exception saving settings: " + exc.Message);
}
}
}
}
_settingsIniFile
成员变量未初始化。
修改了您的代码,以便它初始化该成员,并且在您的LoadSettings
和SaveSettings
中它也引用该成员变量。
试试这个:
using System.Windows.Forms;
using System.IO.Ports;
namespace Milk_Units
{
public partial class MainForm : Form
{
private Settings _settings;
private SettingsIniFile _settingsIniFile = new SettingsIniFile();
public MainForm()
{
InitializeComponent();
LoadSettings(false);
LoadComportName();
}
private void LoadSettings(bool defaults)
{
_settings = _settingsIniFile.LoadSettings(defaults);
LoadTextBoxes();
}
private void SaveSettings()
{
SaveTextBoxes();
_settingsIniFile.Save(_settings);
}
private void LoadTextBoxes()
{
//ACR
_startRemovalTextBox.Text = _settings.AcrStartRemoval;
_removalTimeTextBox.Text = _settings.AcrRemovalTime;
_removalDelayTextBox.Text = _settings.AcrRemovalDelay;
//CLEANING
_durCleaningTextbox.Text = _settings.CleanDurCleaning;
_timeValveOnTextbox.Text = _settings.CleanTimeValveOn;
_timeValveOffTextBox.Text = _settings.CleanTimeValveOff;
//CALIBRATE
_contentLeftTextBox.Text = _settings.CalibrateContentLeft;
_calibrateLeftTextBox.Text = _settings.CalibrateCalibrateLeft;
_contentRightTextBox.Text = _settings.CalibrateContentRight;
_calibrateRightTextBox.Text = _settings.CalibrateCalibrateRight;
//CONDUCTIVITY
_factorLeftTextBox.Text = _settings.ConductFactorLeft;
_offsetLeftTextBox.Text = _settings.ConductOffsetleft;
_factorRightTextBox.Text = _settings.ConductFactorRight;
_offsetRightTextBox.Text = _settings.ConductOffsetRight;
_levelLeftTextBox.Text = _settings.ConductLevelLeft;
_levelRightTextBox.Text = _settings.ConductLevelRight;
//GENERAL
_typeOfValveTextBox.Text = _settings.GeneralTypeOfValve;
_indicatorTextBox.Text = _settings.GeneralIndicator;
_inverseOutputTextBox.Text = _settings.GeneralInverseOutput;
_restartTimeTextBox.Text = _settings.GeneralRestartTime;
_waterTimeTextBox.Text = _settings.GeneralWaterTime;
_gateDelayTextbox.Text = _settings.GeneralGateDelay;
//PULSATION
_pulsationTextBox.Text = _settings.PulsationPulsationPm;
_ratioFrontTextBox.Text = _settings.PulsationSrRatioFront;
_ratioBackTextBox.Text = _settings.PulsationSrRatioBack;
_stimulationTextBox.Text = _settings.PulsationStimulationPm;
_stimFrontTextBox.Text = _settings.PulsationSrStimFront;
_stimBackTextBox.Text = _settings.PulsationSrStimBack;
_stimulationDurTextBox.Text = _settings.PulsationStimulationDur;
return _settings;
}
private void SaveTextBoxes()
{
//ACR
_settings.AcrStartRemoval = _startRemovalTextBox.Text;
_settings.AcrRemovalTime = removalTimeTextBox.Text;
_settings.AcrRemovalDelay = _removalDelayTextBox.Text;
//CLEANING
_settings.CleanDurCleaning = _durCleaningTextbox.Text;
_settings.CleanTimeValveOn = _timeValveOnTextbox.Text;
_settings.CleanTimeValveOff = _timeValveOffTextBox.Text;
//CALIBRATE
_settings.CalibrateContentLeft = _contentLeftTextBox.Text;
_settings.CalibrateCalibrateLeft = _calibrateLeftTextBox.Text;
_settings.CalibrateContentRight = _contentRightTextBox.Text;
_settings.CalibrateCalibrateRight = _calibrateRightTextBox.Text;
//CONDUCTIVITY
_settings.ConductFactorLeft = _factorLeftTextBox.Text;
_settings.ConductOffsetleft = _offsetLeftTextBox.Text;
_settings.ConductFactorRight = _factorRightTextBox.Text;
_settings.ConductOffsetRight = _offsetRightTextBox.Text;
_settings.ConductLevelLeft = _levelLeftTextBox.Text;
_settings.ConductLevelRight = _levelRightTextBox.Text;
//GENERAL
_settings.GeneralTypeOfValve = _typeOfValveTextBox.Text;
_settings.GeneralIndicator = _indicatorTextBox.Text;
_settings.GeneralInverseOutput = _inverseOutputTextBox.Text;
_settings.GeneralRestartTime = _restartTimeTextBox.Text;
_settings.GeneralWaterTime = _waterTimeTextBox.Text;
_settings.GeneralGateDelay = _gateDelayTextbox.Text;
//PULSATION
_settings.PulsationPulsationPm = _pulsationTextBox.Text;
_settings.PulsationSrRatioFront = _ratioFrontTextBox.Text;
_settings.PulsationSrRatioBack = _ratioBackTextBox.Text;
_settings.PulsationStimulationPm = _stimulationTextBox.Text;
_settings.PulsationSrStimFront = _stimFrontTextBox.Text;
_settings.PulsationSrStimBack = _stimBackTextBox.Text;
_settings.PulsationStimulationDur = _stimulationDurTextBox.Text;
}
private void DefaultSettingButtonClick(object sender, System.EventArgs e)
{
LoadSettings(true);
SaveSettings();
LoadComportName();
}
private void SendSettingsButtonClick(object sender, System.EventArgs e)
{
SaveSettings();
}
private void LoadComportName()
{
_comPortComboBox.DataSource = SerialPort.GetPortNames();
}
}
我看不到你在哪里初始化_settingsIniFile;
尝试将声明更改为:
private SettingsIniFile _settingsIniFile = new SettingsIniFile();