标签: nullreferenceexception

如何避免NullReferenceException

 if (alMethSign[z].ToString().Contains(aClass.Namespace))
Run Code Online (Sandbox Code Playgroud)

在这里,我加载一个exe或DLL并检查其命名空间.在一些dll中,没有命名空间,因此aclass.namespace不存在而且它正在抛出一个NullReferenceException.

我必须避免它,它应该继续其余的代码.如果我使用try-catch,它会执行catch部分; 我希望它继续使用其余的代码.

c# exception-handling exception nullreferenceexception

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

C#中的空引用异常

当我试图从结构中返回一个值时,我遇到了"空引用异常".

这是代码:

AssetItem item = new AssetItem(); 

        item = initModified();

        bool found = false;
        int index = getIndex(barcode);
        string modifiedFile = filepath + "Modified\\" + dir + "\\" + index + ".asdt";

        if(File.Exists(modifiedFile))
        {   
            using(StreamReader reader = new StreamReader(modifiedFile))
            {
                string line = reader.ReadLine();
                while(line.Trim()!="")
                {
                    string[] split = line.Split(',');
                    if(split[1]==barcode)
                    {
                        found = true;
                        break;
                    }
                    line = reader.ReadLine();
                }
                reader.Close();
            }
        }

        if(found)
        {
            item.modified = true; 
        }
        else
        {
            item.modified = false;
        }


        return item;
Run Code Online (Sandbox Code Playgroud)

我通过调用包含该item.modified = …

c# streaming notepad nullreferenceexception

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

你调用的对象是空的

我有这个函数创建运行时文本框:

int i = 0;
private TextBox[] addressBox = new TextBox[100];

private void appendNewTab()
{ 
    addressBox[i] = new TextBox();
    addressBox[i].KeyPress += 
        new KeyPressEventHandler(this.addressBox_KeyPress); 
    i++;
}

void addressBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
        MessageBox.Show(addressBox[i].Text);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我在这里没有将对象引用设置为对象的实例

MessageBox.Show(addressBox[i].Text);
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

.net c# nullreferenceexception

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

F#中的NullReferenceException

当我单步执行以下代码时,report第二行为null.但是,第三行生成NullReferenceException.

member this.setTaggedResearchReportList (index : int) (taggedResearchReport : TaggedResearchReportUIVO option) =
    let report = Option.get(taggedResearchReport)
    if not(report.Equals(null)) then
        // do some stuff here
Run Code Online (Sandbox Code Playgroud)

为什么会这样,我该怎么做才能避免呢?谢谢!

后来添加:

这是调用的行this.setTaggedResearchReportList:

getMostRecentTaggedResearchReportForSecurityId (item.id) (new Action<_>(this.setTaggedResearchReportList 0))
Run Code Online (Sandbox Code Playgroud)

这是getMostRecentTaggedResearchReportForSecurityId方法:

let getMostRecentTaggedResearchReportForSecurityId (securityId : int) (callbackUI : Action<_>) =
    getSingleRPCResult<JSONSingleResult<TaggedResearchReportUIVO>, TaggedResearchReportUIVO>
        "TaggedResearchReportRPC"  
        "getMostRecentResearchReportForSecurityId" 
        (sprintf "%i" securityId)
        callbackUI
        (fun (x : option<JSONSingleResult<TaggedResearchReportUIVO>>) ->
            match x.IsSome with
                | true -> Some(Option.get(x).result)
                | false -> None 
        )
Run Code Online (Sandbox Code Playgroud)

null f# nullreferenceexception

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

为什么if语句表达式不被视为条件?

我有一个组合框,里面有一个日期列表.我想确保用户实际选择日期.所以,我有以下几点:

        if (cmbDateSelecter.SelectedItem.ToString().ToLower().Contains("select") || 
            cmbDateSelecter.SelectedItem.ToString().ToLower().Contains("seleccione") || 
            cmbDateSelecter.SelectedItem == null)
Run Code Online (Sandbox Code Playgroud)

默认项目是"选择日期",所以我正在检查是否是所选项目.这曾经是自己工作的,由于某种原因,我还没想到,开始抛出NullReferenceExceptions.所以,我添加了空检查.但是,我仍然得到例外.但如果我这样做:

if(cmbDateSelecter.SelectedItem == null)

现在,我知道我可以先进行空检查,一切都会变得笨拙.我的问题是为什么在抛出异常之前不评估所有表达式?如果其中一个是真的,在我的表达式(作为空检查)的情况下,为什么它仍然抛出异常?

c# nullreferenceexception

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

对象引用未设置为对象EDIT的实例

我是新来的,我尝试查看旧问题,但我也是c#的新手,我发现很难解决下面的问题:

