invalid memory address in Golang












-3














I have checked several other answers and they gave me some good thoughts on how to troubleshoot this but I still can't figure it out.



    localID := generateGenericID("local") // type int64
localName := "local" // type string

// set them to pointers as I need them as type *int64 and *string
plocalID := &localID
plocalName := &localName

// create a pointer to a new github org
var org *github.Organization

// create a new general purpose org
o := Org{}

// Check for nil, per several other answers
if plocalName != nil {
o.Name = plocalName
org.Login = plocalName // ERROR
}

if plocalID != nil {
o.ID = plocalID
org.ID = plocalID
}

fmt.Println(*plocalName) // prints local


hunt.addOrganization(org) // takes a pointer to a github org
report.addReportOrganization(&o) // take a pointer to an org


The error message I am getting is:



panic: runtime error: invalid memory address or nil pointer 


It points to the line marked ERROR. The error makes no sense due to me checking for nil, so I am wondering if maybe I am some kind of pointer hell due to creating a variable that is a pointer and then assigning pointers to it.



Should I simply create the github org as a normal struct and then pass in &org to the hunt function. I tried that and got past the error but a ton of other code pukes so before I start to chase this down I figured I would ask if that even makes sense of if I am looking in the wrong spot. RTFM with links is always welcome.



Thanks!










