对象引用不设置到object.Windows.forms控件的实例导致此 [英] Object reference not set to an instance of an object.Windows.forms control is causing this

查看:501
本文介绍了对象引用不设置到object.Windows.forms控件的实例导致此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例外。



我创建了一个尝试捕捉为在InitializeComponent()这样的。

 
{
的InitializeComponent();
Auto_Complete();
}
赶上(异常前)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}



然后我得到一个异常说对象未设置为一个实例对象。



我一直在寻找错误的原因。



后来我发现,错误,是因为我在XAML中增加了一个控制

 的xmlns:WF =CLR的命名空间:System.Windows.Forms的;装配= System.Windows.Forms的
< WindowsFormsHost保证金=272,10,396,42WIDTH =240>
< WF:文本框X:NAME =txtAutoProductCodeAutoCompleteMode =SuggestAppendAutoCompleteSource =CustomSource/>
< / WindowsFormsHost>

这是Windows.Forms的控制,我加入的XAML自动完成功能。



但这种异常使其难以



我已经创建了一个自动完成()功能和连接的文本框:

 无效Auto_Complete()
{
txtAutoProductCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection科尔=新AutoCompleteStringCollection();

SqlCeCommand COM =新SqlCeCommand(选择产品代码FROM Category_Master(类别名称),CON);
SqlCeDataReader博士;

{
博士= com.ExecuteReader();
而(dr.Read())
{
串aProduct = dr.GetString(0);
coll.Add(aProduct);
}
}
赶上(异常前)
{
System.Windows.Forms.MessageBox.Show(ex.Message,System.Windows.Forms.Application。产品名称,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
txtAutoProductCode.AutoCompleteCustomSource =科尔;
}

公共主窗口()
{

{
的InitializeComponent();
Auto_Complete();
}
赶上(异常前)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}

}

下面是个例外。



  System.Windows.Markup.XamlParseException了未处理
HResult的= -2146233087
消息=结算初始化。主窗口引发了异常。'行号'6'和线位9。
来源= PresentationFramework
LINENUMBER = 6
LinePosition = 9
堆栈跟踪:
在System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,布尔skipJournaledProperties ,对象rootObject,XamlObjectWriterSettings设置,开放的基本URI)
在System.Windows.Markup.WpfXamlLoader.LoadBaml(xamlReader xamlReader,布尔skipJournaledProperties,对象rootObject,XamlAccessLevel ACCESSLEVEL,乌里基本URI)
在System.Windows.Markup .XamlReader.LoadBaml(流流,parserContext parserContext,对象的父母,布尔closeStream)
在System.Windows.Application.LoadBamlStreamWithSyncInfo(流流,parserContext PC)
在System.Windows.Application.LoadComponent(URI resourceLocator,布尔bSkipJournaledProperties)
在System.Windows.Application.DoStartup()在System.Windows.Application
< .ctor> b__1(对象未用)
在System.Windows.Threading程序.ExceptionWrapper.InternalRealCall(委托回调,对象指定参数时,的Int32 numArgs)
在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象指定参数时,的Int32 numArgs,代表catchHandler)
。在系统在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回调.Windows.Threading.DispatcherOperation.InvokeImpl()
,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态)
在System.Windows.Threading.DispatcherOperation.Invoke()
在System.Windows.Threading.Dispatcher.ProcessQueue()
在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr的HWND, INT32味精,IntPtr的的wParam,lParam中的IntPtr,布尔和放大器;处理)
在MS.Win32.HwndWrapper.WndProc(IntPtr的HWND,味精的Int32,IntPtr的的wParam,lParam中的IntPtr,布尔和放大器;处理)
在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)
。在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表回调,对象指定参数时,的Int32 numArgs)
在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象指定参数时,的Int32 numArgs,代表catchHandler )在System.Windows.Threading.Dispatcher.LegacyInvokeImpl(优先级的DispatcherPriority,时间跨度超时,代表法,对象ARGS
,的Int32 numArgs)
在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr的HWND,味精的Int32, IntPtr的的wParam,lParam的的IntPtr)
在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&安培;味精)
在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame帧)
在System.Windows。在System.Windows.Threading.Dispatcher.Run Threading.Dispatcher.PushFrame(DispatcherFrame帧)
()
在System.Windows.Application.RunDispatcher(对象忽略)
在System.Windows。 Application.RunInternal在(窗口的窗口)
在System.Windows.Application.Run(窗口窗口)
在System.Windows.Application.Run()
在Billing.App.Main() C:\Users\kamalmohan\Documents\Visual工作室2012\Projects\Billing\Billing\obj\Debug\App.g.cs:在System.AppDomain线0
。 _nExecuteAssembly(RuntimeAssembly组装,字串[] args)
在System.AppDomain.ExecuteAssembly(字符串assemblyFile,证据assemblySecurity,字串[] args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly
()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext。运行(执行上下文的ExecutionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
的InnerException信息:System.InvalidOperationException
HResult的= -2146233079
消息=不能有嵌套的BeginInit在同一实例调用。
来源= PresentationFramework
堆栈跟踪:在System.Windows.FrameworkElement.BeginInit
()
在MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType,obj对象,布尔开始)
的InnerException:


解决方案

这可能以及帮助解决为您制定出准确的问题所在。




XAMLParseException是WPF中抛出一个共同的异常。不幸的是
是不是非常有帮助。



要帮助找出真正的错误是什么,您可以打开例外
报告在更早视觉工作室。默认组合键
按Ctrl + Alt + E从那里,检查所有的箱子。



现在是在你的代码抛出的异常将被高亮
调试器。




http://stackoverflow.com/a/9045411/427684


I was having an exception.

I created a try catch for the InitializeComponent() like this.

try
{
    InitializeComponent();
    Auto_Complete();
}
catch(Exception ex)
{
    System.Windows.Forms.MessageBox.Show(ex.Message);
}

Then i get an exception saying "Object reference not set to an instance of an object.".

I was looking for the cause of the error.

Then i found that the error was due to a control i added in xaml.

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
            <WindowsFormsHost Margin="272,10,396,42" Width="240">
                <wf:TextBox x:Name="txtAutoProductCode" AutoCompleteMode="SuggestAppend" AutoCompleteSource="CustomSource" />
            </WindowsFormsHost>

This is windows.forms control which i added into xaml for autocomplete capability.

But this exception is making it difficult.

i have created an autocomplete() function and connected the textbox:

    void Auto_Complete()
    {
        txtAutoProductCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection coll = new AutoCompleteStringCollection();

        SqlCeCommand com = new SqlCeCommand("SELECT ProductCode FROM Category_Master(CategoryName)", con);
        SqlCeDataReader dr;
        try
        {
            dr = com.ExecuteReader();
            while (dr.Read())
            {
                string aProduct = dr.GetString(0);
                coll.Add(aProduct);
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        txtAutoProductCode.AutoCompleteCustomSource = coll;
    }

    public MainWindow()
    {
        try
        {
            InitializeComponent();
            Auto_Complete();
        }
        catch(Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
        }

    }

Here is the exception

        System.Windows.Markup.XamlParseException was unhandled
  HResult=-2146233087
  Message='Initialization of 'Billing.MainWindow' threw an exception.' Line number '6' and line position '9'.
  Source=PresentationFramework
  LineNumber=6
  LinePosition=9
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
       at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
       at System.Windows.Application.DoStartup()
       at System.Windows.Application.<.ctor>b__1(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at Billing.App.Main() in c:\Users\kamalmohan\Documents\Visual Studio 2012\Projects\Billing\Billing\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=Cannot have nested BeginInit calls on the same instance.
       Source=PresentationFramework
       StackTrace:
        at System.Windows.FrameworkElement.BeginInit()
        at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)
       InnerException: 

解决方案

This may well help solve it for you to work out exactly where the problem is

XAMLParseException is a common exception thrown in WPF. Unfortunately it isn't very helpful.

To help find out what the real error is, you can turn on exception reporting much earlier in Visual Studio. Default key combination is Ctrl + Alt + E. From there, check all the boxes.

Now the exception that is thrown in your code will be highlighted in the debugger.

http://stackoverflow.com/a/9045411/427684

这篇关于对象引用不设置到object.Windows.forms控件的实例导致此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