Ruby:类型错误:没有将nil隐式转换为String(TypeError)

时间:2014-02-17 19:39:35

标签: ruby xml string typeerror null

我正在尝试解析一个XML文件,看起来我遇到了类型错误,我真的不明白它为什么抱怨类型,我相信它在以下记录中打破了:

  <row Id="30" PostTypeId="2" ParentId="25" CreationDate="2008-08-01T12:22:40.797" Score="7" Body="&lt;p&gt;I've had no trouble using the BSD sockets API in C++, in GNU/Linux. Here's the sample program I used:&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;pre&gt;&lt;code&gt;#include &amp;lt;sys/socket.h&amp;gt;&lt;br&gt;&lt;br&gt;int&lt;br&gt;main()&lt;br&gt;{&lt;br&gt;    return AF_INET;&lt;br&gt;}&lt;br&gt;&lt;/code&gt;&lt;/pre&gt;&#xD;&#xA;&#xD;&#xA;&lt;p&gt;So my take on this is that z/OS is probably the complicating factor here, however, because I've never used z/OS before, much less programmed in it, I can't say this definitively. :-P&lt;/p&gt;" OwnerUserId="13" LastActivityDate="2008-08-01T12:22:40.797" CommentCount="1" />

这是类型错误:

load.rb:105:in `parse': no implicit conversion of nil into String (TypeError)

在我的脚本中,这是它抱怨的行

DateTime.parse(attributes['LastEditDate']).to_time.strftime("%F %T.%N"),
DateTime.parse(attributes['LastActivityDate']).to_time.strftime("%F %T.%N"),
attributes['Title'] == nil ? '' : attributes['Title'], 

以下是我的方法的样子:

  def on_start_element(element, attributes)
    if element == 'row'
      @post_st.execute(attributes['Id'], attributes['PostTypeId'], attributes['AcceptedAnswerId'], attributes['ParentId'], attributes['Score'], attributes['ViewCount'], 
        attributes['Body'], attributes['OwnerUserId'] == nil ? -1 : attributes['OwnerUserId'], attributes['LastEditorUserId'], attributes['LastEditorDisplayName'], 
        DateTime.parse(attributes['LastEditDate']).to_time.strftime("%F %T.%N"), DateTime.parse(attributes['LastActivityDate']).to_time.strftime("%F %T.%N"), attributes['Title'] == nil ? '' : attributes['Title'], 
        attributes['AnswerCount'] == nil ? 0 : attributes['AnswerCount'], attributes['CommentCount'] == nil ? 0 : attributes['CommentCount'], 
        attributes['FavoriteCount'] == nil ? 0 : attributes['FavoriteCount'], DateTime.parse(attributes['CreationDate']).to_time.strftime("%F %T.%N"))
      post_id = attributes['Id']

我相信同样的代码设法解析以前的记录没有问题,因为当我查看我用于存储数据的mysql dataabase时,有这条记录:

  <row Id="27" PostTypeId="2" ParentId="11" CreationDate="2008-08-01T12:17:19.357" Score="13" Body="&lt;p&gt;@jeff&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;p&gt;IMHO yours seems a little long.  However it does seem a little more robust with support for &quot;yesterday&quot; and &quot;years&quot;.  But in my experience when this is used the person is most likely to view the content in the first 30 days.  It is only the really hardcore people that come after that.  So that is why I usually elect to keep this short and simple.&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;p&gt;This is the method I am currently using on one of my websites.  This only returns a relative day, hour, time.  And then the user has to slap on &quot;ago&quot; in the output.&lt;/p&gt;&#xD;&#xA;&#xD;&#xA;&lt;pre&gt;&lt;code&gt;public static string ToLongString(this TimeSpan time)&lt;br&gt;{&lt;br&gt;    string output = String.Empty;&lt;br&gt;&lt;br&gt;    if (time.Days &amp;gt; 0)&lt;br&gt;        output += time.Days + &quot; days &quot;;&lt;br&gt;&lt;br&gt;    if ((time.Days == 0 || time.Days == 1) &amp;amp;&amp;amp; time.Hours &amp;gt; 0)&lt;br&gt;        output += time.Hours + &quot; hr &quot;;&lt;br&gt;&lt;br&gt;    if (time.Days == 0 &amp;amp;&amp;amp; time.Minutes &amp;gt; 0)&lt;br&gt;        output += time.Minutes + &quot; min &quot;;&lt;br&gt;&lt;br&gt;    if (output.Length == 0)&lt;br&gt;        output += time.Seconds + &quot; sec&quot;;&lt;br&gt;&lt;br&gt;    return output.Trim();&lt;br&gt;}&lt;br&gt;&lt;/code&gt;&lt;/pre&gt;" OwnerUserId="17" LastEditorUserId="17" LastEditorDisplayName="Nick Berardi" LastEditDate="2008-08-01T13:16:49.127" LastActivityDate="2008-08-01T13:16:49.127" CommentCount="1" CommunityOwnedDate="2009-09-04T13:15:59.820" />

0 个答案:

没有答案