标签: nullreferenceexception

为什么字符串属性上的GetType会导致NullReferenceException?

当我在int-或DateTime属性上调用GetType时,我得到了预期的结果,但是在字符串属性上,我得到一个NullReferenceException(?):

private int      PropInt    { get; set; }
private DateTime PropDate   { get; set; }
private string   propString { get; set; }

WriteLine(PropInt.GetType().ToString());    // Result : System.Int32
WriteLine(PropDate.GetType().ToString());   // Result : System.DateTime
WriteLine(propString.GetType().ToString()); // Result : NullReferenceException (?!)
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下怎么来?字符串支柱与int-prop有什么不同?

c# properties gettype nullreferenceexception

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

.NET - 非常奇怪的NullReferenceException?

如何NullReferenceException在以下场景中获得?

 Dim langs As IEnumerable(Of SomeCustomObject) = //some LINQ query
 If langs Is Nothing Then Return Nothing 
 If langs.Count = 1 Then //NullReferenceException here
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?Debug显示这langs实际上只是一个没有任何结果的LINQ queryresult ...

linq vb.net nullreferenceexception

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

你调用的对象是空的

我有以下代码 -

private static void convert()
    {
        string csv = File.ReadAllText("test.csv");
        XDocument doc = ConvertCsvToXML(csv, new[] { "," });
        doc.Save("update.xml");

        XmlTextReader reader = new XmlTextReader("update.xml");
        XmlDocument testDoc = new XmlDocument();
        testDoc.Load(@"update.xml");

        XDocument turnip = XDocument.Load("update.xml");
        webservice.function[] test = new webservice.function[1];
        webservice.function CallWebService = new webservice.function();

        foreach(XElement el in turnip.Descendants("row"))
        {
                            test[0].com = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "com").SingleOrDefault().Attribute("value").Value);
            test[0].Centre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "Centre").SingleOrDefault().Attribute("value").Value;
            test[0].CCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "CCentre").SingleOrDefault().Attribute("value").Value;

            MessageBox.Show(test[0].person, "person");
            MessageBox.Show(System.Convert.ToString(test[0].actually), "Actually");
            MessageBox.Show(System.Convert.ToString(test[0].com), "Com");

            CallWebService.updateFeedStatus(test);
        }
Run Code Online (Sandbox Code Playgroud)

它出现了错误 …

c# xml web-services object nullreferenceexception

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

string.IsNullOrEmpty()似乎不能处理类中类的字符串

首先,我想我知道发生了什么,但我想我会把这个问题带到这里进行一些讨论,看看是否有人对这个问题有"回答",而不是我在想什么.因为,这对我来说并不完全有意义.

我发现在为异常创建错误日志时,我这样做并且它无法正常工作:

catch( Exception ex )
{
   LogException( ex.Message );
   if ( !string.IsNullOrEmpty( ex.InnerException.Message ) )
   {
      LogInnerException( ex.InnerException.Message );
   }
}
Run Code Online (Sandbox Code Playgroud)

瞧,当我跑这个时,我经常得到一个NullReferenceException.咦?

我正在检查null,对吗?

