Tag: nullreferenceexception

为什么在单击ToolStrip按钮两次时抛出NullReferenceException – openFileDialog.showDialog()?

我创建了一个干净的WindowsFormsApplication解决方案,在主窗体上添加了一个ToolStrip ,并在其上放置了一个按钮。 我还添加了一个OpenFileDialog ,因此ToolStripButton的Click事件如下所示: private void toolStripButton1_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); } 我没有改变任何其他属性或事件。 有趣的是,当我双击ToolStripButton (第二次单击必须非常快,在对话框打开之前),然后取消两个对话框(或选择一个文件,它并不重要),然后单击客户端主窗体的区域, NullReferenceException崩溃应用程序(post末尾附加的错误详细信息)。 请注意, Click事件是在DoubleClick未执行时实施的 。 更奇怪的是,当OpenFileDialog被任何用户实现的表单替换时, ToolStripButton 阻止被点击两次 。 我在Windows 7 Professional (来自MSDNAA)上使用VS2008和.NET3.5进行最新更新。 我没有在VS中更改很多选项(只有fontsize,工作区文件夹和行编号)。 有谁知道如何解决这个问题? 它在我的机器上是100%可复制的,是否也在其他机器上? 我能想到的一个解决方案是在调用OpenFileDialog.ShowDialog()然后再启用按钮之前禁用该按钮(但这并不好)。 还有其他想法吗? 现在承诺的错误细节: System.NullReferenceException未处理 Message =“对象引用未设置为对象的实例。” 来源= “System.Windows.Forms的” 堆栈跟踪: 在System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG&msg,HandleRef hwnd,Int32 msgMin,Int32 msgMax,Int32 remove) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 […]

Xamarin自定义UITableViewCell抛出System NullReferenceException

我正在为iOS创建一个Xamarin应用程序,我已经在故事板中添加了一个UITableViewCell,以赋予它自己的风格。 我确实为这个自定义UITableViewCell添加了一个类,即MainMenuCell。 我在单元格中添加了两个标签,并将它们与MainMenuCell.h文件连接起来,产生以下代码: MainMenuCell.cs using System; using Foundation; using UIKit; namespace MyProjectNamespace { public partial class MainMenuCell : UITableViewCell { public MainMenuCell (IntPtr handle) : base (handle) { } public MainMenuCell () : base () { } public void SetCellData() { projectNameLabel.Text = “Project name”; projectDateLabel.Text = “Project date”; } } } MainMenuCell.h(自动生成): using Foundation; […]

Base64反序列化期间的空引用exception(C#)

我使用以下方法序列化和反序列化.NET对象: public static string SerializeToBase64(object data) { var stream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(stream, data); stream.Position = 0; return Convert.ToBase64String(stream.ToArray()); } public static object DeserializeFromBase64(string data) { var stream = new MemoryStream(Convert.FromBase64String(data)); stream.Position = 0; var formatter = new BinaryFormatter(); return formatter.Deserialize(stream); } 使用标有[Serializable]属性的简单类时,这些方法似乎工作正常。 但是我需要使用这段代码来序列化由ORM框架创建的实体类(也称为Serializable),其中每个实体类都是从我没有源代码的基类派生的。 使用实体类的实例时,它完成序列化而没有exception,但反序列化总是在执行formatter.Deserialize()时抛出空引用exception。 我不太熟悉序列化的过程,但我认为这个问题必定是由目标对象状态中的exception引起的。 在序列化之前,对象必须满足一组标准标准吗? 任何其他调试建议将不胜感激。 蒂姆,谢谢 更新: […]

启动时Visual Studio Debuggerexception

启动我的WinForms项目(项目只包含一个源文件,Program.cs)时,调试器在以下exception中没有堆栈跟踪的情况下中断: System.ArgumentNullException was unhandled Message=”Value cannot be null.\r\nParameter name: activationContext” Source=”mscorlib” ParamName=”activationContext” StackTrace: at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) at System.Activator.CreateInstance(ActivationContext activationContext) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: 在我的Main()函数输入之前抛出此exception,所以我不知道如何解决这个问题。 这是在Visual C#2008中。该项目是为ClickOnce发布而设置的。

Visual Studio null引用警告 – 为什么没有错误?

我注意到了Visual Studio特有的一些东西。 首先,尝试在函数中的某处键入此(C#): class Foo { public void Bar() { string s; int i = s.Length; } } 现在,它会立即将s.Length的s标记为错误,并说“ Use of unassigned local variable ‘s’ ”。 另一方面,请尝试以下代码: class Foo { private string s; public void Bar() { int i = s.Length; } } 它将编译,并在带有警告的private string s为s加下划线,称“ Field ‘Foo.s’ is never assigned to, and will always […]

在C#中使用List 时,对象引用未设置为对象实例

我有以下代码片段,产生编译错误: public List batchaddresses; public MapFiles(string [] addresses) { for (int i = 0; i < addresses.Count(); i++) { batchaddresses.AddRange(Directory.GetFiles(addresses[i], "*.esy")); } } 我尝试使用List.AddRange()方法时List.AddRange() : Object reference not set to an instance of an object 我究竟做错了什么?

如何在源为https uri时播放wpf MediaElement

在wpf独立应用程序(.exe)中,我在MainWindow中包含了一个MediaElement 从后面的代码我将其Source设置为任何https Uri: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var source = new Uri(“https://stream_which_can_be_opened_with_windows_media_player.com”, UriKind.Absolute); Player.Source = source; Player.Play(); } } 调用Play()方法时,抛出NullReferenceException而不是播放媒体内容。 初始化MediaElement ,从Play()方法抛出NullReferenceException ,见下文。 可以在Windows Media Player(文件 – >打开URL)中打开video的相同Uri。 问题似乎在MediaPlayerState.OpenMedia方法( MediaElement内部使用的对象)中,该方法尝试检查从SecurityHelper.ExtractUriForClickOnceDeployedApp检索到的appDeploymentUri是否具有HTTPS方案。 该应用程序未使用ClickOnce部署(它具有独立安装程序)且appDeploymentUri为null,因此为NullReferenceException 。 这是来自PresentationFramework.dll,System.Windows.Media.MediaPlayerState.OpenMedia if (SecurityHelper.AreStringTypesEqual(uriToOpen.Scheme, Uri.UriSchemeHttps)) { // target is HTTPS. Then, elevate ONLY if we are NOT […]

C ++回调到C#函数期间的NullReferenceException

开发商! 我有一个非常奇怪的问题。 我的项目有用C ++编写的DLL和用C#编写的GUI。 我已经实现了一些互操作性的回调。 我计划在某些情况下C ++ DLL会调用C#代码。 它有效…但不长,我不明白为什么。 在C#部分的注释中标出了问题 这里是简化示例的完整代码: C ++ DLL: #include #define WIN32_LEAN_AND_MEAN #include BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } extern “C” { typedef void (*WriteSymbolCallback) (char Symbol); WriteSymbolCallback Test; […]

空对象引用错误

我创建了一个类如下 public class Table { public enum ColumnNames { ID, TableName, Active, Date } public List Parameters { get; set; } } public enum Types { INT, STRING, DATETIME, BOOLEAN, DECIMAL } public class TableParameter { public Table.ColumnNames ParameterName { get; set; } public Types? Tip { get; set; } public dynamic Value { get; […]

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

我有一个类clsPerson,看起来像这样: public class clsPerson { public string FirstName; public string LastName; public string Gender; public List Books; } 我有另一个类,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; } } 我编写了一个程序来测试将对象序列化为XML。 到目前为止,这就是我所拥有的: class Program […]