Uncaught TypeError: Cannot read property 'parentNode' of null
Closed this issue · 3 comments
I just ran into this while I was messing around with another package. I was creating a view/tab and almost immediately destroying it. Probably not something that you should do, but... This doesn't seem to happen unless the tab I was destroying was the only tab.
Could some if guards be added for this?
Atom Version: 1.6.0-beta4
System: Ubuntu 15.10
Thrown From: pinned-tabs package, v0.1.12
Stack Trace
Uncaught TypeError: Cannot read property 'parentNode' of null
At /home/sami/.atom/packages/pinned-tabs/lib/pinned-tabs.coffee:186
TypeError: Cannot read property 'parentNode' of null
at Object.module.exports.PinnedTabs.getTabInformation (/home/sami/.atom/packages/pinned-tabs/lib/pinned-tabs.coffee:186:19)
at /home/sami/.atom/packages/pinned-tabs/lib/pinned-tabs.coffee:123:26
These seem to do the trick, but may not be the smartest way to solve this:
# Get information about a tab
getTabInformation: (e) ->
+ return unless e
# Get related nodes
tabbar = e.parentNode
# Method that pins/unpins a tab given its element.
pin: (e) ->
# Get information about the tab
r = @getTabInformation e
+ return unless r
# Calculate the new index for this tab based
# on the amount of pinned tabs within this pane.
if r.isPinned
# Observer panes
observers: ->
self = this # This object has to be stored in self because the callback function will create its own 'this'
atom.workspace.onDidAddPaneItem (event) ->
setTimeout (->
# Get information about the tab
r = self.getTabInformation document.querySelector('.tab-bar .tab.active')
+ return unless r
# Move it if necessary
if r.pinIndex > r.curIndex
Hmm, that does indeed not look like the nicest way to solve the problem... Can you explain why you also added return unless r
to the pin method? Given you're description I would say it isn't necessary. And in that case, only the event(s) in observers need the check, which would make the solution less terrible. 😓
You're right, the return unless r
in pin()
doesn't seem to be necessary, I added it simply because getTabInformation()
can now return null
. I guess it would be best to do something like:
# Observer panes
observers: ->
self = this # This object has to be stored in self because the callback function will create its own 'this'
atom.workspace.onDidAddPaneItem (event) ->
setTimeout (->
# Get information about the tab
- r = self.getTabInformation document.querySelector('.tab-bar .tab.active')
+ return unless e = document.querySelector('.tab-bar .tab.active')
+ r = self.getTabInformation e
The above change alone is enough to avoid the error.
I changed it and ran some tests, it seems like the problem is solved (I also published it to apm)! Thanks for the help 😄