xpath icon indicating copy to clipboard operation
xpath copied to clipboard

invalid memory address or nil pointer dereference

Open acook opened this issue 3 years ago • 2 comments

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/[email protected]/query.go:205 +0x2a
github.com/antchfx/xpath.axisPredicate.func1(0x7afca0, 0xc0000a08b8, 0x7f460faa35b8)
        /home/acook/go/pkg/mod/github.com/antchfx/[email protected]/build.go:45 +0x51
github.com/antchfx/xpath.(*builder).processAxisNode.func1(0x7afca0, 0xc0000a08b8, 0xc0000adc10)
        /home/acook/go/pkg/mod/github.com/antchfx/[email protected]/build.go:82 +0x46
github.com/antchfx/xpath.(*descendantQuery).Select.func1(0x71ffa0, 0xc00020a900)
        /home/acook/go/pkg/mod/github.com/antchfx/[email protected]/query.go:252 +0x1e3
github.com/antchfx/xpath.(*descendantQuery).Select(0xc0000dc640, 0x7a89a0, 0xc0000ca740, 0x20, 0x705340)
        /home/acook/go/pkg/mod/github.com/antchfx/[email protected]/query.go:284 +0x34
github.com/antchfx/xpath.(*NodeIterator).MoveNext(0xc0000ca740, 0xc0000ca740)
        /home/acook/go/pkg/mod/github.com/antchfx/[email protected]/xpath.go:86 +0x49
github.com/antchfx/htmlquery.QuerySelector(0x0, 0xc0000ca700, 0xc0000ca700)
        /home/acook/go/pkg/mod/github.com/antchfx/[email protected]/query.go:73 +0xe8
github.com/antchfx/htmlquery.Query(0x0, 0x744dbd, 0x7, 0x7a8e00, 0xc00020a810, 0x0)
        /home/acook/go/pkg/mod/github.com/antchfx/[email protected]/query.go:67 +0x85
main.getElementFromURL(0xc0000b27d0, 0x4, 0x744dbd, 0x7, 0x1, 0x11)
        /home/acook/Source/Mein/Go/TuneBot/main.go:142 +0x75

acook avatar May 29 '21 02:05 acook

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/[email protected]/query.go:143 +0x1c
github.com/antchfx/htmlquery.InnerText(0x6fa3e3?)
	/home/jim/go/pkg/mod/github.com/antchfx/[email protected]/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

jerzybrzoska avatar Jun 17 '22 08:06 jerzybrzoska

@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.

zhengchun avatar Jun 17 '22 12:06 zhengchun