c# – 如何修复System.NullReferenceException:对象引用未设置为对象的实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何修复System.NullReferenceException:对象引用未设置为对象的实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5862字,纯文字阅读大概需要9分钟。
内容图文
![c# – 如何修复System.NullReferenceException:对象引用未设置为对象的实例](/upload/InfoBanner/zyjiaocheng/784/33bba4cf3bea485ca605e7e5a60bae22.jpg)
编辑:这个问题已经解决了,非常感谢Reniuz 5个小时的工作和对这个问题的研究,谢谢大家.
NullReferenceException:未将对象引用设置为对象的实例.在下面的代码上,我搜索并搜索了我的头发超过7-8小时,现在试图解决它.
private void buttonAddEffect_Click_1(object sender, EventArgs e)
{
EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);
if (effectSelectorForm.ShowDialog(this) == DialogResult.OK)
{
// create a new instance of the selected effect as we may want multiple copies of one effect
Effect effect = (Effect)Activator.CreateInstance(effectSelectorForm.SelectedEffect.GetType());
audioGraph.AddEffect(effect);
int index = checkedListBox1.Items.Add(effect, true);
checkedListBox1.SelectedIndex = index;
}
//MessageBox.Show(String.Format("I have {0} effects", Effects.Count));
}
错误就行了:
EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);
班级:
namespace WindowsFormsApplication13
{
public partial class EffectSelectorForm : Form
{
public EffectSelectorForm(ICollection<Effect> effects)
{
InitializeComponent();
listBoxEffects.DisplayMember = "Name";
listBoxEffects.DataSource = effects;
}
private void buttonOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
public Effect SelectedEffect
{
get
{
return (Effect)listBoxEffects.SelectedItem;
}
}
private void listBoxEffects_DoubleClick(object sender, EventArgs e)
{
buttonOK_Click(sender, e);
}
这应该做的是当effectSelectorForm加载它时应该列出所有语音转换器选项,但它没有这样做….它没有加载任何内容,我从另一种形式获取此代码用于更改Skype中的语音并重新编写了大约400行代码,以便在我的应用程序中运行,但现在我遇到了这个问题,而且我并没有放弃到目前为止所付出的所有努力.如果它加载到另一个项目,为什么不在这个?我已经过了几个小时的代码,一遍又一遍地思考我错过了什么,但没有.
任何帮助都会很棒.
堆
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsFormsApplication13.pwn4g3.buttonAddEffect_Click_1(Object sender, EventArgs e) in F:\Users\Tom\Desktop\New folder\New folder (2)\TestApp\pwn4g3\PWN4G3\MainForm2.cs:line 1758
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Effect.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JSNet
{
public abstract class Effect
{
private List<Slider> sliders;
public float SampleRate { get; set; }
public float Tempo { get; set; }
public bool Enabled { get; set; }
public Effect()
{
sliders = new List<Slider>();
Enabled = true;
Tempo = 120;
SampleRate = 44100;
}
public IList<Slider> Sliders { get { return sliders; } }
public Slider AddSlider(float defaultValue, float minimum, float maximum, float increment, string description)
{
Slider slider = new Slider(defaultValue, minimum, maximum, increment, description);
sliders.Add(slider);
return slider;
}
public abstract string Name { get; }
// helper base methods
// these are primarily to enable derived classes to use a similar
// syntax to JS effects
protected float slider1 { get { return sliders[0].Value; } }
protected float slider2 { get { return sliders[1].Value; } }
protected float slider3 { get { return sliders[2].Value; } }
protected float slider4 { get { return sliders[3].Value; } }
protected float slider5 { get { return sliders[4].Value; } }
protected float slider6 { get { return sliders[5].Value; } }
protected float slider7 { get { return sliders[6].Value; } }
protected float slider8 { get { return sliders[7].Value; } }
protected float min(float a, float b) { return Math.Min(a, b); }
protected float max(float a, float b) { return Math.Max(a, b); }
protected float abs(float a) { return Math.Abs(a); }
protected float exp(float a) { return (float)Math.Exp(a); }
protected float sqrt(float a) { return (float)Math.Sqrt(a); }
protected float sin(float a) { return (float)Math.Sin(a); }
protected float tan(float a) { return (float)Math.Tan(a); }
protected float cos(float a) { return (float)Math.Cos(a); }
protected float pow(float a, float b) { return (float)Math.Pow(a, b); }
protected float sign(float a) { return Math.Sign(a); }
protected float log(float a) { return (float)Math.Log(a); }
protected float PI { get { return (float)Math.PI; } }
protected void convolve_c(float[] buffer1, int offset1, float[] buffer2, int offset2, int count)
{
for (int i = 0; i < count * 2; i += 2)
{
float r = buffer1[offset1 + i];
float im = buffer1[offset1 + i + 1];
float cr = buffer2[offset2 + i];
float ci = buffer2[offset2 + i + 1];
buffer1[offset1 + i] = r * cr - im * ci;
buffer1[offset1 + i + 1] = r * ci + im * cr;
}
}
public virtual void Init()
{
}
public abstract void Slider();
public virtual void Block(int samplesblock)
{
}
public abstract void Sample(ref float spl0, ref float spl1);
public override string ToString()
{
return Name;
}
}
}
解决方法:
如果问题是100%在这里
EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);
只有一种可能的解释:属性/变量“效果”未正确初始化…调试代码以查看传递给对象的内容.
几个小时后编辑
有一些问题:
> MEF属性[导入]未按预期工作,因此我们暂时使用手动填充的List<>替换它.虽然该集合为null,但是当该方法试图获取所选项的类型且没有时,它会在代码中导致异常.
>几个事件处理程序没有连接到控制事件
一些问题仍然存在,但我认为OP的原始问题已得到解决.其他问题与此无关.
内容总结
以上是互联网集市为您收集整理的c# – 如何修复System.NullReferenceException:对象引用未设置为对象的实例全部内容,希望文章能够帮你解决c# – 如何修复System.NullReferenceException:对象引用未设置为对象的实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。