Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,208,300 members, 8,002,181 topics. Date: Thursday, 14 November 2024 at 06:24 AM |
Nairaland Forum / Science/Technology / Programming / Null Reference Exception Object Reference Not Set To An Instance Of An Object In (1705 Views)
Ways Wrap A Label Around An Object In Blender / Model And Add Glow To Any Object In Blender / Object In Javascript (2) (3) (4)
Null Reference Exception Object Reference Not Set To An Instance Of An Object In by wapSmart(m): 9:14am On Apr 07, 2020 |
Good day all. I am working on a project. A billing system and I have stretch it to some extent but recently I am getting this error NullReferenceException Object reference not set to an instance of an object I have searched online on how to resolve this but I haven't been able to resolve this with the solutions proffer... Please, I need your help in getting this resolved
|
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by MT: 4:49pm On Apr 07, 2020 |
I assumed you are using C#. What that means is you didn't instantiate the object. For instance , let's say you have a class named Car. If you only create the object this way: Car Ferrari {get; set} . This will create object Ferrari but nullreferenceexception error will be thrown because you haven't assigned a value to the object. You can assign a value to your object through the constructor of your class and the error will disappear. If you need further help, ask. *Click that tiny arrow beside the exception and let's see what you have got in there. Munch the screen and post it* |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by Thenaijaitguy: 6:09pm On Apr 07, 2020 |
wapSmart: So many thing can causes this when working with .net If you provide more information about the program you're running I can assist . 1 Like |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by Thenaijaitguy: 6:13pm On Apr 07, 2020 |
From my experience another thing that can cause it if you're mapping two things that do not match together . For example insert data with four values into a table that can only take two values. So share more info about your project. |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by MT: 6:43pm On Apr 07, 2020 |
Thenaijaitguy: This can never throw up such an error. There is just ONE way this can happen. One, either you don't properly initialise an object. Like I stated above, if you have this kind of class: Public car { Public void move () { // Write your code for move method } } // End of class You now initialise the object like: Car ferari {get; set;} Ferari is now an object of the class. If you now do this: Ferari.Move() //this will throw nullexception error because ferari is not properly initialised, which means it is holding null value. Another way is if you assign the class properly but what you assign the object to is returning null value e.g Car ferari = processsomething(); If processsomething() method return null value and you still try to do this: Ferari.move(); //it will throw nullexception. Generally, you can not access the property or method of an object holding a null value ,it will throw up nullexception error. The op should runhis code in a debugger and check for null. Always try and check for null before you carry out an operation on an object, it will save a lot of headache. Sorry for any error, i wrote this on the move 1 Like |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by Thenaijaitguy: 6:54pm On Apr 07, 2020 |
Bro I'm not here to argue just want to help out. The question you should ask yourself is what is an object? By then you will realize we are saying the same thing . The data I used as example is and object. The table you're inserting into is also an object. So if the two do not match such exception will come up. I have experience such exception in different project. I'm only here to learn and help . Thank you . |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by MT: 7:11pm On Apr 07, 2020 |
Thenaijaitguy: I'm just trying to help too. We are not actually saying the same thing if you look at it carefully. What we are saying is different. Your scenario can't throw up null exception. Look at it carefully. How a class can hold a table is beyond me. In a code first approach, a class doesn't hold the table. The dbcontext in an ef will create and manipulate the table stated inside the class. I only tried to explain the ONLY valid way nullexception can be thrown. Do a bit of research on it and you will agree with me. We are all here to learn. 1 Like |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by kingsangoda: 7:11pm On Apr 07, 2020 |
Can you click on the copy detail and share here |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 11:08pm On Apr 07, 2020 |
Let me first say, @MT is almost right.... There are two steps to creating an object (instantiation): 1. You create an OBJECT REFERENCE variable. 2. You create an INSTANCE OF AN OBJECT with the NEW keyword and then assign it to the variable you created in step 1. Using MT's example, if we have a class called Car, and we type the c# code below, Car Ferrari; We do not have any object yet, what we have is an OBJECT REFERENCE variable, Ferrari. If we try to access any member of the Car class with Ferrari, we will get "OBJECT REFERENCE is not set to an INSTANCE OF AN OBJECT" exception as seen in the picture. Note that OBJECTS are REFERENCE TYPES and they are stored on the HEAP, but the REFERENCE VARIABLES are VALUE TYPES, stored on the stack; the reference variable is meant to store the memory address of the INSTANCE OF THE OBJECT that it is set to and point to the object in that address (reference the object), but since we did not set Ferrari to any Car Object instance, it points to NULL or it references null (nothing), meaning that Ferrari is not a car yet, but we are now trying to invoke the Move( ) method on it. 1 Like
|
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 11:37pm On Apr 07, 2020 |
MT: Ferrari is an OBJECT REFERENCE variable not an OBJECT itself, and Ferrari will be stored on the stack like every other value type (all the primitive types apart from String are value types). Objects are stored on the Heap. |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 11:50pm On Apr 07, 2020 |
Now, we do not get that exception and the code works perfectly because we have set the OBJECT REFERENCE variable to an INSTANCE OF AN OBJECT. What this means is we have ASSIGNED A TYPE OF CAR to FERRARI. So, when we click the MOVE MY CAR button, the Move( ) method of the car class is invoked correctly on Ferrari and the message box is displayed, with no error. Car Ferrari = new Car( );
|
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 12:01am On Apr 08, 2020 |
MT: @MT is right. @wapSmart, you need to thoroughly inspect every part of your code where you have referenced other classes. |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by MT: 12:03am On Apr 08, 2020 |
SUPREMOTM: Oh yes you are right. I missed the "reference" word cos I was typing on the move. You did a good job by actually implementing it. 1 Like |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by wapSmart(m): 8:08am On Apr 08, 2020 |
Wow... I never believe I could get all this response. Thanks all. I will definitely act on the instructions you wonderful people have given and get back to you ASAP |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by wapSmart(m): 8:15am On Apr 08, 2020 |
Meanwhile, I have actually call stack to trace where the bug occurs but still can't get over it.. Attached is the point at which I am getting error. I am currently 3 months into programming and I am already enjoying the challenge with the fact that I am still a novice.
|
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by MT: 10:11am On Apr 08, 2020 |
@Op, we can neither see your code nor understand the logic you are trying to implement, we can only guide you. Run your code in debug mode, set breakpoints and check for null values. That will resolve the issue you are having. |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by Thenaijaitguy: 10:49am On Apr 08, 2020 |
Good day @ MT Good day @supermotm Is good to know that we have .net expert in the house . @op I think the problems might be from your main.cs class, can you show us the main.cs class. |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by wapSmart(m): 2:26pm On Apr 08, 2020 |
Thenaijaitguy: ullReferenceException Object reference not set to an instance of an object Application.Run(new Main()); I have attached picture to make the question more clearer using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Billing_System { public partial class Main : Form { public Main() { InitializeComponent(); } private void productToolStripMenuItem_Click(object sender, EventArgs e) { // to open child form mdi(main) form Product p = new Product(); p.MdiParent = this; p.Show(); } private void newBillToolStripMenuItem_Click(object sender, EventArgs e) { NewBill nb = new NewBill(); nb.MdiParent = this; nb.Show(); } private void viewBillsToolStripMenuItem_Click(object sender, EventArgs e) { ViewBills vb = new ViewBills(); vb.MdiParent = this; vb.Show(); } } } |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 4:13pm On Apr 08, 2020 |
Thenaijaitguy: |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 4:19pm On Apr 08, 2020 |
Duplicate Post Deleted |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 4:46pm On Apr 08, 2020 |
wapSmart: Whenever you post source code on this forum, format it so it can look organized and readable like this. To achieve that, select your code (Ctl+A), then click the octothorpe symbol (#) in nairaland's text editor before you click the Submit button. The code above only tells us that you have 4 forms (at least) and you want to set one of them as an MDI parent form. You want the 3 other forms to display as MDIChildren within the MDIParent form when certain menu items are clicked. You should know that codes are generated when you set or do not set certain properties from the Properties Window in Visual Studio Design View. Keep troubleshooting and you'll eventually get the solution since you are the only one with complete access to your source code as well as your forms' design. Don't worry, you'll be the better for it after you successfully detect the cause of the Exception. |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by wapSmart(m): 7:57pm On Apr 08, 2020 |
SUPREMOTM: Really appreciate sir.. Will act in accordance |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by MT: 8:41pm On Apr 08, 2020 |
wapSmart: @op look at this part of your code: p.MdiParent = this; nb.MdiParent = this; vb.MdiParent = this; What are you expecting "this" which represents the Main class to return. I suspect that is where the issue lies. I cannot see the full code as you only showed the partial class of main. Set breakpoints at those 3 points and run your code in debug mode. Then read what the "this" is spitting out, if it is null, then that might be the problem. @supremotm I will look at how you can subscribe to the group. Meanwhile I am not an expert o, just a student trying to learn too. 1 Like |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by ubsky(m): 9:26pm On Apr 08, 2020 |
Wow this is lovely never knew this section is still alive and have got c# peeps here too,this is nice ... |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by SUPREMOTM: 2:42pm On Apr 09, 2020 |
MT: Thanks Just announce on that thread whenever you are about to start, I'll keep calling some of the phone numbers of the other enthusiasts until someone adds me to the group. |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by wapSmart(m): 4:30pm On Apr 19, 2020 |
Thanks to you all I have been able to solve this... 1 Like |
Re: Null Reference Exception Object Reference Not Set To An Instance Of An Object In by omoluabiguy(m): 11:45pm On May 23, 2020 |
Subscribe to my channel and learn everything for free from the scratch till you become a pro. Follow me on a journey to create fullstack software engineers. On top of that, I would also be making vlogs about life as a software engineer and things to expect in the software industry. I’m a software industry veteran with years of experience and derive joy in teaching students and aspiring engineers how to go about their craft. Subscribe to my YouTube channel and watch my videos. You can thank me later by subscribing https://www.youtube.com/channel/UCwNrlP_X_VV4vg00P2QmDnw You can as well join our Facebook community where we answer questions, discuss and share ideas about technologies https://www./738709033333419/?ref=share |
(1) (Reply)
Change Of Bank Account Phone Number / Please Recommend A Good Laptop / Test Your Ethical Hacking Skills In The 2017 Capture The Flag (CTF) Hackathon
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 59 |