Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid memory address or nil pointer dereference #63

Open
acook opened this issue May 29, 2021 · 2 comments
Open

invalid memory address or nil pointer dereference #63

acook opened this issue May 29, 2021 · 2 comments

Comments

@acook
Copy link

acook commented May 29, 2021

This happens when calling .InnerText when a page happens to be blank (such as a redirect).

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x6a496a]

goroutine 8 [running]:
github.com/antchfx/htmlquery.(*NodeNavigator).NodeType(0xc0000a08b8, 0x40f35b)
        /home/acook/go/pkg/mod/github.com/antchfx/htmlquery@v1.2.3/query.go:205 +0x2a
github.com/antchfx/xpath.axisPredicate.func1(0x7afca0, 0xc0000a08b8, 0x7f460faa35b8)
        /home/acook/go/pkg/mod/github.com/antchfx/xpath@v1.1.6/build.go:45 +0x51
github.com/antchfx/xpath.(*builder).processAxisNode.func1(0x7afca0, 0xc0000a08b8, 0xc0000adc10)
        /home/acook/go/pkg/mod/github.com/antchfx/xpath@v1.1.6/build.go:82 +0x46
github.com/antchfx/xpath.(*descendantQuery).Select.func1(0x71ffa0, 0xc00020a900)
        /home/acook/go/pkg/mod/github.com/antchfx/xpath@v1.1.6/query.go:252 +0x1e3
github.com/antchfx/xpath.(*descendantQuery).Select(0xc0000dc640, 0x7a89a0, 0xc0000ca740, 0x20, 0x705340)
        /home/acook/go/pkg/mod/github.com/antchfx/xpath@v1.1.6/query.go:284 +0x34
github.com/antchfx/xpath.(*NodeIterator).MoveNext(0xc0000ca740, 0xc0000ca740)
        /home/acook/go/pkg/mod/github.com/antchfx/xpath@v1.1.6/xpath.go:86 +0x49
github.com/antchfx/htmlquery.QuerySelector(0x0, 0xc0000ca700, 0xc0000ca700)
        /home/acook/go/pkg/mod/github.com/antchfx/htmlquery@v1.2.3/query.go:73 +0xe8
github.com/antchfx/htmlquery.Query(0x0, 0x744dbd, 0x7, 0x7a8e00, 0xc00020a810, 0x0)
        /home/acook/go/pkg/mod/github.com/antchfx/htmlquery@v1.2.3/query.go:67 +0x85
main.getElementFromURL(0xc0000b27d0, 0x4, 0x744dbd, 0x7, 0x1, 0x11)
        /home/acook/Source/Mein/Go/TuneBot/main.go:142 +0x75
@jerzybrzoska
Copy link

When calling this function with fmt.Println(getSnatch49xp())

func getSnatch49xp() string {
  var title string

  doc, err := htmlquery.LoadURL("https://en.wikipedia.org/wiki/List_of_Olympic_records_in_weightlifting")
  if err != nil {
      log.Fatal(err)
  }
    xp1 := "div[@id='mw-content-text']/div[1]/table[1]/tbody/tr[3]/td[2]"
    td := htmlquery.FindOne(doc, xp1)
    record = htmlquery.InnerText(td)
    
  return record
}

I get something similar that the OP brought up:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x67277c]

goroutine 1 [running]:
github.com/antchfx/htmlquery.InnerText.func1(0xc000410220?, 0xc000465830?)
	/home/jim/go/pkg/mod/github.com/antchfx/htmlquery@v1.2.5/query.go:143 +0x1c
github.com/antchfx/htmlquery.InnerText(0x6fa3e3?)
	/home/jim/go/pkg/mod/github.com/antchfx/htmlquery@v1.2.5/query.go:156 +0x6c
main.getSnatch49xp()
	/home/jim/CS/SoftwareDevelopment/MySoftware/Go/weightlifting-records/main.go:47 +0x7c
main.main()
	/home/jim/CS/SoftwareDevelopment/MySoftware/Go/weightlifting-records/main.go:57 +0x19

@zhengchun
Copy link
Contributor

@jerzybrzoska, checking your variable td is not nil before calling htmlquery.InnerText(td), like this if td != nil { record = htmlquery.InnerText(td) }

  1. your xpath express is not good, change it to //div[@id='mw-content-text']/div[1]/table[1]/tbody/tr[3]/td[2] . div[@id='mw-content-text']/div[1]/table[1]/tbody/tr[3]/td[2] will not found any matching nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants