|
-
Oct 4th, 2006, 10:18 AM
#1
Thread Starter
Fanatic Member
But I get error: Object reference not set to an instyance of an object.
When I define my class like:
public class ProductEntity
{
private int productID;
// Public properties, to expose the state of the entity
public int ProductID
{
get { return productID; }
set { productID = value; }
}
......
then to bind the ProductID to my textbox.Text in a form I do:
textBox1.DataBindings.Add("Text", myProductObject, "ProductID");
But I get error: Object reference not set to an instyance of an object.
But When I initialize the value of productID like in
private int productID=0;
that fixes the problem
My question:
I don t see why I should initialize that value.
Is there a way not to get the error without initializing productID .
and what s a good practice for this pls (I mean binding Business entity classes to form controls)
Thanks
P.S: this is C# but VB.Net is practically the same syntax.
-
Oct 4th, 2006, 10:41 AM
#2
Banned
Re: But I get error: Object reference not set to an instyance of an object.
How can you define nullability within an object? You cannot, therefore the ProductID or the Product object entity itself cannot have a nullable state,especially when it is bound.
Remember null is something but not nothing...actually null can include the universe..or not..or so...or nevermind, you get the idea.
PS: why do you use databindings ? Why not just use functions within your class that return / (set / get) statements to get values back and store them into a textbox control ?
-
Oct 4th, 2006, 10:44 AM
#3
Re: But I get error: Object reference not set to an instyance of an object.
if youre aware that this is c# code, then why dont you post in c# forum?
-
Oct 4th, 2006, 10:48 AM
#4
Banned
Re: But I get error: Object reference not set to an instyance of an object.
His issue can apply to any programming language, it is more of OOP fundamentals than it is to a programming language in general.
-
Oct 4th, 2006, 10:49 AM
#5
Re: But I get error: Object reference not set to an instyance of an object.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 4th, 2006, 11:32 AM
#6
Banned
Re: But I get error: Object reference not set to an instyance of an object.
Didnt need to be moved but umm ok..
His question is not language specific.
-
Oct 4th, 2006, 11:35 AM
#7
Re: But I get error: Object reference not set to an instyance of an object.
I understand that but having threads in their appropriate language forum aids in searches. What if you had an issue and searched in a specif .net forum but the solution wasnt posted in the appropriate forum, you wouldnt get it listed in your hit list.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 4th, 2006, 11:41 AM
#8
Banned
Re: But I get error: Object reference not set to an instyance of an object.
-
Oct 4th, 2006, 11:49 AM
#9
Re: But I get error: Object reference not set to an instyance of an object.
If you do an advanced search and get more then 500 hits it wont show them all, limited to 500. so you would refine your search by limiting the forums searched in. 
This has been the policy from day one.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 4th, 2006, 11:50 AM
#10
Banned
Re: But I get error: Object reference not set to an instyance of an object.
Policy smolishy...my answer was not geared to your policy it was geared towards the original poster, forget the politics people are interested in answers, not a smart alec .
-
Oct 4th, 2006, 11:54 AM
#11
Re: But I get error: Object reference not set to an instyance of an object.
I'm being completely professional here and as such we shouldnt take this thread anymore off topic. 
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 4th, 2006, 11:55 AM
#12
Banned
Re: But I get error: Object reference not set to an instyance of an object.
-
Oct 4th, 2006, 01:37 PM
#13
Thread Starter
Fanatic Member
Re: But I get error: Object reference not set to an instyance of an object.
Please guys. it s all my fault. sorry about the confusion. I didn t mean to get discussions off .Net topics. pls accept my appology.
Let s talk technology.
Thanks for your time amigos.
-
Oct 4th, 2006, 02:19 PM
#14
Re: But I get error: Object reference not set to an instyance of an object.
The DataBindings.Add method of a control has 3 parameters,
Code:
Control.DataBindings.Add("Text_property_of_control", DataSetName, "TableName.ColumnName");
So TableName.ColumnName, basically the 3rd parameter cannot be null. So, when you dont initialise the variable, that parameter is null by default and hence cannot be accepted by the Add function. So either initialise the variable or set a value before binding it to any data.
When you bind the dataset to the control, column will give some value to that parameter so that the control could display it.
Also, use [New Binding] keyword.
Code:
Control.DataBindings.Add(New Binding("Text_property_of_control", DataSetName, "TableName.ColumnName"));
-
Oct 4th, 2006, 08:31 PM
#15
Thread Starter
Fanatic Member
Re: But I get error: Object reference not set to an instyance of an object.
why is it better to use .Add(New...... [B]Binding pls
Thanks
-
Oct 5th, 2006, 02:33 AM
#16
Re: But I get error: Object reference not set to an instyance of an object.
 Originally Posted by tutus
why is it better to use .Add(New...... [B]Binding pls
Thanks
Just a opinion. Most of the times, not using these keywords results in unexpected results.
Also, Databindings property returns a ControlBindingsCollection type, that contains the Binding objects for the control. I don't know how to explain it, but just like you use New keyword when dealing with Arraylists or some sort of collections (it is somewhat required), the same concept goes for this thing. No matter how may controls you bind, it will return the same ControlBindingsCollection.
-
Oct 5th, 2006, 02:53 AM
#17
Re: But I get error: Object reference not set to an instyance of an object.
You don't need to use the "New Binding" bit. The ControlBindingsCollection.Add method is overloaded so you're actually calling two different methods. One is named "Add" and takes three arguments of types String, Object and String The other is also named "Add" and takes a single argument of type Binding. There are several other overloads of the Add method too. If you call the one that takes a Binding argument it just adds that Binding. If you call one of the others they just create a Binding object using the values you specify and then call the one that takes a Binding argument. It's exactly the same result regardless with the only difference being whether you want to explicitly create a Binding object or let the system create on implicitly.
-
Oct 14th, 2006, 07:44 PM
#18
Thread Starter
Fanatic Member
Re: But I get error: Object reference not set to an instyance of an object.
jmcilhinney, can u pls demistify the term overload for me. what s a simple definition for that in plain english (sorry i don t have strong software background)
thanks
-
Oct 14th, 2006, 07:51 PM
#19
Re: But I get error: Object reference not set to an instyance of an object.
To overload a member means to provide two or more implementations with the same name but different signatures, i.e. different argument lists. Here is the overload list for the ControlBindingsCollection.Add method. There are seven overloaded implementations of the Add method. That means that you can call Add and pass a Binding object that you've already created or various combinations of values required to build a Binding object for you. The system is smart enough to know which overload to invoke based on the types of the parameters you pass when you call it. If your parameters are ambiguous, i.e. they could apply to more than one overload, then the compiler will throw an error and refuse to continue until you clarify the parameters such that they can apply to only one overload.
-
Oct 14th, 2006, 08:04 PM
#20
Thread Starter
Fanatic Member
Re: But I get error: Object reference not set to an instyance of an object.
Yes yes thanks for making the right association for me. I have seen that in MSDN documentation and had no idea what it s called. Now I got it. thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|