1

So I am using Processing.py except I am using it through IDLE as my project requires python modules as well but I am getting an error that makes no sense to me. Was wondering if anyone here had any ideas on how I could fix it?

Here's the error I am getting:

>>> The file "Background.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
>> java.lang.NullPointerException
>>  at processing.core.PGraphics.image(PGraphics.java:3767)
>>  at processing.core.PApplet.image(PApplet.java:12132)
>>  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:515)
>>  at org.python.core.PyObject.__call__(PyObject.java:521)
>>  at org.python.core.PyMethod.__call__(PyMethod.java:171)
>>  at org.python.pycode._pyx7.f$0(<string>:1)
>>  at org.python.pycode._pyx7.call_function(<string>)
>>  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._pyx5.listen$5(i3_jython.py:28)
>>  at org.python.pycode._pyx5.call_function(i3_jython.py)
>>  at org.python.core.PyTableCode.call(PyTableCode.java:167)
>>  at org.python.core.PyBaseCode.call(PyBaseCode.java:124)
>>  at org.python.core.PyFunction.__call__(PyFunction.java:403)
>>  at org.python.pycode._pyx5.draw$2(i3_jython.py:19)
>>  at org.python.pycode._pyx5.call_function(i3_jython.py)
>>  at org.python.core.PyTableCode.call(PyTableCode.java:167)
>>  at org.python.core.PyBaseCode.call(PyBaseCode.java:124)
>>  at org.python.core.PyFunction.__call__(PyFunction.java:403)
>>  at org.python.core.PyFunction.__call__(PyFunction.java:398)
>>  at jycessing.PAppletJythonDriver.draw(PAppletJythonDriver.java:1059)
>>  at processing.core.PApplet.handleDraw(PApplet.java:2403)
>>  at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
>>  at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Attached is my file structure which clearly shows that I do in fact have the image file in the working directory

This is my file structure:

Hope someone will be able to help

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Owen
  • 13
  • 6
  • All that file structure shows is that "Background.png" is in the same directory as the file(s) that you are attempting to execute. However, the directory containing a Python file is *not* necessarily its working directory. – jjramsey Jun 16 '21 at 18:27
  • "or that the file has been added to your sketch and is readable" -- there's probably some procedure to "add that file to your sketch". – AKX Jun 16 '21 at 18:31
  • You may want to look at this question: https://stackoverflow.com/questions/15367688/default-working-directory-for-python-idle – jjramsey Jun 16 '21 at 20:23
  • @Owen Could post the snippet where you're attempting to load Background.png ? (You might need something like `loadImage(sketchPath("Background.png"))` in the hope the IDLE initialised PApplet can still initialise paths the same as the Processing IDE does) – George Profenza Jun 16 '21 at 21:02
  • Will add that when I'm back on my main development device – Owen Jun 18 '21 at 07:44

1 Answers1

0

Odds are that the working directory is not where the script is actually located. This can be solved by getting the absolute path of "background.png" as follows:

import os
dirname = os.getcwd()
filepath = dirname + '/background.png'
Peatherfed
  • 178
  • 1
  • 10
  • That shouldn't work, unless the point of the script is to find whatever file named "background.png" is in the current directory, rather than the "background.png" file that is in a particular place. More likely than not, the desired "background.png" is in the same directory as the Python script, in which case, `dirname` should be `os.path.dirname(os.path.abspath(__file__))`. – jjramsey Jun 17 '21 at 16:09