Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang: runtime error: invalid memory address or nil pointer dereference

Tags:

go

I am new to golang and currently following this tutorial and source code here - http://golang.org/doc/articles/wiki/part2.go

After building this file, I am getting

calvin$ ./mywebwiki2 
2012/07/23 17:12:59 http: panic serving [::1]:58820: runtime error: invalid memory address or nil pointer dereference
/usr/local/go/src/pkg/net/http/server.go:576 (0x3f202)
    _func_003: buf.Write(debug.Stack())
/private/tmp/bindist454984655/go/src/pkg/runtime/proc.c:1443 (0x10c79)
/private/tmp/bindist454984655/go/src/pkg/runtime/runtime.c:128 (0x11745)
/private/tmp/bindist454984655/go/src/pkg/runtime/thread_darwin.c:418 (0x148b5)
/Users/calvin/work/gowiki/mywebwiki2.go:33 (0x2248)
    viewHandler: fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
/usr/local/go/src/pkg/net/http/server.go:690 (0x331ae)
    HandlerFunc.ServeHTTP: f(w, r)
/usr/local/go/src/pkg/net/http/server.go:926 (0x34030)
    (*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/local/go/src/pkg/net/http/server.go:656 (0x32fc1)
    (*conn).serve: handler.ServeHTTP(w, w.req)
/private/tmp/bindist454984655/go/src/pkg/runtime/proc.c:271 (0xed7f)
2012/07/23 17:12:59 http: panic serving [::1]:58821: runtime error: invalid memory address or nil pointer dereference

Any idea what I did wrong to be causing this apparent memory corruption?

like image 983
Calvin Cheng Avatar asked Jul 23 '12 09:07

Calvin Cheng


People also ask

How do I fix runtime error invalid memory address or nil pointer dereference?

Solution. To get rid of this error, you have to make sure that you are not following any nil pointers. In this case, you have to check if the error is nil, by using an if-else condition.

How do I dereference a pointer in Golang?

The * and & operators In Go a pointer is represented using the * (asterisk) character followed by the type of the stored value. In the zero function xPtr is a pointer to an int . * is also used to “dereference” pointer variables. Dereferencing a pointer gives us access to the value the pointer points to.

What is a nil pointer dereference?

Description. A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit. Extended Description. NULL pointer dereference issues can occur through a number of flaws, including race conditions, and simple programming omissions.

What is invalid memory address?

Occurs when a read or write instruction references memory that is logically or physically invalid. If present, represents the location and associated call stack from which the memory block containing the offending address was allocated.


2 Answers

There's an ignored err at line 36. The error probably says open .txt: no such file or directory if you tested in browser using URL http://localhost:8080/view/ or open foo.txt: no such file or directory if you tested in browser using URL http://localhost:8080/view/foo. In the later case there must be a file "foo.txt" in your working directory for this example code to work. After that the code seems to work for me locally.

Someone should probably fill an issue about the ignored error value.

like image 134
zzzz Avatar answered Sep 22 '22 14:09

zzzz


In the tutorial, you created the file TestPage previously. Which is the page you should be navigating to when you first build the server. In the tutorial, they have you navigate to view/test instead of view/TestPage as you should which is what creates the confusion.

like image 37
ScotterC Avatar answered Sep 21 '22 14:09

ScotterC