标签: nullreferenceexception

如何使用流体断言.ShouldBeNull()

我正在尝试使用 Fluent Assertions ShouldBeNull() 扩展方法来断言我的实例确实为 null。

Result.ShouldBeNull();
Run Code Online (Sandbox Code Playgroud)

显然,这会引发异常,因为我无法在空实例上调用扩展。

System.NullReferenceException
Run Code Online (Sandbox Code Playgroud)

如何正确使用 Fluent Assertions 来做出此断言?

c# tdd nullreferenceexception fluent-assertions

2
推荐指数
1
解决办法
1333
查看次数

R 中的错​​误消息“尝试在 NULL 上设置属性”

我正在自己阅读统计教科书。我正在使用半官方代码(来自其他人编写的手册,但在教科书网站上有链接)来计算示例和练习。

该代码包括一个用于拟合广义线性模型的函​​数。我从手册中输入它并按照建议运行它。尽管实际拟合有效,但在为结果分配标签时出现上述错误。

抛出错误的行是

names(fit$part) <- dn 
Run Code Online (Sandbox Code Playgroud)

在这一点上,价值dnIS[1] "(Intercept)" "Var1"和的值fit$par就是[1] -5.9604611 0.3165604

我在那里没有看到任何 NULL,想知道是什么导致了错误。

完整的代码是:

ct <- data.frame(alcohol.comsumption = c("0", "<1", "1-2", "3-5", ">= 6"), scores=c(0, 0.5, 1.5, 4, 7), absent = c(17066, 14464, 788, 126, 37), present = c(48, 38, 5, 1, 1))

n <- ct$absent + ct$present
alc.consumption <- rep(ct$scores, n)
y <- rep(rep(c(1,0), nrow(ct)), c(rbind(ct$present, ct$absent)))

