[1.1.0] Jython-2.7-rc3: Subclassing Region: with __init__ the java level constructors not used with super()__init__(), leaving Region "empty" --- workaround

Asked by forrest

********* workaround

setup a subclass of Region this way:

class MyRegion(Region):
    def __init__(self, x=1, y=1, w=1, h=1):
        self.setRect(x, y, w, h)

-----------------------------------------------------------

Installed nightly sikulixsetup-1.1.0-20150427.231036-54-forsetup, and my old code is breaking when I try to highlight a class that extends Region. The code worked fine with Sikuli from Dec 2014 and recently with sikulixsetup-1.1.0-20150417

Here is the simple code:
# begin code ===============================
import org.sikuli.basics.SikulixForJython
from sikuli import *

def highlight_flash(rejun, cycles=1, lightsec=1, darksec=0.25):
    if rejun != None:
        for i in range(cycles):
            rejun.highlight(lightsec)
            wait(darksec)

class SLRegion(Region):
    def __init__(self, x=1, y=1, w=1, h=1):
        super(SLRegion, self).__init__(x, y, w, h)

rejun = Region(100,100,100,100)
highlight_flash(rejun, 3)

rejun = SLRegion(100,100,100,100)
highlight_flash(rejun, 3)
# ============================================= end.

The first call to highlight_flash(...) works fine - flashes 100x100 box 3 times.
The second call fails with Null Pointer...

# begin Sikuli_IDE Log Message Window Output ==============

[log] highlight R[100,100 100x100]@S(0)[0,0 1280x1024] E:Y, T:3.0 for 1.0 secs

[log] highlight R[100,100 100x100]@S(0)[0,0 1280x1024] E:Y, T:3.0 for 1.0 secs

[log] highlight R[100,100 100x100]@S(0)[0,0 1280x1024] E:Y, T:3.0 for 1.0 secs

[log] highlight R[0,0 0x0]@Screen null E:Y, T:3.0 for 1.0 secs
[error] script [ deletelater ] stopped with error in line 18
[error] java.lang.NullPointerException ( java.lang.NullPointerException )
[error] --- Traceback --- error source first line: module ( function ) statement 7: main ( highlight_flash ) rejun.highlight(lightsec)
[error] --- Traceback --- end --------------
# end Sikuli_IDE Log Message Window Output ==============

and here is what it tells me when I run the same code in Eclipse...

# begin Eclipse Luna Error Output ==============
console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
pydev debugger: starting (pid: 10232)
[log] highlight R[100,100 100x100]@S(0)[0,0 1280x1024] E:Y, T:3.0 for 1.0 secs
[log] highlight R[100,100 100x100]@S(0)[0,0 1280x1024] E:Y, T:3.0 for 1.0 secs
[log] highlight R[100,100 100x100]@S(0)[0,0 1280x1024] E:Y, T:3.0 for 1.0 secs
[log] highlight R[0,0 0x0]@Screen null E:Y, T:3.0 for 1.0 secs
Traceback (most recent call last):
  File "C:\usrbin\eclipse\plugins\org.python.pydev_4.0.0.201504132356\pysrc\pydevd.py", line 2278, in <module>
    globals = debugger.run(setup['file'], None, None)
  File "C:\usrbin\eclipse\plugins\org.python.pydev_4.0.0.201504132356\pysrc\pydevd.py", line 1704, in run
    pydev_imports.execfile(file, globals, locals) # execute the script
  File "C:\sl\bin\sikuli\deletelater.sikuli\deletelater.py", line 18, in <module>
    highlight_flash(rejun, 3)
  File "C:\sl\bin\sikuli\deletelater.sikuli\deletelater.py", line 7, in highlight_flash
    rejun.highlight(lightsec)
 at org.sikuli.util.ScreenHighlighter.captureScreen(ScreenHighlighter.java:205)
 at org.sikuli.util.ScreenHighlighter.highlight(ScreenHighlighter.java:182)
 at org.sikuli.util.ScreenHighlighter.highlight(ScreenHighlighter.java:192)
 at org.sikuli.script.Region.highlight(Region.java:2015)
 at org.sikuli.script.Region.highlight(Region.java:2043)
 at org.sikuli.script.Region.highlight(Region.java:2027)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)

java.lang.NullPointerException: java.lang.NullPointerException
# end Eclipse Luna Error Output ==============

Question information

Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

I do not know wether this is a Jython bug or correct behaviour.

The Jython super class Region does not have any __init__.
So in previous Jython versions the applicable constructors in the Java super class Region where used in case
(up to

Now with the latest Jython 2.7-rc3 (is bundled with 1.1.0 now) in the above case, the Java constructors are no longer used in the above case and hence in the end you get an "empty" Region
R[0,0 0x0]@Screen null E:Y, T:3.0

... which then leads to the NPE with the Screen null with highlight()

I do not have a solution for that (besides going back to Jython 2.7b4, what I do not want to do though).

a workaround:
the Java constructors set the coordinates, dimension and screen, so in your case this works:

def __init__(self, x=1, y=1, w=1, h=1):
    self.setRect(x, y, w, h)

Revision history for this message
forrest (fjohnston) said :
#2

Yes, that worked for me.
Jython adds another "fun" dimension to the challenge of SWD...

Thank You for your attention to this. Much appreciated.

-----Original Message-----
From: <email address hidden> [mailto:<email address hidden>] On Behalf Of RaiMan
Sent: Thursday, April 30, 2015 12:57 AM
To: Forrest Johnston
Subject: Re: [Question #266141]: [1.1.0] Jython-2.7-rc3: Extending Region breaks when I move to new Sikuli

Your question #266141 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/266141

    Status: Open => Answered

RaiMan proposed the following answer:
I do not know wether this is a Jython bug or correct behaviour.

The Jython super class Region does not have any __init__.
So in previous Jython versions the applicable constructors in the Java super class Region where used in case (up to

Now with the latest Jython 2.7-rc3 (is bundled with 1.1.0 now) in the above case, the Java constructors are no longer used in the above case and hence in the end you get an "empty" Region
R[0,0 0x0]@Screen null E:Y, T:3.0

... which then leads to the NPE with the Screen null with highlight()

I do not have a solution for that (besides going back to Jython 2.7b4, what I do not want to do though).

a workaround:
the Java constructors set the coordinates, dimension and screen, so in your case this works:

def __init__(self, x=1, y=1, w=1, h=1):
    self.setRect(x, y, w, h)

--
If this answers your question, please go to the following page to let us know that it is solved:
https://answers.launchpad.net/sikuli/+question/266141/+confirm?answer_id=0

If you still need help, you can reply to this email or go to the following page to enter your feedback:
https://answers.launchpad.net/sikuli/+question/266141

You received this question notification because you asked the question.

Revision history for this message
forrest (fjohnston) said :
#3

Thanks RaiMan, that solved my question.

Revision history for this message
Ryan (ryan-pisano) said :
#4

RaiMan fixed this in 1.1.0 actually (as per my request). If you subclass Region you need to include self.init(ptn) in the __init__. The ptn should be whatever the incoming variable to create the region is - the image path, for instance. The point of the fix is to get around the fact that there is no __init__ as mentioned above

This should probably get added to the docs at some point too. I get null regions if I don't add this as well.