-1

I know that this issue has been treatise many times but I think this time is different. In my Vb program, I have a checkbox and an image. When the user clicks on the checkbox, the visible image becomes invisible and so on. The problem is that I wrote the code but when I compile it, no error occurs, but when I run it a problem tells me "Object reference not set to an instance of an object" saying that the image returns Nothing. The code is very simple

If (DisplayImageCheckBox.IsChecked) Then
        NaturePhoto.Visibility = Visibility.Visible

    Else
        NaturePhoto.Visibility = Visibility.Hidden


    End If

The problem is at line 2. The strange thing is that there is no problem about the Else line. If I delete NaturePhoto.Visibility = Visibility.Visible The problem there is not; while if I delete the Else statement and I leave the line after the If one the problem remains. As I said, the NaturePhoto is the photo that disappears and appears, while the DisplayImageCheckBox is the Checkbox to make disappears and not the image. Can you help me, please? I don't know how to solve.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • I don't think the `NaturePhoto` object has been initialized. – OneFineDay Aug 12 '17 at 18:25
  • How do I initialize an image? Haven't I initialized when I set the name of the Image in the Properties Window yet? – Roberto Chiaiese Aug 12 '17 at 18:32
  • @RobertoChiaiese What is the image? is it a PictureBox that you have dragged onto your form or have you declared it in your code? For example `Dim NaturePhoto as Bitmap` or something else? – David Wilson Aug 12 '17 at 20:28
  • I think you are using a check box and a PictureBox to do this right if so then you can try this. If DisplayImageCheckBox.Checked = True Then NaturePhoto.Visible = True Else NaturePhoto.Visible = False End If - It is difficult to write code in comment but see if it helps. – Mohammed Julfikar Ali Mahbub Aug 13 '17 at 00:04
  • I uploaded a video for you on my channel because I was not able to add answer to this as Plutonix marked your question as Duplicate. Here is the link to the video: https://www.youtube.com/watch?v=GD_BHpdjUAs – Mohammed Julfikar Ali Mahbub Aug 13 '17 at 00:43
  • That also looks like `WPF` - not the same as the video, but that is not the problem either. Somehow the `NaturePhoto` object is null. Once you figure out that part you are done. – OneFineDay Aug 13 '17 at 03:57
  • In an attempt to be helpful :-) There is a difference between Declaring an object of a particular type, and initializing it. When you type `Dim NaturePhoto As BitMap`, you are just declaring that `NaturePhoto` can hold an object of the type BitMap. You also have to assign a `BitMap` to it. If you could edit your question to include more code so that we might be able to run it an duplicate your problem, I and the other users here would still like to help. – David Wilson Aug 13 '17 at 15:29

1 Answers1

-1

The "Object reference not set to an instance of an object" error is very famous. It means that you are trying to access a property of an undefined object.

From the examples at: Microsoft C# FAQ:

Your situation will not be this explicit because the variable you are trying to use could be a class field, parameter, or local variable that was once instantiated but was subseqently set to null. Given that foo is null, the following code will throw a NullReferenceException:

Try to debug and find which NaturePhoto or Visibility object is null.

Fabien
  • 4,862
  • 2
  • 19
  • 33
pushStack
  • 3,315
  • 1
  • 15
  • 14
  • When I try to debug, VS tells me Visibility.Visible is set to Visible, Visibility is set to Collapsed and NaturePhoto is set to Nothing. I don't understand what I have to modify and where ( the code o the GUI) – Roberto Chiaiese Aug 12 '17 at 18:30
  • Fine, then you have to initialize NaturePhoto with the "new" keyword. Try NaturePhoto = new NaturePhoto() – pushStack Aug 12 '17 at 18:33
  • It depends on the code : if the NaturePhoto object is not set before, you have to initialize it **before** the If statment. Then compile and debug to see if it is not null. – pushStack Aug 12 '17 at 18:37
  • It says that the NaturePhoto type has not been defined. I put the line before the If statement – Roberto Chiaiese Aug 12 '17 at 18:37
  • Precisely when I put the mouse on NaturePhoto() It says to me ' The "NaturePhoto" type has not been defined. – Roberto Chiaiese Aug 12 '17 at 18:42
  • Then I suppose you have to create the NaturePhoto object. https://msdn.microsoft.com/fr-fr/library/7t9k08y5(v=vs.90).aspx Then debug again – pushStack Aug 12 '17 at 18:46
  • Why do I have to create a class? In the past, I used images in Visual Basic without creating a class about the image. I used VS 2017. It seems strange – Roberto Chiaiese Aug 12 '17 at 18:49
  • 2
    I'm normally a patient person, but when people come here who are learning vb.net and object oriented programming and don't know how to complete basic steps. it gets VERY wearing. Go and read a book about VB.net for beginners. DONT skip any pages. DO the tutorials. DONT go to the next chapter until you understand the one you're reading! Pretty please. Sorry. Rant over. – David Wilson Aug 12 '17 at 20:13
  • Excuse me David but I don't think you know me, do I? I have spent the last 5 days to try to install VS 2013: the company I applied for they require vb.net 13 that is a bit different from the 2017 and they told me to follow a specific guide. I'm exasperated because of I have met only mistakes in these weeks so please don't come here to say what I have to do. At the end, I chose to use VS 2017 with a guide to a book of VS 13. I noticed that as I said things are different so because of I'm a beginner I'm finding the study of this simple language a bit hard. Please don't speak if you don't know. – Roberto Chiaiese Aug 13 '17 at 01:09
  • And just for the record, my book talks about creating a class after the middle of the book. And as I said things are different so I have to adapt vb.net 2013 with Visual Studio 2017. I wrote these comments just to clear the situation, not to make controversy. Perhaps I would have reacted in the same way If I would have been an expert programmer like you, but as you don't know and that I follow each chapter and page-to-page I say you are wrong. We went off topic enough. – Roberto Chiaiese Aug 13 '17 at 01:21
  • @RobertoChiaiese : `I used images in Visual Basic without creating a class about the image.` - You don't need to create a _**new**_ class, however you _**may**_ need to initialize `NaturePhoto` to a new instance of whatever type it is currently defined as, depending on the situation. Is `NaturePhoto` a UI element/control or a variable? If the former, can you please tell/show me _where_ you run this code? – Visual Vincent Aug 13 '17 at 07:55
  • @RobertoChiaiese No you are correct, I don't know you, and as I said at the bottom of my message. Sorry. I am far from an expert programmer and would like to help people with their problems, but to help, we need to know detail. And so many new users don't truly learn the fundamentals. After 20 months on Stack Overflow, I've seen many questions about fundamental things. For example - there are 18,920 questions about NullReferenceExceptions. Most are related to basic mistakes. This is the first time I ranted, and I hope it will never happen again. I also hope I havent put you off this great site – David Wilson Aug 13 '17 at 15:25