# Quell NullPointerException errors when 'attachment' not present

**URL:** <https://discuss.elastic.co/t/quell-nullpointerexception-errors-when-attachment-not-present/8265>\
**Category:** Elasticsearch\
**Created:** [June 29, 2012, 4:55pm UTC](https://discuss.elastic.co/t/quell-nullpointerexception-errors-when-attachment-not-present/8265 "2012-06-29T16:55:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meltemi](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Meltemi](https://discuss.elastic.co/u/Meltemi)\
**Post date:** [June 29, 2012, 4:55pm UTC](https://discuss.elastic.co/t/quell-nullpointerexception-errors-when-attachment-not-present/8265/1 "2012-06-29T16:55:29Z")

</div>

Using Tire gem to interface with Elasticsearch from w/in a Rails  
application. FWIW- I'm new to both Tire & ES.

As I'm exploring Tire & ES I frequently make changes to the Index which I  
reload with:

$ rake environment tire:import CLASS=Article FORCE=true

which appears to do a bulk import of some sort. Anyway, things seem to be  
working fine and attachments are indexed (along with attributes from the  
model) but some of the records don't, yet, have attachments associated with  
them and the 'attachment' attribute in their instance currently returns  
'null'. Problem (at least according to the log it's a problem) is that when  
trying to index one of those records ES writes this in the log:

[2012-06-29 09:01:15,515][DEBUG][action.bulk] [Skunge the  
Laxidazian Troll] [articles][4] failed to execute bulk item (index) index  
{[articles][article][10], source[{"content":"this title is  
fluff","created\_at":"2012-06-27T22:02:43Z","filename":null,"id":10,"published\_on":"2012-06-27","title":"Test  
title","updated\_at":"2012-06-27T22:02:43Z","attachment":null}]}  
java.lang.NullPointerException

Is there something I could be returning (instead of a null value) when  
'filename' is not present in the instance to make ES ignore the indexing of  
the field or at least not complain about it?

appreciate any help!

#app/models/article.rb

class Article \< ActiveRecord::Base  
include Tire::Model::Search  
include Tire::Model::Callbacks

attr\_accessible :title, :content, :published\_on, :filename

mapping do  
indexes :id, :type =\>'integer'  
indexes :title  
indexes :content  
indexes :published\_on, :type =\> 'date'  
indexes :attachment, :type =\> 'attachment'  
end

def to\_indexed\_json  
to\_json(:methods =\> [:attachment])  
end

def attachment  
if filename.present?  
path\_to\_pdf = "/Volumes/Disk41402/Internal/IRRAD\_large/sample\_pdfs/#{filename}.pdf"  
Base64.encode64(open(path\_to\_pdf) { |pdf| pdf.read })  
end  
endend

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:22am UTC](https://discuss.elastic.co/t/quell-nullpointerexception-errors-when-attachment-not-present/8265/2 "2017-07-06T03:22:01Z")

</div>

Closed on July 6, 2017, 3:22am UTC