share|improve this question



























    -3














    I have checked several other answers and they gave me some good thoughts on how to troubleshoot this but I still can't figure it out.



        localID := generateGenericID("local") // type int64
    localName := "local" // type string

    // set them to pointers as I need them as type *int64 and *string
    plocalID := &localID
    plocalName := &localName

    // create a pointer to a new github org
    var org *github.Organization

    // create a new general purpose org
    o := Org{}

    // Check for nil, per several other answers
    if plocalName != nil {
    o.Name = plocalName
    org.Login = plocalName // ERROR
    }

    if plocalID != nil {
    o.ID = plocalID
    org.ID = plocalID
    }

    fmt.Println(*plocalName) // prints local


    hunt.addOrganization(org) // takes a pointer to a github org
    report.addReportOrganization(&o) // take a pointer to an org


    The error message I am getting is:



    panic: runtime error: invalid memory address or nil pointer 


    It points to the line marked ERROR. The error makes no sense due to me checking for nil, so I am wondering if maybe I am some kind of pointer hell due to creating a variable that is a pointer and then assigning pointers to it.



    Should I simply create the github org as a normal struct and then pass in &org to the hunt function. I tried that and got past the error but a ton of other code pukes so before I start to chase this down I figured I would ask if that even makes sense of if I am looking in the wrong spot. RTFM with links is always welcome.



    Thanks!










    share|improve this question

























      -3












      -3








      -3







      I have checked several other answers and they gave me some good thoughts on how to troubleshoot this but I still can't figure it out.



          localID := generateGenericID("local") // type int64
      localName := "local" // type string

      // set them to pointers as I need them as type *int64 and *string
      plocalID := &localID
      plocalName := &localName

      // create a pointer to a new github org
      var org *github.Organization

      // create a new general purpose org
      o := Org{}

      // Check for nil, per several other answers
      if plocalName != nil {
      o.Name = plocalName
      org.Login = plocalName // ERROR
      }

      if plocalID != nil {
      o.ID = plocalID
      org.ID = plocalID
      }

      fmt.Println(*plocalName) // prints local


      hunt.addOrganization(org) // takes a pointer to a github org
      report.addReportOrganization(&o) // take a pointer to an org


      The error message I am getting is:



      panic: runtime error: invalid memory address or nil pointer 


      It points to the line marked ERROR. The error makes no sense due to me checking for nil, so I am wondering if maybe I am some kind of pointer hell due to creating a variable that is a pointer and then assigning pointers to it.



      Should I simply create the github org as a normal struct and then pass in &org to the hunt function. I tried that and got past the error but a ton of other code pukes so before I start to chase this down I figured I would ask if that even makes sense of if I am looking in the wrong spot. RTFM with links is always welcome.



      Thanks!










      share|improve this question













      I have checked several other answers and they gave me some good thoughts on how to troubleshoot this but I still can't figure it out.



          localID := generateGenericID("local") // type int64
      localName := "local" // type string

      // set them to pointers as I need them as type *int64 and *string
      plocalID := &localID
      plocalName := &localName

      // create a pointer to a new github org
      var org *github.Organization

      // create a new general purpose org
      o := Org{}

      // Check for nil, per several other answers
      if plocalName != nil {
      o.Name = plocalName
      org.Login = plocalName // ERROR
      }

      if plocalID != nil {
      o.ID = plocalID
      org.ID = plocalID
      }

      fmt.Println(*plocalName) // prints local


      hunt.addOrganization(org) // takes a pointer to a github org
      report.addReportOrganization(&o) // take a pointer to an org


      The error message I am getting is:



      panic: runtime error: invalid memory address or nil pointer 


      It points to the line marked ERROR. The error makes no sense due to me checking for nil, so I am wondering if maybe I am some kind of pointer hell due to creating a variable that is a pointer and then assigning pointers to it.



      Should I simply create the github org as a normal struct and then pass in &org to the hunt function. I tried that and got past the error but a ton of other code pukes so before I start to chase this down I figured I would ask if that even makes sense of if I am looking in the wrong spot. RTFM with links is always welcome.



      Thanks!







      pointers go






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 19:22









      Matty

      2751412




      2751412
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your code does essentially this:



          var org *github.Organization
          org.Login = plocalName


          org is a nil pointer, it doesn't point to a value, so org.Login tries to de-reference a nil pointer - hence the error. So don't declare org as a pointer type.



          You also have a lot of unnecessary code dealing with pointers, e.g.



          plocalID := &localID
          ..
          if plocalID != nil {
          o.ID = plocalID
          org.ID = plocalID
          }


          On that first line you created plocalID and initialized it with &localID. There is no way plocalID can then be a nil pointer, so it doesn't make sense to have the if plocalID != nil { check. You can just get rid of the plocalID and write your code as



          o.ID = &localID
          org.ID = &localID





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400113%2finvalid-memory-address-in-golang%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Your code does essentially this:



            var org *github.Organization
            org.Login = plocalName


            org is a nil pointer, it doesn't point to a value, so org.Login tries to de-reference a nil pointer - hence the error. So don't declare org as a pointer type.



            You also have a lot of unnecessary code dealing with pointers, e.g.



            plocalID := &localID
            ..
            if plocalID != nil {
            o.ID = plocalID
            org.ID = plocalID
            }


            On that first line you created plocalID and initialized it with &localID. There is no way plocalID can then be a nil pointer, so it doesn't make sense to have the if plocalID != nil { check. You can just get rid of the plocalID and write your code as



            o.ID = &localID
            org.ID = &localID





            share|improve this answer




























              2














              Your code does essentially this:



              var org *github.Organization
              org.Login = plocalName


              org is a nil pointer, it doesn't point to a value, so org.Login tries to de-reference a nil pointer - hence the error. So don't declare org as a pointer type.



              You also have a lot of unnecessary code dealing with pointers, e.g.



              plocalID := &localID
              ..
              if plocalID != nil {
              o.ID = plocalID
              org.ID = plocalID
              }


              On that first line you created plocalID and initialized it with &localID. There is no way plocalID can then be a nil pointer, so it doesn't make sense to have the if plocalID != nil { check. You can just get rid of the plocalID and write your code as



              o.ID = &localID
              org.ID = &localID





              share|improve this answer


























                2












                2








                2






                Your code does essentially this:



                var org *github.Organization
                org.Login = plocalName


                org is a nil pointer, it doesn't point to a value, so org.Login tries to de-reference a nil pointer - hence the error. So don't declare org as a pointer type.



                You also have a lot of unnecessary code dealing with pointers, e.g.



                plocalID := &localID
                ..
                if plocalID != nil {
                o.ID = plocalID
                org.ID = plocalID
                }


                On that first line you created plocalID and initialized it with &localID. There is no way plocalID can then be a nil pointer, so it doesn't make sense to have the if plocalID != nil { check. You can just get rid of the plocalID and write your code as



                o.ID = &localID
                org.ID = &localID





                share|improve this answer














                Your code does essentially this:



                var org *github.Organization
                org.Login = plocalName


                org is a nil pointer, it doesn't point to a value, so org.Login tries to de-reference a nil pointer - hence the error. So don't declare org as a pointer type.



                You also have a lot of unnecessary code dealing with pointers, e.g.



                plocalID := &localID
                ..
                if plocalID != nil {
                o.ID = plocalID
                org.ID = plocalID
                }


                On that first line you created plocalID and initialized it with &localID. There is no way plocalID can then be a nil pointer, so it doesn't make sense to have the if plocalID != nil { check. You can just get rid of the plocalID and write your code as



                o.ID = &localID
                org.ID = &localID






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 20 '18 at 19:40

























                answered Nov 20 '18 at 19:31









                nos

                174k43317425




                174k43317425






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400113%2finvalid-memory-address-in-golang%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga