简体   繁体   English

为什么我在 Processing.py 中遇到 strokeWeight() 的 NullPointerException?

[英]Why am I getting a NullPointerException for strokeWeight() in Processing.py?

A quick disclaimer: I'm a bit of a newbie, so this issue might be just me not seeing the obvious, but basically when I run快速免责声明:我有点新手,所以这个问题可能只是我没有看到明显的问题,但基本上当我跑步时

def setup():
    size(400,400)

def test():
    strokeWeight(5)
    circle(100, 100, 100)

test()

in Processing I get the following error:在处理中我收到以下错误:

processing.app.SketchException: java.lang.NullPointerException
at processing.core.PApplet.strokeWeight(PApplet.java:14431)
    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:188)
    at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:206)
    at org.python.core.PyObject.__call__(PyObject.java:497)
    at org.python.core.PyObject.__call__(PyObject.java:501)
    at org.python.core.PyMethod.__call__(PyMethod.java:141)
    at org.python.pycode._pyx58.test$2(sketch_210426a.pyde:6)
    at org.python.pycode._pyx58.call_function(sketch_210426a.pyde)
    at org.python.core.PyTableCode.call(PyTableCode.java:171)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:139)
    at org.python.core.PyFunction.__call__(PyFunction.java:413)
    at org.python.pycode._pyx58.f$0(sketch_210426a.pyde:1)
    at org.python.pycode._pyx58.call_function(sketch_210426a.pyde)
    at org.python.core.PyTableCode.call(PyTableCode.java:171)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1614)
    at org.python.core.Py.exec(Py.java:1658)
    at org.python.pycode._pyx57.f$0(/var/folders/vf/3zy00t252_372n6n1n060tc00000gn/T/sketch_210426a6112183873956099370/sketch_210426a.pyde:96)
    at org.python.pycode._pyx57.call_function(/var/folders/vf/3zy00t252_372n6n1n060tc00000gn/T/sketch_210426a6112183873956099370/sketch_210426a.pyde)
    at org.python.core.PyTableCode.call(PyTableCode.java:171)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1614)
    at org.python.core.Py.exec(Py.java:1658)
    at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
    at jycessing.PAppletJythonDriver.processSketch(Unknown Source)
    at jycessing.PAppletJythonDriver.findSketchMethods(Unknown Source)
    at jycessing.Runner.runSketchBlocking(Unknown Source)
    at jycessing.mode.run.SketchRunner.lambda$startSketch$3(Unknown Source)
    at java.lang.Thread.run(Thread.java:748)

    at jycessing.mode.run.SketchRunner.convertPythonSketchError(Unknown Source)
    at jycessing.mode.run.SketchRunner.lambda$startSketch$3(Unknown Source)
    at java.lang.Thread.run(Thread.java:748)

Interestingly enough, I am only returned the error when any one of Processing's built in functions (such as setup() , draw() or mouseClicked ) is defined as well, so if I ran:有趣的是,只有在同时定义了任何一个处理的内置函数(例如setup()draw()mouseClicked )时,我才会返回错误,所以如果我运行:

def why_arent_you_working():
    print("please")
    
def test():
    strokeWeight(5)
    circle(100, 100, 100)
    
test()
why_arent_you_working()

it would work for some reason.出于某种原因,它会起作用。

I've tried 10 diffrent threads concerning NullPointerException but I just can't seem to find what I did wrong.我已经尝试了 10 个关于NullPointerException的不同线程,但我似乎无法找到我做错了什么。 As I've prefaced this post, I am a newbie, so its probably some stupid oversight of mine but it would be great if someone helped me out.正如我在这篇文章的前言中一样,我是一个新手,所以这可能是对我的一些愚蠢的疏忽,但如果有人帮助我,那就太好了。

When you declare a variable what you're doing is creating something that points towards that variable.当您声明一个变量时,您正在做的是创建指向该变量的东西。 Ie when we create a variable x = 5. x is just a reference that points towards the number 5 .即当我们创建一个变量 x = 5. x只是一个指向数字5的引用。

NullPointerException occurs when you're pointing at something that is Null .当您指向Null的东西时,会发生NullPointerException I'm not all too familiar with the strokeWeight method so I can't tell you exactly but I can try to help more if you add a tiny bit more code for me.我对 strokeWeight 方法不太熟悉,所以我不能准确地告诉你,但如果你为我添加更多代码,我可以尝试提供更多帮助。

In the log files (The second part you've coded in) you can click the line number and it will take you to which part of a method the error is coming from.在日志文件(您已编码的第二部分)中,您可以单击行号,它将带您到错误来自方法的哪一部分。

So below it tells us that the NullPointerException is coming from somewhere called processing.core.PApplet.strokeWeight in line 14431. If you could click the number and copy the method into your original post I can try help a little more from there.因此,它在下面告诉我们NullPointerException来自第 14431 行中名为processing.core.PApplet.strokeWeight的某个地方。如果您可以单击该数字并将该方法复制到您的原始帖子中,我可以尝试从那里获得更多帮助。

processing.app.SketchException: java.lang.NullPointerException at processing.core.PApplet.strokeWeight(PApplet.java:14431)

This might be a result of "mixing active and static modes".这可能是“混合活动模式和 static 模式”的结果。 Active mode is when you use setup() and/or draw() , and static mode is when you don't.活动模式是在您使用setup()和/或draw()时,而 static 模式是在您不使用时。 Basically, if you're using setup() , you can't call any functions outside of an indentation (or a variable definition).基本上,如果您使用的是setup() ,则不能在缩进(或变量定义)之外调用任何函数。 The standard way to draw a circle on the screen is like this:在屏幕上画圆的标准方法是这样的:

def setup():
    size(400,400)

def draw():
    strokeWeight(5)
    circle(100, 100, 100)

draw() is called just like setup() is: it's automatically called without you explicitly calling it anywhere. draw()的调用就像setup()一样:它会自动调用,而无需您在任何地方显式调用它。

Keep in mind that draw() is a loop, so it will run continuously until noLoop() is called.请记住, draw()是一个循环,因此它将持续运行,直到noLoop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM