Note that there are some explanatory texts on larger screens.

plurals
  1. POIronpython: Function works in CPython, mysterious null pointer exception in IronPython
    primarykey
    data
    text
    <p>I'm trying to do something that seems very simple, and falls within the range of standard python. The following function takes a collection of sets, and returns all of the items that are contained in two or more sets.</p> <p>To do this, while the collection of sets is not empty, it simply pops one set out of the collection, intersects it with the remaining sets, and updates a set of items that fall in one of these intersections.</p> <pre><code>def cross_intersections(sets): in_two = set() sets_copy = copy(sets) while sets_copy: comp = sets_copy.pop() for each in sets_copy: new = comp &amp; each print new, # Print statements to show that these references exist print in_two in_two |= new #This is where the error occurs in IronPython return in_two </code></pre> <p>Above is the function I'm using. To test it, in CPython, the following works:</p> <pre><code>&gt;&gt;&gt; a = set([1,2,3,4]) &gt;&gt;&gt; b = set([3,4,5,6]) &gt;&gt;&gt; c = set([2,4,6,8]) &gt;&gt;&gt; cross = cross_intersections([a,b,c]) set([2, 4]) set([]) set([4, 6]) set([2, 4]) set([3, 4]) set([2, 4, 6]) &gt;&gt;&gt; cross set([2, 3, 4, 6]) </code></pre> <p>However, when I try to use IronPython:</p> <pre><code>&gt;&gt;&gt; b = cross_intersections([a,b,c]) set([2, 4]) set([]) set([4, 6]) set([2, 4]) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "c:/path/to/code.py", line 10, in cross_intersections SystemError: Object reference not set to an instance of an object. </code></pre> <p>In the title I said this was a mysterious null pointer exception. I probably have no idea how .NET handles null pointers (I've never worked with a C-like language, and have only been using IronPython for a month or so), but if my understanding is correct, it occurs when you attempt to access some property of an object that points to <code>null</code>.</p> <p>In this case, the error occurs at line 10 of my function: <code>in_two |= new</code>. However, I've put <code>print</code> statements right before this line that (at least to me) indicate that neither of these objects point to <code>null</code>.</p> <p>Where am I going wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload