标签: nullreferenceexception

是否有可以检查空引用异常的工具?

是否有可用的工具可以扫描代码并检查可能的空引用异常,即在调用方法/访问属性之前没有代码检查null?

c# nullreferenceexception

3
推荐指数
2
解决办法
1068
查看次数

SelectedIndex是一个空引用异常?

我一直收到这个错误:

System.NullReferenceException was unhandled by user code
  Message=[Arg_NullReferenceException]
Arguments: 
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=5.0.60401.00&File=mscorlib.dll&Key=Arg_NullReferenceException
  StackTrace:
       at Jantire.DoHomeworkView.TextAlignment_combobox_SelectionChanged(Object sender, SelectionChangedEventArgs e)
       at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
       at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)
       at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
       at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.ItemCollection.NotifyCollectionReady()
       at System.Windows.Controls.ItemsControl.NotifyAllItemsAdded(IntPtr nativeItemsControl)
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

在代码:

private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //This next line is where error …
Run Code Online (Sandbox Code Playgroud)

c# visual-studio-2010 nullreferenceexception silverlight-4.0

3
推荐指数
1
解决办法
2770
查看次数

Nullable属性在.HasValue上抛出NullReferenceException

这行(C#)代码

if (!currentLap.S1.HasValue)
Run Code Online (Sandbox Code Playgroud)

给了我

System.NullReferenceException:未将对象引用设置为对象的实例.

如果我确定currentLap变量被实例化(因为它之前使用了几行而且它是一个局部变量)并且它具有以下属性:

private double? _s1;

[DefaultValue(null)]
[JsonConverter(typeof(ShortDoubleConverter))]
public double? S1
{
    get { return _s1; }
    set { _s1 = value; }
}
Run Code Online (Sandbox Code Playgroud)

怎么可能抛出NullReferenceException?它可以与发布模式下的优化有关吗?

谢谢,Stevo

编辑:

这是完整的方法代码.

public void Update(DriverData driverData)
    {
        LapInfo currentLap = this.CurrentLap;

        if (currentLap != null &&
       this.LastDriverData != null &&
       driverData.TotalLaps != this.LastDriverData.TotalLaps &&
       driverData.InPits &&
       driverData.Speed < 10 &&
       !this.LastDriverData.InPits)
        {
            currentLap.Escaped = true;
        }    

        this.LastDriverData = driverData;

        if ((currentLap == null || currentLap.Lap != driverData.LapNumber) &&
            !this.Laps.TryGetValue(driverData.LapNumber, out currentLap)) …
Run Code Online (Sandbox Code Playgroud)

c# nullable release nullreferenceexception

3
推荐指数
1
解决办法
3090
查看次数

为什么我在模拟测试对象上获得空引用异常?

我有以下内容

IdleClass idleSend = MockRepository.GenerateMock<IdleClass >();
SpeedClass speedSend = MockRepository.GenerateMock<SpeeClass >();
idleSend.Expect(x => x.IsUnitInSystem(networkID)).Return(true).Repeat.Any();
speedSend.Expect(x => x.IsUnitInSystem(networkID)).Return(true).Repeat.Any();
Run Code Online (Sandbox Code Playgroud)

和原始的IsUnitInSystem(networkID)函数(在IdleClass和SpeedClass的子类中)如下:

public bool IsUnitInSystem(string networkID)
        {
            DateTime outVal = default(DateTime);
            return m_list1.DoesListContainUnit(networkID) ||
                   m_list2.TryGetValue(networkID, out outVal) ||
                   m_list3.TryGetValue(networkID, out outVal) ||
                   m_list4.TryGetValue(networkID, out outVal) ||
                   m_list5.TryGetValue(networkID, out outVal);
        }
Run Code Online (Sandbox Code Playgroud)

我正在IsUnitInSystem函数的返回行上恢复System.NullReferenceException.

堆栈跟踪:

IdleClass.IsUnitInSystem(String networkID)
PendingTest.<ResendClassTest>b__1b(IdleClass x)
RhinoMocksExtension.Expect[T,R](T mock, Function'2 action)
PendingTest.ResendClassTest()
Run Code Online (Sandbox Code Playgroud)

我不明白的是,当我为函数指定了.Expect功能时,程序为什么以及如何进入函数?

c# unit-testing rhino-mocks mocking nullreferenceexception

3
推荐指数
1
解决办法
1718
查看次数

VB.NET System.NullReferenceException

我有一个VB.NET程序,可以从10个不同的数据库构建多个数据集.

我得到了这个例外:

System.NullReferenceException: Object referenced not set to an 
instance of an object:
Run Code Online (Sandbox Code Playgroud)

此错误发生在以下行:

Me.OverTableAdapter.Adapter.SelectCommand.CommandTimeout = 60000
Me.OverTableAdapter.Fill(Me.Dataset.Over, TodayDt, TodayEnd)
Run Code Online (Sandbox Code Playgroud)

这个例外意味着什么?

vb.net dataset nullreferenceexception

3
推荐指数
1
解决办法
2309
查看次数

SSIS脚本组件作为源抛出System.NullReferenceException

我有一个DataFlow,其中有一个脚本组件作为源。

我已经根据需要定义了Output(OutputRows)和Column(MyOutputValue)。

当我想测试我的脚本时,即使使用硬编码值,也总是会遇到相同的错误:

System.NullReferenceException: Object reference not set to an instance of an object. at ScriptMain.CreateNewOutputRows().

我不知道这里出了什么问题。任何的想法?

这是我的代码:

using System;
using System.Data;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Microsoft.SqlServer.Dts.Runtime;
using Excel = Microsoft.Office.Interop.Excel;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Members
    String MyOutputValue;
#endregion

public override void PreExecute()
{
    base.PreExecute();


    MyOutputValue = "test";

    CreateNewOutputRows();

}

public override void PostExecute()
{

base.PostExecute();
}

public override void CreateNewOutputRows()
{
    OutputRowsBuffer.AddRow();

    OutputRowsBuffer.MyOutputValue = MyOutputValue;
}
}
Run Code Online (Sandbox Code Playgroud)

在我的SSIS程序包中,我开始调试,然后得到以下屏幕(它是德语,因此针对该帖子,我将错误翻译为英语): 在此处输入图片说明

c# ssis nullreferenceexception

3
推荐指数
1
解决办法
6601
查看次数

可选的设计模式,优点

因此,众所周知,臭名昭着NullReferenceException是软件产品中最常见的例外.我一直在阅读一些文章,并发现自己采用了可选方法.

它的目标是围绕可以为空的值创建某种封装

public sealed class Optional<T> where T : class {

    private T value;

    private Optional(T value) {
        this.value = value;
    }

    //Used to create an empty container
    public static Optional<T> Empty() {
        return new Optional(null);
    }

    //Used to create a container with a non-null value
    public static Optional<T> For(T value) {
        return new Optional(value);
    }

    //Used to check if the container holds a non-null value
    public bool IsPresent {
        get { return value != null; …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns nullpointerexception optional nullreferenceexception

3
推荐指数
1
解决办法
2625
查看次数

尝试在空对象引用[Android]上调用虚方法'int java.util.Random.nextInt(int)'

我点击按钮时尝试使用java中的rng,但每次单击它时程序崩溃并给出以下错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference
at me.test.first.MainActivity.onGenPress(MainActivity.java:25)
Run Code Online (Sandbox Code Playgroud)

按钮按键方法

public void onGenPress(View v){
        TextView tv = (TextView) findViewById(R.string.copper);
        int number = 1 + dice.nextInt(rollProgressMA);
        co.copper += number;
        tv.setText("Copper: " + co.copper + " + " + number);
}
Run Code Online (Sandbox Code Playgroud)

课堂上涉及的对象

CoinTracker co = new CoinTracker();
RollProgress rpo = new RollProgress();
private Random dice;
private int rollProgressMA = rpo.rollProgress;
Run Code Online (Sandbox Code Playgroud)

RollProgress类的Int值

public int rollProgress = 1;
Run Code Online (Sandbox Code Playgroud)

提前感谢任何解决/解决方案的尝试.

java random android nullreferenceexception

3
推荐指数
1
解决办法
6202
查看次数

将字符串数组中的值分配给自定义类型数组的每个元素的字符串属性

假设我有一个字符串数组countryNames[]- 包含世界各国的名字:

string[] countryNames = { "Afghanistan" , "Albania" , "Algeria", ... }
Run Code Online (Sandbox Code Playgroud)

我还有一个名为Country包含这些属性的类,其中包括:

public string CountryCode { get; set; }
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)

我的目标是创建一个自定义类型的数组Country,并Country.Name为每个元素的属性赋值Country[]相应索引的字符串值countryNames[].我试着通过以下方式这样做,在我实现字符串数组的方法中:

Country[] countries = new Country[193];
for (int i = 0; i < 193; i++)
{
    countries[i].Name = countryNames[i];
}
return countries;
Run Code Online (Sandbox Code Playgroud)

该countries[i].Name然而,引起NullReferenceException.我不知道问题在哪里,因为属性Country.Name是一个字符串.阵列和属性混合在一起时是否有任何复杂性?

多谢你们!

c# arrays nullreferenceexception

3
推荐指数
1
解决办法
5342
查看次数

有什么方法可以强制Equals返回false而不是Null引用异常?

我在线遇到了一个空引用异常:

dr["IsSandpit"] = p.MineralName.Equals("Sand");
Run Code Online (Sandbox Code Playgroud)

当然修复是:

dr["IsSandpit"] = p.MineralName!=null && p.MineralName.Equals("Sand");
Run Code Online (Sandbox Code Playgroud)

但我想知道是否可以配置一个可空类型,Equals方法而不是抛出空引用异常只会返回false一个强类型变量?还是一些方法NotNullAndEquals?

数据库中的空表示"未填充"或空"空","空"肯定不等于"沙".(也许这是重复但我没有找到正确的问题)

c# nullreferenceexception equals-operator

3
推荐指数
1
解决办法
94
查看次数