if (File.Exists(@"C:\ESC\Impostazioni.txt"))
{
    TextReader lettore_file = new StreamReader(@"C:\ESC\Impostazioni.txt");
    String opzioni = lettore_file.ReadLine();

    int i;
    for (i = 0; i < opzioni.Length; i++) <----here, indicating "i=0"
    {
        if (opzioni[i] == '-')
        {
             char[] coloregenerale = new char[i];
             for (int j = 0; j < i; j++)
               coloregenerale[j] = opzioni[j];

           break;
Run Code Online (Sandbox Code Playgroud)

c# nullreferenceexception

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

为什么我不能在List <>中添加对象?

我有一个类clsPerson,看起来像这样:

public class clsPerson
{
    public string FirstName;
    public string LastName;
    public string Gender;
    public List<Book> Books;
}
Run Code Online (Sandbox Code Playgroud)

我有另一个类,Book,看起来像这样:

public class Book
{
    public string Title;
    public string Author;
    public string Genre;        

    public Book(string title, string author, string genre)
    {            
        this.Title = title;
        this.Author = author;
        this.Genre = genre;
    }
}
Run Code Online (Sandbox Code Playgroud)

我编写了一个程序来测试将对象序列化为XML.到目前为止,这就是我所拥有的:

class Program
{
    static void Main(string[] args)
    {
        var p = new clsPerson();
        p.FirstName = "Kevin";            
        p.LastName = "Jennings";
        p.Gender = "Male";

        var book1 = new Book("Neuromancer", "William …
Run Code Online (Sandbox Code Playgroud)

c# nullreferenceexception

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

对营销人员的webforms保存操作会引发异常

我使用的是最新版本的Sitecore - 7.2(rev.140228)和最新版本的WFFM - 2.4 rev.140923.所以问题是无论何时我尝试提交表单,无论我有什么样的保存操作,Sitecore都会抛出异常:

你调用的对象是空的.

描述:发生了未处理的异常.

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

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   Sitecore.Forms.Mvc.Controllers.ModelBinders.FormModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +571
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +457
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +152
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +641
Run Code Online (Sandbox Code Playgroud)

一些额外的信息.最新版本的WFFM支持MVC所以在这种情况下我使用cshtml布局.这是博客文章,其中描述了如何使用MVC - 虽然它适用于以前版本的SC,但无关紧要,该解决方案适用于最新版本.

sitecore submit nullreferenceexception web-forms-for-marketers

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

"读访问违规:这是nullptr"我以为我正确分配了它?

我有一个玩家类,其中包含玩家的名字,正确的答案以及玩家得到的错误答案.当我尝试访问getRight(),getWrong(),addToRight()或addToWrong()函数时,我在这些函数内部的语句中收到一条错误,上面写着"读取访问冲突:这是nullptr".我一定不能正确设置我的指针.我应该做些什么改变?谢谢!

这是Player.h文件

#ifndef PLAYER_H
#define PLAYER_H
#pragma once

using namespace std;
class Player;//FWD declaration

class Player
{
public:
    Player();
    Player(string playerName);

    string getName() const
    {
        return name;
    }

    //These functions show stats from
    //current round
    int getRight() const
    {
        return right;
    }

    int getWrong() const
    {
        return wrong;
    }

   //These functions update
   //player info that will be saved
   //to player profile
   void setName(string userName);
   void addToRight();
   void addToWrong();

private:
     string name;
     int right;
     int wrong;
};
#endif
Run Code Online (Sandbox Code Playgroud)

这是Player.cpp文件:

#include <iostream> …
Run Code Online (Sandbox Code Playgroud)

c++ pointers class function nullreferenceexception

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

为什么在映射到null类型和从null类型映射时null不会导致NullReferenceException?

我基本上试图迭代大量数据并将返回的数据查询转换为视图模型中更有限的对象.

我没有做大量代码,而是在列表中调用.ForEach(),然后在视图模型列表中添加一个新条目.

这很好用,但是,有一个属性(Address)是可选的.

当我到达可选项时,NullReferenceException如果DB中的项目没有条目,我会得到.

代码示例是:

    var tmp = _context.Person.Include(x => x.Address).ToList();

    tmp.ForEach(x => vm.List.Add(new IndexListItem()
    {
        Name = x.Name,
        Address = x.Address.FirstLine + " " + x.Address.SecondLine,
        ID = x.ID

    }));
Run Code Online (Sandbox Code Playgroud)

我从这个网站上的不同答案中发现,如果我更改地址行,它会显示:

        Address = x.Address?.FirstLine + " " + x.Address?.SecondLine,
Run Code Online (Sandbox Code Playgroud)

当我点击一个空条目时,代码现在可以工作了tmp.

我不明白这一点,因为Address属性tmp已经允许空值,并且视图模型上的Address属性允许空值,所以,为什么更改行突然不会返回错误?

另外,我不必这样做x.Address?.FirstLine?是因为这是一个字符串,字符串已经可以为空了吗?

c# linq nullreferenceexception

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