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

NoMethodError: undefined method unpack for nil:NilClass #79

Closed
bianjp opened this issue Aug 16, 2016 · 4 comments
Closed

NoMethodError: undefined method unpack for nil:NilClass #79

bianjp opened this issue Aug 16, 2016 · 4 comments
Assignees

Comments

@bianjp
Copy link

bianjp commented Aug 16, 2016

I have a dbf file (09_02_23-nqhq.dbf.zip) which has 8687 rows, but record_count returns 8733. And maybe it's the cause that many methods failed.

filepath = '09_02_23-nqhq.dbf'
table = DBF::Table.new(filepath, nil, 'gbk')

table.record_count # works, returns 8733
table.count # error
table.each {|record| }# error
table.find 8686 # works
table.find 8687 # error

All errors are like the following:

NoMethodError: undefined method `unpack' for nil:NilClass
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.5/lib/dbf/table.rb:282:in `deleted_record?'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.5/lib/dbf/table.rb:124:in `record'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.5/lib/dbf/table.rb:113:in `block in each'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.5/lib/dbf/table.rb:113:in `times'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.5/lib/dbf/table.rb:113:in `each'
    from (irb):18

Not sure whether the dbf file is corrupted, but reading it with Python's dbfread has no problem.

@infused infused self-assigned this Aug 19, 2016
@infused
Copy link
Owner

infused commented Aug 19, 2016

The record count in the header of the file is incorrectly set to 8733. Possibly it was not updated correctly the last time the file was written to. I've just released version 3.0.6 which fixes the error when trying to access the non-existent records.

Please confirm if 3.0.6 works for you.

@bianjp
Copy link
Author

bianjp commented Aug 19, 2016

Thanks. The former error disappeared but another error arises:

a.each {}
TypeError: no implicit conversion of nil into String
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/record.rb:11:in `initialize'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/record.rb:11:in `new'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/record.rb:11:in `initialize'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/table.rb:125:in `new'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/table.rb:125:in `record'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/table.rb:113:in `block in each'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/table.rb:113:in `times'
    from /home/vanitas/.gem/ruby/2.3/gems/dbf-3.0.6/lib/dbf/table.rb:113:in `each'
    from (irb):11

Is that possible to address the incorrectness of the record count and correct it automatically?

In addition, .find with an index greater than record count causes the same error. Maybe it's better to check the index and return nil or raise IndexError if the index >= record count?

@infused
Copy link
Owner

infused commented Aug 25, 2016

Sorry about that. I was traveling and rushed a bad patch. I've had to yank version 3.0.6 from rubygems due to the bad patch. Please give 3.0.7 a try now and let me know if that fixes the issue.

@bianjp
Copy link
Author

bianjp commented Aug 25, 2016

It works. Thanks~
Since the element may be nil, we have to iterate it carefully.
Remove nil elements (using table.reject(&:nil?)) before iterating may be a good idea.

But I still think that correct the record count automatically would be the best solution.

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

2 participants