Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6706385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:30:52+00:00 2026-05-26T07:30:52+00:00

A very strange error, showing an object is nil. the code in subject is

  • 0

A very strange error, showing an object is nil.

the code in subject is

while pbs:HasNext() do
    local char = self.DecodeCharacter(pbs)
    ...

One would think, that if pbs:HasNext() is true, it means that, pbs is not nil, whatsoever.

However, the print(pbs) – the first line of HTMLEntityCodec:DecodeCharacter prints nil

function HTMLEntityCodec:DecodeCharacter(pbs)
    print(pbs)
    ...

The entire file dumped below, it was stripped from 1800+ lines to 110 so it can be clear for SO users to get he context. But that stripping took away all logic from the code, so do not get confused by that.

#!/usr/bin/env lua

function Inherits( baseClass )
    local new_class = {}
    local class_mt = { __index = new_class }

    function new_class:create()
        local newinst = {}
        setmetatable( newinst, class_mt )
        return newinst
    end

    if baseClass then
        setmetatable( new_class, { __index = baseClass } )
    end

    return new_class
end


-------------------------------------------
-- PushbackString
-------------------------------------------
PushbackString = Inherits({})

function PushbackString:Init(input)
    self.input = input
    self.pushback = nil
    self.temp = nil
    self.index = 0 
    self.mark = 0
end

-- Mark the current index, so the client can reset() to it if need be.        
function PushbackString:HasNext()
    return true
end

function PushbackString:Mark ()
    self.temp = self.pushback
    self.mark = self.index
end


BaseCodec = Inherits({}) 

function BaseCodec:Decode(input)
    local buff = ''    
    local pbs = PushbackString:create()

    pbs:Init(input)

    while pbs:HasNext() do
        local char = self.DecodeCharacter(pbs)
        if char ~= nil then
            buff = buff .. char
        else
            buff = buff .. pbs:Next()
        end
    end
    return buff
end


HTMLEntityCodec = Inherits(BaseCodec)
-- HTMLEntityCodec.classname = ('HTMLEntityCodec')

function HTMLEntityCodec:DecodeCharacter(pbs)
    print(pbs)
    pbs:Mark()    
end

DefaultEncoder = Inherits({})

function DefaultEncoder:Init(codecs)
    self.html_codec = HTMLEntityCodec:create()
end

function DefaultEncoder:TestInput(input , strict)
    print ("\n----------------8<----------------8<----------------\n")
    print ("Input:\t" .. input)
    -- default value
    if strict == nil then strict = true end

    -- nothing to do
    if input == nil then return nil end

    local working = input
    local codecs_found = {}
    local found_count = 0
    local clean = false

    while not clean do
        clean = true
        old = working
        working = self.html_codec:Decode( working )
        if old ~= working then
            print ("Warning:\tINTRUSION DETECTED")
        end
    end

    print ("Output:\t".. working)
    return working
end


local default_encoder = DefaultEncoder:create()
default_encoder:Init()
default_encoder:TestInput("%25", true)

----------8<-----------8<--------------8<----------------

END OF FILE

Console Output:

tzury@1005:~/devel/lua$ lua problem.lua 

----------------8<----------------8<----------------

Input:  %25
nil
lua: problem.lua:70: attempt to index local 'pbs' (a nil value)
stack traceback:
        problem.lua:70: in function 'DecodeCharacter'
        problem.lua:54: in function 'Decode'
        problem.lua:96: in function 'TestInput'
        problem.lua:109: in main chunk
        [C]: ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:30:52+00:00Added an answer on May 26, 2026 at 7:30 am

    In your code, the crash happens on this line:

    local char = self.DecodeCharacter(pbs)
    

    The problem is that you are calling DecodeCharacter with incorrect number of arguments.

    Solution: call it like this (notice the colon):

    local char = self:DecodeCharacter(pbs)
    

    Explanation:

    When you define functions in Lua using the colon (:), you are using a syntax sugar which hides an implicit first argument named self. Definitions like:

    function HTMLEntityCodec:DecodeCharacter(pbs) ... end
    

    Are actually ‘translated’ to this:

    HTMLEntityCodec.DecodeCharacter = function (self, pbs) ... end
    

    When you call the function, you either need to pass the self argument yourself, or use the colon call to supply it automatically. In your code (self.DecodeCharacter(pbs)), you are passing pbs which ends up as self in HTMLEntityCodec.DecodeCharacter, and pbs ends up being nil. Both following calls are equivalent and should solve the issue:

    local char = self.DecodeCharacter(self, pbs)
    local char = self:DecodeCharacter(pbs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A very strange error: if I add some specific code to my project, any
The story began with a very strange error while I was running my script
there's very strange error with my code.. In fact, there's no errors at all,
I'm having issues with a very strange error in some code I wrote. The
Using the standard add record code I am getting a very strange error when
I have a very strange error in my Qt project. Here is the code,
I'm getting a very strange error with preg_replace. I'm using the following code: $text=preg_replace('/(\s)?'.preg_quote($f).'(\s)?/','<a
I have a very strange error using NSMutableArray in cocos2d/xcode In my code, I
When compiling code which uses code contracts, I have a very strange error I
I am facing a very strange issue. I get an error Object Reference Not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.