# Undefined method (XRC problem)

**URL:** <https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328>\
**Category:** wxRuby\
**Created:** [February 14, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328 "2009-02-14T16:06:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rustee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rustee](https://www.ruby-forum.com/u/rustee)\
**Post date:** [February 14, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328/1 "2009-02-14T16:06:20Z")

</div>

I’m trying to get my first small test wxRuby app up and running. I have  
created an XRC file using DialogBlocks and then used xrcise to produce  
the ruby code which loads it. I then did a bit of cutting and pasting to  
produce my own ruby code to add functionality. However I keep running  
into the same problem of ‘undefined method’. I’ve tried lots of  
different alternatives to get\_selection (ie, value, get\_value,  
get\_item\_data etc but with no luck).

I’m sure I’m doing something wrong that someone will find to be a basic  
error (hopefully!). Happy to post XRC file if necessary. Any help  
gratefully appreciated.

\_\_\_ The error message:\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

$ ruby xrctest.rb  
xrctest.rb:10:in `generatetext': undefined method`get\_selection’ for  
nil:NilClass (NoMethodError)  
from xrctest.rb:18:in `initialize' from /usr/lib/ruby/gems/1.8/gems/wxruby-1.9.10-x86_64-linux/lib/wx/classes/app.rb:16:in`call’  
from  
/usr/lib/ruby/gems/1.8/gems/wxruby-1.9.10-x86\_64-linux/lib/wx/classes/app.rb:16:in  
`process_event' from /usr/lib/ruby/gems/1.8/gems/wxruby-1.9.10-x86_64-linux/lib/wx/classes/app.rb:16:in`on\_run’  
from  
/usr/lib/ruby/gems/1.8/gems/wxruby-1.9.10-x86\_64-linux/lib/wx/classes/app.rb:16:in  
`main_loop' from /usr/lib/ruby/gems/1.8/gems/wxruby-1.9.10-x86_64-linux/lib/wx/classes/app.rb:16:in`run’  
from xrctest.rb:23

_**Xrcise produced this (myframe.rb):**_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

class FrameBaseClass \< Wx::Frame  
attr\_reader :id\_choice\_select, :id\_datectrl, :id\_button\_test,  
:id\_textctrl\_test, :id\_button\_close

def initialize(parent = nil)  
super()  
xml = Wx::XmlResource.get  
xml.flags = 2 # Wx::XRC\_NO\_SUBCLASSING  
xml.init\_all\_handlers  
xml.load(“testwindow.xrc”)  
xml.load\_frame\_subclass(self, parent, “ID\_WXFRAME”)

```
finder = lambda do | x |
  int_id = Wx::xrcid(x)
  begin
    Wx::Window.find_window_by_id(int_id, self) || int_id
    # Temporary hack to work around regression in 1.9.2; remove
    # begin/rescue clause in later versions
    rescue RuntimeError
    int_id
  end
end

@id_choice_select = finder.call("id_choice_select")
@id_datectrl = finder.call("id_datectrl")
@id_button_test = finder.call("id_button_test")
@id_textctrl_test = finder.call("id_textctrl_test")
@id_textctrl_test.extend(ClassTextCtrl)
@id_button_close = finder.call("id_button_close")
if self.class.method_defined? "on_init"
  self.on_init()
end

```

end  
end

_**And my own code (xrctest.rb):**_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

require ‘rubygems’  
require ‘wx’

# load in the generated code

require ‘myframe’

# Mix-in

module ClassTextCtrl  
def generatetext  
self.value = @id\_choice\_select.get\_selection  
end  
end

# Inherit from the generated base class and set up event handlers

class FrameBase \< FrameBaseClass  
def initialize  
super()  
evt\_button(id\_button\_test) { id\_textctrl\_test.generatetext }  
end  
end

# Run the class

Wx::App.run do  
FrameBase.new.show  
end

---

<div class="post-metadata">

**Author:** ![rustee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rustee](https://www.ruby-forum.com/u/rustee)\
**Post date:** [February 14, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328/2 "2009-02-14T20:38:07Z")

</div>

Hi Rooby N.,

@id\_choice\_select = finder.call(“id\_choice\_select”)

> 

It look like your xrc file lack or mispelled the name  
“id\_choice\_select”.

bio.

---

<div class="post-metadata">

**Author:** ![rustee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rustee](https://www.ruby-forum.com/u/rustee)\
**Post date:** [February 15, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328/3 "2009-02-15T19:53:33Z")

</div>

> > @id\_choice\_select = finder.call(“id\_choice\_select”)

> It look like your xrc file lack or mispelled the name  
> “id\_choice\_select”.

Fabio - thanks for your suggestion but I’m not sure that’s the case or  
indeed the problem here. Still no luck with this - any other ideas  
gratefully appreciated.

---

<div class="post-metadata">

**Author:** ![rustee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rustee](https://www.ruby-forum.com/u/rustee)\
**Post date:** [February 15, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328/4 "2009-02-15T21:34:22Z")

</div>

The problem is, two things that I can see. First, I don’t see where  
your  
actually mixing in the module ClassTextCtrl into the instance of  
id\_textctrl\_test.

You need to do something like this in your initialization function:

class \<\< @id\_textctrl\_test  
extend ClassTextCtrl  
end

Second, your trying to access an instance variable that is defined in  
the  
FrameBase class, from the TextCtrl class. Which is where your nil is  
coming  
from. My suggestion would be to do this in your module function:

def generatetext  
self.value = self.parent.id\_choice\_select.get\_selection  
end

This should solve your problem, and make it work properly.

---

<div class="post-metadata">

**Author:** ![rustee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rustee](https://www.ruby-forum.com/u/rustee)\
**Post date:** [February 15, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328/5 "2009-02-15T21:44:20Z")

</div>

Rooby N. wrote:

> Fabio - thanks for your suggestion but I’m not sure that’s the case or  
> indeed the problem here. Still no luck with this - any other ideas  
> gratefully appreciated.

It’s because @id\_choice\_select is an instance variable of the  
FrameBaseClass instance. When you are in the method generatetext, “self”  
is the TextCtrl, not the Frame.

If you were to rewrite the event handler as:

evt\_button(…) { id\_textctrl\_text.value = id\_choice\_select.selection }

it should work

a

---

<div class="post-metadata">

**Author:** ![rustee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rustee](https://www.ruby-forum.com/u/rustee)\
**Post date:** [February 15, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328/6 "2009-02-15T23:02:31Z")

</div>

Mario S. wrote:

> The problem is, two things that I can see. First, I don’t see where  
> your actually mixing in the module ClassTextCtrl into the instance of  
> id\_textctrl\_test.

Mario - the code that xrcise generates does this for you. If you define  
a “subclass” for a widget in the XRC editor, xrcise will generate code  
that does

@widget.extend SubClass

which is all you need; this is too much, I think:

> class \<\< @id\_textctrl\_test  
> extend ClassTextCtrl  
> end

cheers  
alex

---

<div class="post-metadata">

**Author:** ![rustee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@rustee](https://www.ruby-forum.com/u/rustee)\
**Post date:** [February 16, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/undefined-method-xrc-problem/160328/7 "2009-02-16T19:57:01Z")

</div>

Some great suggestions here guys, many thanks again for taking the time  
to come to the rescue. I’ve had a number of successes based on your  
suggestions and I’m now starting to pick up some speed in developing in  
wxRuby (and finally starting to have some fun in the process).
