Processingエラーまとめ
自分がProcessingでコーディング中に遭遇したエラーと解決策をまとめました。
きっとこんなエラー初歩的過ぎて誰も記事にしないと思いますが、私のような迷える子羊のために書きました。
Processingバージョン: 3.3
- NullPointerException
- hoge cannot be resolved to a variable
- The size of this sketch could not be determined from your code
NullPointerException
エラー: NullPointerException
原因: クラスの初期化が行われていないため
クラスをインスタンス化しないまま使用しようとすると出ます。
例
class Hoge{ int piyo; int getPiyo(){ return piyo; } } Hoge hoge; void draw(){ hoge.getPiyo(); //ここでNullPointerException }
解決策: クラスのインスタンスを作る。
class Hoge{ int piyo; int getPiyo(){ return piyo; } } Hoge hoge; void setup(){ hoge = new Hoge(); //Hogeのインスタンスを作成 } void draw(){ hoge.getPiyo(); }
hoge cannot be resolved to a variable
エラー: hoge cannot be resolved to a variable
原因: void setup()とvoid draw()でスコープが異なるため、setupで宣言した変数はdrawから見ることができない。
例
void setup(){ int hoge =0; } void draw(){ println(hoge); }
解決策: hogeをグローバル変数にするか、hogeをdraw()内で宣言する。
The size of this sketch could not be determined from your code
エラー: The size of this sketch could not be determined from your code. Use only numbers(not variables) for the size() command. Read the size() reference for more details.
原因: size()に変数を引数として渡しているから
例
final int SCREEN_WIDTH = 800; final int SCREEN_HEIGHT = 600; void setup(){ size(SCREEN_WIDTH,SCREEN_HEIGHT); background(0); } void draw(){ }
finalを指定していても変数ではあるため、size関数は受け付けてくれない。
解決策: settings()を使用する。
final int SCREEN_WIDTH = 800; final int SCREEN_HEIGHT = 600; void settings(){ size(SCREEN_WIDTH,SCREEN_HEIGHT); } void setup(){ background(0); } void draw(){ }
settings()内でsize()を指定することで、size()の引数として変数が使用できる。
参考サイト
qiita.com
tyfkda.github.io
settings() \ Language (API) \ Processing 2+