{"id":5104,"date":"2014-03-30T18:50:13","date_gmt":"2014-03-30T18:50:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/lua-attempt-to-call-method-new-a-nil-value-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:50:13","modified_gmt":"2014-03-30T18:50:13","slug":"lua-attempt-to-call-method-new-a-nil-value-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/lua-attempt-to-call-method-new-a-nil-value-collection-of-common-programming-errors\/","title":{"rendered":"Lua &#8211; attempt to call method &#39;new&#39; (a nil value)-Collection of common programming errors"},"content":{"rendered":"<p>First of all, the semicolons at the end of lines are not necessary and probably a bad habit for writing Lua code. Secondly, I changed <code>require 'middleclass'<\/code> to <code>require 'middleclass.init'<\/code> in both files and removed <code>module(..., package.seeall)<\/code>. After that, the example code worked just fine on my machine with Lua 5.1.4.<\/p>\n<p><strong>main.lua<\/strong><\/p>\n<pre><code>require 'Person'\n\nlocal testPerson = Person:new(\"Sally\")\ntestPerson:speak()\n<\/code><\/pre>\n<p><strong>Person.lua<\/strong><\/p>\n<pre><code>require 'middleclass.init'\n\nPerson = class('Person')\n\nfunction Person:initialize(name)\n  self.name = name\n  print(\"INITIALIZE: \" .. self.name)\nend\n\nfunction Person:speak()\n  print('Hi, I am ' .. self.name ..'.')\nend\n<\/code><\/pre>\n<p>You may be including the <em>middleclass.lua<\/em> file directly. It is not setup to work that way. The intention is to include <em>middleclass\/init.lua<\/em>.<\/p>\n<p>If you use the two files exactly as shown above and layout your files as shown below this will work.<\/p>\n<pre><code>.\/main.lua\n.\/Person.lua\n.\/middleclass\/init.lua\n.\/middleclass\/middleclass.lua\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First of all, the semicolons at the end of lines are not necessary and probably a bad habit for writing Lua code. Secondly, I changed require &#8216;middleclass&#8217; to require &#8216;middleclass.init&#8217; in both files and removed module(&#8230;, package.seeall). After that, the example code worked just fine on my machine with Lua 5.1.4. main.lua require &#8216;Person&#8217; local [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5104","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5104"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5104\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}