简体   繁体   English

如何修复 System.NullReferenceException:Object 引用未设置为 object 的实例

[英]How to fix System.NullReferenceException: Object reference not set to an instance of an object

EDIT: This issue is resolved, good thanks to Reniuz for his 5 hours of work and research of this issue, thank's everyone.编辑:这个问题已经解决,非常感谢 Reniuz 为这个问题付出了 5 个小时的工作和研究,谢谢大家。

NullReferenceException: Object reference not set to an instance of an object. on the following code and I've searched and searched and pulled my hair out for over 7-8 hours now trying to fix it. NullReferenceException: Object reference not set to an instance of an object. 在以下代码中,我已经搜索并搜索了超过 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));
}

Error is on the line: EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);错误在线:EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);

the class: class:

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);
        }

What this is supposed to do is when the effectSelectorForm loads it should make a list of all the voice changer options but it does not do that.... it loads nothing on it, I got this code from another form for changing voice in Skype and re-wrote about 400 lines of code for it to work in my app but now I have this issue and I'm not giving up with all the effort I've put in so far.这应该做的是当 effectSelectorForm 加载时,它应该列出所有语音转换器选项,但它并没有这样做......它没有加载任何内容,我从另一个用于在 Skype 中更改语音的表单获得此代码并重新编写了大约 400 行代码以使其在我的应用程序中运行,但现在我遇到了这个问题,我并没有放弃迄今为止所做的所有努力。 If it loaded on the other project why not in this one?如果它加载到其他项目中,为什么不加载到这个项目中呢? I've went over code for hours over and over thinking im missing something but no.我一遍又一遍地检查代码几个小时,以为我遗漏了一些东西,但没有。

Any help would be fantastic.任何帮助都会很棒。

Stack

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效果.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;
        }
    }
}

If the problem is 100% here如果这里的问题是 100%

EffectSelectorForm effectSelectorForm = new EffectSelectorForm(Effects);

There's only one possible explanation: property/variable "Effects" is not initialized properly... Debug your code to see what you pass to your objects.只有一种可能的解释:属性/变量“Effects”未正确初始化...调试代码以查看传递给对象的内容。

EDIT after several hours几个小时后编辑

There were some problems:有一些问题:

  • MEF attribute [Import] didn't work as expected, so we replaced it for the time being with a manually populated List<>. MEF 属性 [Import] 没有按预期工作,因此我们暂时将其替换为手动填充的 List<>。 While the collection was null, it was causing exceptions later in the code, when the method tried to get the type of the selected item and there was none.虽然该集合是 null,但它在稍后的代码中导致了异常,当时该方法试图获取所选项目的类型但没有。

  • several event handlers weren't wired up to control events几个事件处理程序没有连接到控制事件

Some problems are still present, but I believe OP's original problem has been fixed.一些问题仍然存在,但我相信 OP 的原始问题已得到解决。 Other problems are not related to this one.其他问题与此无关。

During debug, break on all exceptions thrown.在调试期间,中断所有抛出的异常。 Debug->Exceptions调试->异常

Check all 'Thrown' exceptions.检查所有“抛出”异常。 F5, the code will stop on the offending line. F5,代码将停止在有问题的行上。

I had the same problem but it only occurred on the published website on Godaddy.我有同样的问题,但它只发生在 Godaddy 上发布的网站上。 It was no problem in my local host.在我的本地主机上没问题。

The error came from an aspx.cs (code behind file) where I tried to assign a value to a label. It appeared that from within the code behind, that the label Text appears to be null. So all I did with change all my Label Text properties in the ASPX file from Text="" to Text=" ".错误来自 aspx.cs(代码隐藏文件),我试图在其中为 label 赋值。从代码后面看来,label 文本似乎是 null。所以我所做的一切都改变了我的所有Label ASPX 文件中的文本属性从 Text="" 到 Text=" "。

The problem disappeared.问题消失了。 I don't know why the error happens from the hosted version but not on my localhost and don't have time to figure out why.我不知道为什么错误发生在托管版本而不是我的本地主机上,也没有时间弄清楚原因。 But it works fine now.但它现在工作正常。

I was getting this same error, but for me this was due to a method in a base class (in Project A) having the output type changed from a non-void type to void.我遇到了同样的错误,但对我而言,这是由于基类 class(在项目 A 中)中的一种方法将 output 类型从非空类型更改为空类型。 A child class existed in Project B (which I didn't want used and had marked obsolete) that I missed when performing this update and hence started throwing this error.项目 B 中存在一个子项 class(我不想使用它并标记为过时),我在执行此更新时错过了它,因此开始抛出此错误。

1>CSC: error CS8104: An error occurred while writing the output file: System.NullReferenceException: Object reference not set to an instance of an object. 1>CSC:错误 CS8104:写入 output 文件时出错:System.NullReferenceException:Object 引用未设置为 object 的实例。

Original Code:原始代码:

[Obsolete("Calling this method will throw an error")]
public override CompletionStatus Run()
{
    throw new CustomException("Run process not supported.");
}

Revised Code:修改后的代码:

[Obsolete("Calling this method will throw an error")]
public override void Run()
{
    throw new CustomException("Run process not supported.");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在MVC Web服务中修复此“ system.nullreferenceexception对象引用未设置为对象实例” - How to Fix This “system.nullreferenceexception object reference not set to an instance of an object” in mvc web service System.NullReferenceException - 未将对象引用设置为对象的实例 - System.NullReferenceException – Object reference not set to an instance of an object Foreach System.NullReferenceException:未将对象引用设置为对象的实例 - Foreach System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object 类型&#39;System.NullReferenceException&#39;的异常:对象引用未设置为对象的实例 - An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object 错误:System.NullReferenceException:对象引用未设置为对象的实例 - Error :System.NullReferenceException: Object reference not set to an instance of an object Xamarin android System.NullReferenceException:未将对象引用设置为对象的实例 - Xamarin android System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:未将对象引用设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题 - System.NullReferenceException: „Object reference not set to an instance of an object.” problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM