Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2007, 04:52 PM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default User Control Instantiation Issue

I have a UserControl Class that when loaded presents a NullReferenceException.

The control is instantiated twice in my main form as follows:
Code:
private CtrlPlayer Player1 = new CtrlPlayer(1);
private CtrlPlayer Player2 = new CtrlPlayer(2);
The UserControl displays in the Design Mode without a problem, and the program compiles fine as well.

While attempting to adhere to FxCop rules I have inadvertently made a change that has caused this issue, as the control previously worked.

I have spent several hours attempting to research this via Google and have not found a resolve.

I'll be glad to respond back with any further detail needed.




Best Regards,
Earl
__________________


Best Regards,
Earl
 
Old December 12th, 2007, 05:07 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

What did you change per FxCop? Where is the exception being raised from?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old December 12th, 2007, 05:43 PM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Doug,

Thanks up front for assisting me with this one.

The changes made per FxCop would be lengthy to list as I am using it as a best practice guide. I did make changes regarding security and CLSCompliance, but both changes have been commented out and the situation persists. It is almost like the designer portion of the code is not being read.

Security changes to main form (commented out):
Code:
//[assembly: IsolatedStorageFilePermission(SecurityAction.RequestMinimum, UserQuota = 1048576)]
//[assembly: SecurityPermission(SecurityAction.RequestRefuse, UnmanagedCode = true)]
//[assembly: FileIOPermission(SecurityAction.RequestOptional, Unrestricted = true)]
CLSCompliant changes to CtrlPlayer UserControl (commented out):
Code:
//[assembly: CLSCompliant(true)]
A simple call to change the text of a groupbox on the control yields the exception. If I comment that line then the next line (and the next...) it will generate the famous "Object reference not set to an instance of an object" error on each subsequent line.

This one is not going to be easy to find (although the resolution probably will be).




Best Regards,
Earl
 
Old December 13th, 2007, 05:17 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

OK, firstly, none of the changes detailed above are changes to either the control or the form - they are all assembly properties, so it doesn't matter which source file they appear in.

Secondly, how many CtrlPlayer controls do you have on the form in design time? You create two in the code above, but then refer to it in the singular form later.

If the exception is being thrown when you try to change the groupbox text then either the user control is null, or the group box is null.

I notice you have a different constructor that is passes a number (i.e. CtrlPlayer(1)) - is this new constructor calling InitializeComponents() ?

/- Sam Judson : Wrox Technical Editor -/
 
Old December 13th, 2007, 10:42 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Thank You Sam,

As stated in the first post I thought the resolve would be easy. InitializeComponent() was not called in this constructor.



Best Regards,
Earl





Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
User Control Issue bloveall General .NET 3 September 27th, 2004 11:14 PM
Please Help with Context.User Issue niketu BOOK: ASP.NET Website Programming Problem-Design-Solution 2 June 28th, 2004 08:29 PM
ascx issue with User.Identity.Name ian.macey Classic ASP Basics 2 June 11th, 2003 02:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.