Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. UNhandled Exception of Type System.NullReferenceException

UNhandled Exception of Type System.NullReferenceException

Scheduled Pinned Locked Moved C#
graphicshelpquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    signimage
    wrote on last edited by
    #1

    In my project two forms(form1 and form2) when from form1 i try to Access any object of from2 then it give me error that object reference not set to an instance of object, me using following way in form1 i create the obg of from2 as from2 f=new fomr2(); Bitmap b=(Bitmap)f.pictureBox1.Image; if(b.GetPixel(100,100)==-1) {//code} it give the abovemention error in if statement when i try to accec the pixels of that object. What should i do?????????

    C 1 Reply Last reply
    0
    • S signimage

      In my project two forms(form1 and form2) when from form1 i try to Access any object of from2 then it give me error that object reference not set to an instance of object, me using following way in form1 i create the obg of from2 as from2 f=new fomr2(); Bitmap b=(Bitmap)f.pictureBox1.Image; if(b.GetPixel(100,100)==-1) {//code} it give the abovemention error in if statement when i try to accec the pixels of that object. What should i do?????????

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Just by instantiating the form does not necessarily mean that the picture box will have anything in it. Thefore the error is correct. Where does the picture box get its image? Once you have the answer to that then you know what else you need to instantiate in order to get your code to work.


      *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      S 2 Replies Last reply
      0
      • C Colin Angus Mackay

        Just by instantiating the form does not necessarily mean that the picture box will have anything in it. Thefore the error is correct. Where does the picture box get its image? Once you have the answer to that then you know what else you need to instantiate in order to get your code to work.


        *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        S Offline
        S Offline
        signimage
        wrote on last edited by
        #3

        PictureBox take its image from file and i m sure that pictureBox has its image,this is not empty pictureBox but still it give thte above mentioned error

        C 1 Reply Last reply
        0
        • S signimage

          PictureBox take its image from file and i m sure that pictureBox has its image,this is not empty pictureBox but still it give thte above mentioned error

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          signimage wrote:

          PictureBox take its image from file and i m sure that pictureBox has its image,this is not empty pictureBox but still it give thte above mentioned error

          Obviously, it is an empty picture box at the time you are running your code otherwise you would not be getting the error. Since you don't seem to be doing anything else with the form (at least from the code you provided) then I would suggest that you probably don't need the form anyway. Perhaps you just need to instantiate a Bitmap with the relevant file and use that.


          *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          S 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Just by instantiating the form does not necessarily mean that the picture box will have anything in it. Thefore the error is correct. Where does the picture box get its image? Once you have the answer to that then you know what else you need to instantiate in order to get your code to work.


            *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

            S Offline
            S Offline
            signimage
            wrote on last edited by
            #5

            i face the same problem when ever i try to acces any object of form from any other calss (within same project) how to acces the form object (like picture) from any other calss i use the form obj to acces the formobjfrom other class but it give reference inmstance error

            C 1 Reply Last reply
            0
            • S signimage

              i face the same problem when ever i try to acces any object of form from any other calss (within same project) how to acces the form object (like picture) from any other calss i use the form obj to acces the formobjfrom other class but it give reference inmstance error

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              That is because it is obviously not instantiated properly. A form is a class designed to store stuff that is displayed on the screen (within that form). If you are not going to display it on the screen then your design is screwed up and you should reconsider what you are doing.


              *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

              S 1 Reply Last reply
              0
              • C Colin Angus Mackay

                signimage wrote:

                PictureBox take its image from file and i m sure that pictureBox has its image,this is not empty pictureBox but still it give thte above mentioned error

                Obviously, it is an empty picture box at the time you are running your code otherwise you would not be getting the error. Since you don't seem to be doing anything else with the form (at least from the code you provided) then I would suggest that you probably don't need the form anyway. Perhaps you just need to instantiate a Bitmap with the relevant file and use that.


                *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                S Offline
                S Offline
                signimage
                wrote on last edited by
                #7

                is there any way to access the from object from another class,usually i create the obj of form then using that obj we access the obj from form then why it give me the null reference erroe, and im sur that obj has obj referenc but it cannot send this reference to other class,(why)

                1 Reply Last reply
                0
                • C Colin Angus Mackay

                  That is because it is obviously not instantiated properly. A form is a class designed to store stuff that is displayed on the screen (within that form). If you are not going to display it on the screen then your design is screwed up and you should reconsider what you are doing.


                  *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                  S Offline
                  S Offline
                  signimage
                  wrote on last edited by
                  #8

                  Colin Angus Mackay wrote:

                  That is because it is obviously not instantiated properly

                  how to instantiat object properly?

                  C 1 Reply Last reply
                  0
                  • S signimage

                    Colin Angus Mackay wrote:

                    That is because it is obviously not instantiated properly

                    how to instantiat object properly?

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    SomeClass instance = new SomeClass();

                    or variations on that theme. See the class documentation in MSDN for details. Some objects use static Create methods instead.


                    *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups