invalid memory address in Golang
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
add a comment |
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
add a comment |
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
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
pointers go
asked Nov 20 '18 at 19:22
Matty
2751412
2751412
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited Nov 20 '18 at 19:40
answered Nov 20 '18 at 19:31
nos
174k43317425
174k43317425
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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