logitreg <- function(x, y, wt = rep(1, length(y)), intercept = T, start = rep(0, p), …
Run Code Online (Sandbox Code Playgroud)

r nullreferenceexception

2
推荐指数
1
解决办法
2万
查看次数

NullReferenceException 当什么都不是 null 时?

我正在测试(集成)我的存储库层,并且我不断收到来自 EF 的空引用错误。我担心的是,当我运行调试器时,对象 null 中没有任何内容。事实上,整个方法中没有任何东西是空的。所有属性都在 Automapper 中正确设置,我对所有内容都使用真实(非模拟)值。他们都在那里并使用代码的真实版本。

测试:

[Test]
public void Clone_ShouldCloneUser()
{
    using (var scope = new TransactionScope())
    {
        //arrange
        var request = Builder<CloneUserRequest>.CreateNew()
            .With(x => x.KeyToClone = 29)
            .With(x => x.User = Builder<User>.CreateNew().With(y => y.Key = 0)
                .Build())
            .Build();

        //act
        _sut.Clone(request);

        //assert
        Assert.DoesNotThrow(() => _sut.Clone(request));
    }
}
Run Code Online (Sandbox Code Playgroud)

方法:

public void Clone(CloneUserRequest request)
{
    var usersGroupsToBeCloned = _context.WebUserGroups.Where(x => x.UserKey == request.KeyToClone).ToList();
    var webUser = _mappingService.Map(request.User, new WebUser());
    webUser.WebUserGroups = usersGroupsToBeCloned;
    //On this line I receive a NullReferenceException, but …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework nullreferenceexception

2
推荐指数
1
解决办法
1442
查看次数

对象初始化期间的 NullReferenceException

为什么在下面的代码中尝试设置 X 的值时会出现 NullReferenceException?当我new在初始化时使用关键字时它工作正常B,但是为什么它可以在没有的情况下正常编译new然后在运行时失败?

https://dotnetfiddle.net/YNvPog

public class A
{
    public _B B;
    public class _B
    {
        public int X;
    }
}

public class Program
{
    public static void Main()
    {
        var a=new A{
                B={
                    X=1
                }
            };
    }
}
Run Code Online (Sandbox Code Playgroud)

c# nullreferenceexception

2
推荐指数
1
解决办法
2190
查看次数

检查 null 后出现空引用异常(检查 null 不起作用)

看一下这段代码:

var categories = tokens.SelectMany(x => x.Categories);

if (categories != null)
{
    if (categories.Contains("interp")) //null ref exception
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在类别中查找“interp”字符串时,出现空引用异常。所以看来“类别!= null”不起作用。

我找到了一些建议(这里How to check if IEnumerable is null orempty?),但它们涉及使用.Any()。但它只会使异常更早地准确(使用 .Any() 时)。甚至 ?.Any() 也会引发异常。

有任何想法吗?

.net c# ienumerable nullreferenceexception

2
推荐指数
1
解决办法
986
查看次数

为什么会在我的try-catch中抛出错误?

为什么我的try-catch块在处理时仍会抛出错误?

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例.

              Try
    Here >> :   _MemoryStream.Seek(6 * StartOffset, 0)
                _MemoryStream.Read(_Buffer, 0, 6)
              Catch ex As IOException
                // Handle Error
              End Try
Run Code Online (Sandbox Code Playgroud)

编辑:清除问题以删除无关的信息.

vb.net asp.net exception nullreferenceexception

1
推荐指数
2
解决办法
301
查看次数

为什么会发出NullReferenceException?

我有一个方法调用者addColisionBox,当我调用它并设置值时,我在调用它的地方得到nullpointer ..我将显示一些代码:

    public void addCollisionBox(int x, int y, int arrayNum)
    {
        //Creating a new rectangle at the x & y cord passed in
        rectangle[arrayNum] = new Rectangle(x, y, R_Width, R_Height);
    }
Run Code Online (Sandbox Code Playgroud)

我在另一个类里面创建了这样的:

CollisionHandler collision;
....
//CurrentX and CurrentY position to pass into addCollisionBox method and at the array number i
collision.addCollisionBox(currentX, currentY, i);
Run Code Online (Sandbox Code Playgroud)

它在消息框中说Visual C#express表示:"对象引用未设置为对象的实例."

.net c# nullreferenceexception

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

C#NullReferenceException未被用户代码处理

任何有关这方面的帮助将不胜感激.每当我运行我的程序时,我得到一个NullReferenceException未被用户代码处理.

因此,当我启动程序时,我会看到一个表单,我应该从名单列表中进行选择,并能够更新详细信息或删除它们,但下拉列表中没有任何内容.

我得到空引用:

comm.Parameters["@EmployeeID"].Value =
employeesList.SelectedItem.Value;
Run Code Online (Sandbox Code Playgroud)

这是CS文件中的选择按钮:

protected void selectButton_Click(object sender, EventArgs e)
{
    // Declare objects
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    // Read the connection string from Web.config
    string connectionString =
        ConfigurationManager.ConnectionStrings[
        "HelpDesk"].ConnectionString;
    // Initialize connection
    conn = new SqlConnection(connectionString);
    // Create command
    comm = new SqlCommand(
        "SELECT FirstName, LastName, Username, Address, City, County, Post Code, " +
        "HomePhone, Extension, MobilePhone FROM Employees " +
        "WHERE EmployeeID = @EmployeeID", conn);
    // Add command parameters
    comm.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int);
    comm.Parameters["@EmployeeID"].Value = …
Run Code Online (Sandbox Code Playgroud)

c# sql asp.net nullreferenceexception visual-web-developer-2010

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

在一个语句中初始化多个变量会导致NullReferenceException

我试图在列表中嵌套对象列表; 我尝试过以下方法:

实例:

  public static List<PhonePeople> Helpdesk, Admins = new List<PhonePeople>();
  public static List<List<PhonePeople>> PDepartments = 
          new List<List<PhonePeople>>{Helpdesk,Admins};
Run Code Online (Sandbox Code Playgroud)

尝试添加到帮助台列表,如下所示:

 MainWindow.PDepartments[counter].Add(thisPerson);
Run Code Online (Sandbox Code Playgroud)

错误:

"你调用的对象是空的"

我将counter定义为0,0是帮助台的索引.Mainwindow是静态列表所在的位置.我相信我的列表清单或我添加的方式存在问题;

怎么了?

c# nullreferenceexception

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

如何检查字符串是否为null并在同一行中分配其值

如果我有一个有一行的数据表,如何检查字段是否为空以避免异常并在同一行中分配它的值?

txt_objective.Text = dtReqMas.Rows[0]["objective"].ToString().Trim();
Run Code Online (Sandbox Code Playgroud)

现在我要检查dtReqMas.Rows[0]["objective"]的null ,并设置txt_objective.Text = String.Empty;是否为空.

c# linq asp.net string nullreferenceexception

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