现在,我必须使用这个:

   if ( ex.InnerException != null && !string.IsNullOrEmpty( ex.InnerException.Message ) 
Run Code Online (Sandbox Code Playgroud)

但这似乎是违反直觉的,也会产生反作用.因为,如果我这样做:

   if ( !string.IsNullOrEmpty( null ) )
Run Code Online (Sandbox Code Playgroud)

这根本不会给我任何问题.如果ex.InnerException为null,那么当然ex.InnerException.Message为null,对吗?

显然不是.

我写了一个完整的控制台应用程序来重现这个.如果你

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace stringisnullorempty
{
    class Program
    {
        static void Main( string[] args )
        {
            if ( !string.IsNullOrEmpty( null ) )
            {
                Console.WriteLine( "Ha ha ha, right...." );
            }

            MyBClass …
Run Code Online (Sandbox Code Playgroud)

.net c# string nullreferenceexception isnullorempty

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

为什么我的事件会抛出空错误?

出于某种原因,我的事件抛出了一个空错误.我没有看到任何错误.

这是事件

public delegate void connectionSuccess(bool success);
public event connectionSuccess Connection;
Run Code Online (Sandbox Code Playgroud)

然后在一个函数我有这个女巫抛出null错误

Connection(true);
Run Code Online (Sandbox Code Playgroud)

在另一个调用连接函数的类中编辑*函数

Server.Connection += onConnection;
Server.startConnection();
Run Code Online (Sandbox Code Playgroud)

c# events nullreferenceexception

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

C#ListBox选择项目空例外

用户可以单击ListBox中的项目,如下所示:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    textBox2.Clear();
    listBox2.Items.Clear();

    string[] p = 
         Directory.GetFiles(
             textBoxDir.Text, 
             listBox1.SelectedItem.ToString(),
             SearchOption.AllDirectories);

    foreach (string open in p) 
        ......
}
Run Code Online (Sandbox Code Playgroud)

一切都很好.但是,如果用户单击ListBox中的空白区域,则会显示以下错误:

System.NullReferenceException
Run Code Online (Sandbox Code Playgroud)

这是因为这行代码:

string[] p = 
    Directory.GetFiles(
        textBoxDir.Text, 
        listBox1.SelectedItem.ToString(),   
        SearchOption.AllDirectories);
Run Code Online (Sandbox Code Playgroud)

有没有人有聪明的工作?或者建议我的代码替代?

c# listbox nullreferenceexception

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

空引用异常C#get set

我认为我误解了获得的目的; 组; 在C#中.我有一个我正在尝试填充的双打列表,我正在使用以下代码...这两个都在同一个类中,当我尝试运行它时,我在尝试填充列表时得到一个Null引用异常.我到底误解了什么?

public List<double> NewData
{ get; set; }

public InfoFile(String fileName, String groupName)
{

    this.group = groupName;
    test = File.ReadAllLines(fileName);
    FileInfo label = new FileInfo(fileName);
    this.name = Path.GetFileName(fileName);
    isDrawn = false;

    for (int t = 2; t < test.Length; t++)
    {
        NewData.Add(double.Parse(test[t].Substring(6, 4)));
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# list nullreferenceexception

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

System.Collections.Generic.LinkedList`1.AddLast(T value)中的NullReferenceException

我LinkedList<T>在我的项目中添加和删除了很多元素(每秒几百个).这是通过锁同步的多个线程发生的.

现在有时(可能每几个100.000元素)我得到一个NullReferenceException"内部" AddLast方法,所以堆栈跟踪的顶部条目如下:

在System.Collections.Generic.LinkedList`1.AddLast(T值)

有人知道为什么会发生这种情况以及如何避免这种情况?

一个简单/愚蠢的try-> catch->重复将是我最好的主意,但这更像是一个肮脏的解决方法......

c# multithreading linked-list nullreferenceexception

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

尝试使用Request.QueryString时获取对象引用错误

我有一个page.aspx读取查询字符串,如果QueryString类似于page.aspx?id = 1,它会运行一些代码.

但是,如果没有指定QueryString,我希望它只是忽略该代码.如果我只是去page.aspx.现在,我在页面加载时唯一的代码是

if (Request.QueryString["id"].Equals("1"))
  {
     //Do something
  }
Run Code Online (Sandbox Code Playgroud)

如果我去page.aspx?id = 1那就行.但是,只需转到page.aspx就会出现此错误:

你调用的对象是空的

如何加载没有查询字符串的页面?

c# asp.net nullreferenceexception query-string

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

如何处理异常以及为什么有时可能会忽略它们

我的代码的重要部分如下所示:

sensor.ColorFrameReady += sensor_ColorFrameReady;

void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs colorImageFrameReadyEventArgs)
    {
        using (var frame = colorImageFrameReadyEventArgs.OpenColorImageFrame())
        {
            var bitmap = CreateBitmap(frame);
            Canvas.Background = new ImageBrush(bitmap);
        }
    }

private static BitmapSource CreateBitmap(ColorImageFrame frame)
    {
        var pixelData = new byte[frame.PixelDataLength];
        frame.CopyPixelDataTo(pixelData);
        var stride = frame.Width * frame.BytesPerPixel;
        var bitmap = BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, pixelData, stride);
        return bitmap;
    }

private void PictureButton_Click(object sender, RoutedEventArgs e)
    {
        using(var frame = sensor.ColorStream.OpenNextFrame(0))
        {
            var bitmap = CreateBitmap(frame);
            Canvas.Background = new ImageBrush(bitmap);
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling nullreferenceexception kinect-sdk visual-studio-2013

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