Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I avoid 'Object reference not set to an instance of an object'? It happens when I change BulkyWeb to BulkyBookWeb. I can't change rename.
My program is on Github on https://github.com/Thibaut501/Bulky.Please[^]
Any help? Thanks!

What I have tried:

I changed the name. It didn't succeed. Tried other changes, but in vain.
Posted
Updated 29-Aug-23 1:54am
v2

It is telling you that the variable is not initialised before use. Example, to initialise a collection:
C#
List<int> numbers = new List<int>();

The link is broken, giving a 404. I would say that the repo is set to private. Without code, I can't say more.
 
Share this answer
 
To add to what Graeme has said ... This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Without code my guess is about the same as any others.

Your object 'BulkyWeb' has not been created or initialized properly, make sure that you have actually created an instance of the object. If you have created the object, ensure that you have properly initialized it before trying to rename it. Before renaming the object, do a null check to make sure that the object is not null. If it is null, you will need to create and initialize it before renaming, as an example -
C#
//Check if the object exists and is not null...
if (BulkyWeb != null)
{
    //Rename your object...
    BulkyWeb.Name = "BulkyBookWeb";
    //Add code to work with the renamed object...
}
else
{
    //If the object is null, create and initialize it first to avoid your error...
    BulkyWeb = new YourObjectType(); //Replace 'YourObjectType' with the actual type you are using...
    BulkyWeb.Name = "BulkyBookWeb";
    //Add code to work with the new created and renamed object...
}
 
Share this answer
 
Comments
fab joey Thibaut 29-Aug-23 11:58am    
how do I make a null check?thanks.
Andre Oosthuizen 29-Aug-23 13:26pm    
Mmmm, did you read the code I supplied? - if (BulkyWeb != null)
fab joey Thibaut 30-Aug-23 0:41am    
Yes,I read.
Andre Oosthuizen 30-Aug-23 4:46am    
Ok, that is the answer then, you check if the object is not null as per my code, if it is, you create and initialize it first

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900