nullpointer exception, tutorial sketch won't run

edited July 2017 in Python Mode

Pretty much as title says, trying to run the object tutorial for python mode, but even after correcting indentation the sketch won't run and gives a nullpointer. EDIT removed picture, see sketch code & error message below.

Hopefully someone can tell me what's going on and how to fix it. Thanks!

Tagged:

Answers

  • edited July 2017

    Avoid code screenshots! Read below how to post them in this forum via markup: L-)
    https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    ... even after correcting indentation...

    Any1 can see your code isn't properly indented at that code screenshot! 8-|
    Also, where is the rest of its class block? :-\"

  • edited July 2017

    Oh my bad, thought code wouldn't be necessary. Also, by properly indented I mean there're no more errors which say 'this line probably needs to be indented'. At any rate here's the code:

    # Even though there are multiple objects, we still only need one class.
    # No matter how many cookies we make, only one cookie cutter is needed.
    class Car(object):
    # The Constructor is defined with arguments.
    
        def __init__(self, c, xpos, ypos, xspeed):
            self.c = c
            self.xpos = xpos
            self.ypos = ypos
            self.xspeed = xspeed
    
            def display(self):
                stroke(0)
            fill(self.c)
            rectMode(CENTER)
            rect(self.xpos, self.ypos, 20, 10)
    
            def drive(self):
                self.xpos = self.xpos + self.xspeed
                if self.xpos > width:
                    self.xpos = 0
    
    myCar1 = Car(color(255, 0, 0), 0, 100, 2)
    myCar2 = Car(color(0, 255, 255), 0, 10, 1)
    
    def setup():
        size(200, 200)
    
    def draw():
        background(255)
        myCar1.drive()
        myCar1.display()
        myCar2.drive()
        myCar2.display()
    

    And here's the error message:

    processing.app.SketchException: java.lang.NullPointerException
        at processing.core.PApplet.fill(PApplet.java:14465)
        at jycessing.PAppletJythonDriver.fill(PAppletJythonDriver.java:816)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)
        at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)
        at org.python.core.PyObject.__call__(PyObject.java:478)
        at org.python.core.PyObject.__call__(PyObject.java:482)
        at org.python.core.PyMethod.__call__(PyMethod.java:141)
        at org.python.pycode._pyx44.__init__$2(python.pyde:18)
        at org.python.pycode._pyx44.call_function(python.pyde)
        at org.python.core.PyTableCode.call(PyTableCode.java:167)
        at org.python.core.PyBaseCode.call(PyBaseCode.java:307)
        at org.python.core.PyBaseCode.call(PyBaseCode.java:198)
        at org.python.core.PyFunction.__call__(PyFunction.java:482)
        at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237)
        at org.python.core.PyMethod.__call__(PyMethod.java:228)
        at org.python.core.PyMethod.__call__(PyMethod.java:223)
        at org.python.core.Deriveds.dispatch__init__(Deriveds.java:19)
        at org.python.core.PyObjectDerived.dispatch__init__(PyObjectDerived.java:1112)
        at org.python.core.PyType.type___call__(PyType.java:1713)
        at org.python.core.PyType.__call__(PyType.java:1696)
        at org.python.core.PyObject.__call__(PyObject.java:515)
        at org.python.core.PyObject.__call__(PyObject.java:521)
        at org.python.pycode._pyx44.f$0(python.pyde:1)
        at org.python.pycode._pyx44.call_function(python.pyde)
        at org.python.core.PyTableCode.call(PyTableCode.java:167)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1386)
        at org.python.core.Py.exec(Py.java:1430)
        at org.python.pycode._pyx43.f$0(C:\Users\myName\AppData\Local\Temp\python2118232332312181385\python.pyde:89)
        at org.python.pycode._pyx43.call_function(C:\Users\myName\AppData\Local\Temp\python2118232332312181385\python.pyde)
        at org.python.core.PyTableCode.call(PyTableCode.java:167)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1386)
        at org.python.core.Py.exec(Py.java:1430)
        at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
        at jycessing.PAppletJythonDriver.processSketch(PAppletJythonDriver.java:194)
        at jycessing.PAppletJythonDriver.findSketchMethods(PAppletJythonDriver.java:446)
        at jycessing.Runner.runSketchBlocking(Runner.java:385)
        at jycessing.mode.run.SketchRunner$3.run(SketchRunner.java:118)
        at java.lang.Thread.run(Thread.java:748)
    
        at jycessing.mode.run.SketchRunner.convertPythonSketchError(SketchRunner.java:230)
        at jycessing.mode.run.SketchRunner.access$4(SketchRunner.java:227)
        at jycessing.mode.run.SketchRunner$3.run(SketchRunner.java:122)
        at java.lang.Thread.run(Thread.java:748)
    
  • edited July 2017 Answer ✓

    In method display() below, clearly only the stroke(0) statement is correctly indented in relation to its method. The other 3 statements are outside method display(): #-o

    def display(self):
        stroke(0)
    fill(self.c)
    rectMode(CENTER)
    rect(self.xpos, self.ypos, 20, 10)
    

    Solution, indent the other 3 statements as well: ;;)

    def display(self):
        stroke(0)
        fill(self.c)
        rectMode(CENTER)
        rect(self.xpos, self.ypos, 20, 10)
    
  • edited July 2017 Answer ✓

    2nd bug, in class Car, only the __init__() constructor method is indented in relation to its class.
    The other 2 methods are indented in relation to __init__() instead of their class! @-)

    class Car(object):
        def __init__(self, c, xpos, ypos, xspeed):
            self.c = c
            self.xpos = xpos
            self.ypos = ypos
            self.xspeed = xspeed
    
            def display(self):
                stroke(0)
                fill(self.c)
                rectMode(CENTER)
                rect(self.xpos, self.ypos, 20, 10)
    
            def drive(self):
                self.xpos = self.xpos + self.xspeed
                if self.xpos > width:
                    self.xpos = 0
    

    Solution, make all def aligned, so they're all indented in relation to class Car. B-)

    Extra æsthetic tip, skip 2 lines after each def block. And 3 for each class block: :bz

    class Car(object):
        def __init__(self, c, xpos, ypos, xspeed):
            self.c = c
            self.xpos = xpos
            self.ypos = ypos
            self.xspeed = xspeed
    
    
        def display(self):
            stroke(0)
            fill(self.c)
            rectMode(CENTER)
            rect(self.xpos, self.ypos, 20, 10)
    
    
        def drive(self):
            self.xpos = self.xpos + self.xspeed
    
            if self.xpos > width:
                self.xpos = 0
    
    
    
    myCar1 = Car(color(255, 0, 0), 0, 100, 2)
    myCar2 = Car(color(0, 255, 255), 0, 10, 1)
    
  • thanks a bunch! Apparently the error 'this line _probably _need to be indented' isn't all that helpful then? And admittedly I got annoyed by it. :-S :D

    Thanks again!

Sign In or Register to comment.