PApplet内の変数g(Pgraphics)がNullのまま処理が行われる。
Processingのアニメーションを練習するため簡単なクラスを作ったのですが、なぜかPApplet
クラスの変数である
PGraphics g
という変数が初期化されず、Nullのままなため、PApplet
クラスを継承したTestPVector
でellpse()
を呼び出したときにPApplet
でjava.lang.NullPointerException
が発生します。
ざっとデバックしてみたのですが、なぜnullのままなのかまったくわかりません。そもそもPApplet
内のどこでg
が初期化されているのかすらわかりませんでした。
画面は、フレームは表示されるが、まったく更新されず灰色のまま特に動くことはありません。
発生している問題・エラーメッセージ
java.lang.NullPointerException at processing.core.PApplet.ellipse(PApplet.java:12103) at usingProcessing.TestPVector.draw(TestPVector.java:40) at usingProcessing.RandomSpreadParticle.draw(RandomSpreadParticle.java:34) at processing.core.PApplet.handleDraw(PApplet.java:2429) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
該当のソースコード
Processing
1public class RandomSpreadParticle extends PApplet { 2 int num = 1000; 3 TestPVector[] mys = new TestPVector[num]; 4 5 public static void main(String[] args) { 6 // TODO 自動生成されたメソッド・スタブ 7 PApplet.main(new String[] {RandomSpreadParticle.class.getName()}); 8 } 9 10 public void settings() { 11 size(800,600); 12 } 13 public void setup() { 14 frameRate(60); 15 for(int i = 0; i < num; i++) { 16 mys[i] = new TestPVector(); 17 mys[i].location.set(width/2.0f, height/2.0f); 18 mys[i].acceleration.set(random(-10, 10), random(-10, 10)); 19 mys[i].gravity.set(0.0f,0.1f); 20 mys[i].friction = 0.01f; 21 } 22 } 23 public void draw() { 24 fill(0, 31); 25 rect(0, 0, width, height); 26 noStroke(); 27 fill(255); 28 for(int i = 0; i < num; i++) { 29 mys[i].update_loc(); 30 mys[i].draw(); 31 mys[i].bounce(); 32 } 33 } 34} 35-------------------------------------------------------------------------------------------------------- 36 37public class TestPVector extends PApplet { 38 PVector location; 39 PVector acceleration; 40 PVector velocity; 41 PVector gravity; 42 PVector min; 43 PVector max; 44 float mass; 45 float friction; 46 float radius; 47 48 public TestPVector() { 49 mass = 1.0f; 50 friction = 0.01f; 51 radius = 4.0f; 52 //位置、加速度、速度、質量、初期の力を初期化 53 location = new PVector(0f, 0f); 54 velocity = new PVector(0f, 0f); 55 gravity = new PVector(0f, 0f); 56 acceleration = new PVector(0f, 0f); 57 //運動範囲 58 min = new PVector(0.0f,0.0f); 59 max = new PVector(width,height); 60 } 61 62 public void draw() { 63 ellipse(location.x, location.y,mass * radius * 2, mass * radius * 2); 64 } 65 66 public void update_loc() { 67 //加速度に重力を追加 68 acceleration.add(gravity); 69 //速度に運動量を追加 70 velocity.add(acceleration); 71 //運動量に摩擦力をかける 72 velocity.mult(1.0f-friction); 73 //位置に運応用を追加 74 location.add(velocity); 75 //加速度0 76 acceleration.set(0 ,0); 77 } 78 79 public void bounce() { 80 if(location.x > max.x) { 81 location.x = max.x; 82 velocity.x *= -1; 83 } 84 if(location.x < min.x) { 85 location.x = min.x; 86 velocity.x *= -1; 87 } 88 if(location.y > max.y) { 89 location.y = max.y; 90 velocity.y *= -1; 91 } 92 if(location.y < min.y) { 93 location.y = min.y; 94 velocity.y *= -1; 95 } 96 } 97 public void throughWall() { 98 if(location.x > max.x) { 99 location.x = max.x; 100 } 101 if(location.x < min.x) { 102 location.x = min.x; 103 } 104 if(location.y > max.y) { 105 location.y = max.y; 106 } 107 if(location.y < min.y) { 108 location.y = min.y; 109 } 110 } 111} 112
試したこと
入門本を参考に書いてるため難しいことはしていません。なので、ほとんどサンプルコードを参考に書いてます。デバッグはしたのですが、PApplet
以外の箇所でnullが発生していることはなかったです。
補足情報(FW/ツールのバージョンなど)
eclipse32bit版の環境で動かしています。pleiadesでダウンロードしたので、おそらくjavaも32bit版だと思います。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。