質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

2回答

691閲覧

[Java] Error: java.lang.NullPointerException

amber_snob

総合スコア30

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2019/11/03 12:46

JavaからProcessingをライブラリとして使用しPythonのタートルグラフィックスと似た機能を実装しています。

ソースコード中の関数draw()の一部分

Java

1turtles[i] = t;

でjava.lang.NullPointerExceptionがでました。
変数tはクラスTurtleのインスタンスでそれをTurtleの配列に代入したところエラーが出たようです。

エラーの解消方法を教えていただきたいです。

以下は関数draw()の部分になります。

Java

1 public void draw() { 2 background(255); 3 // BEGIN 4 float r = 100 * cos(2 * PI / 5) / cos(3 * PI / 10); 5 for (int i = 0; i < 5; i++) { 6 float a = PI * (4 * i - 7) / 10; 7 float x = 250 + r * cos(a); 8 float y = 250 + r * sin(a); 9 Turtle t = new Turtle(x, y, 2 * PI * (i - 1) / 5, i); 10 turtles[i] = t; 11 } 12 for (int i = 0; i < 5; i++) { 13 // BEGIN 14 drawStar(turtles[i]); 15 turtles[i].up(); 16 turtles[i].right(2 * PI / 5); 17 turtles[i].forward(200 * cos(2 * PI / 5)); 18 // END 19 } 20 for (int i = 0; i < 5; i++) { 21 turtles[i].drawCursor(); 22 } 23 // END 24 currentFrame++; 25 }

以下はソースコード全体になります。

java

1import processing.core.PApplet; 2 3public class ObjectTurtleGraphics extends PApplet { 4 5 static final float LINE_WEIGHT = 3; 6 static final int LINE_COLOR = 0xff000000; 7 8 static final float LINEAR_SPEED = 400; 9 static final float ROTATIONAL_SPEED = 2 * PI; 10 11 static final float TURTLE_WEIGHT = 5; 12 static final int TURTLE_COLOR = 0xff00c000; 13 static final float TURTLE_SIZE = 10; 14 static final float TURTLE_ANGLE = PI / 3; 15 16 class Turtle { 17 18 boolean downFlag; 19 float positionX; 20 float positionY; 21 float direction; 22 float remainingTime; 23 24 // BEGIN 25 Turtle(float px, float py, float dir, float delay) { 26 this.downFlag = true; 27 this.positionX = px; 28 this.positionY = py; 29 this.direction = dir; 30 this.remainingTime = currentFrame / 60.0f - delay; 31 } 32 33 void down() { 34 this.downFlag = true; 35 } 36 37 void up() { 38 this.downFlag = false; 39 } 40 41 void forward(float distance) { 42 // BEGIN 43 strokeWeight(LINE_WEIGHT); 44 stroke(LINE_COLOR); 45 noFill(); 46 if(this.downFlag){ 47 if(this.remainingTime > 0 && this.remainingTime*LINEAR_SPEED < distance){ // 描画が中途半端な時に実行される。 48 beginShape(); 49 vertex(this.positionX, this.positionY); 50 vertex(this.positionX + this.remainingTime * LINEAR_SPEED * cos(this.direction), 51 this.positionY + this.remainingTime * LINEAR_SPEED * sin(this.direction)); 52 endShape(CLOSE); 53 this.positionX += this.remainingTime * LINEAR_SPEED * cos(this.direction); 54 this.positionY += this.remainingTime * LINEAR_SPEED * sin(this.direction); 55 }else if(this.remainingTime*LINEAR_SPEED >= distance){ // 描画が完璧な時に実行される。 56 beginShape(); 57 vertex(this.positionX, this.positionY); 58 vertex(this.positionX + distance * cos(this.direction), 59 this.positionY + distance * sin(this.direction)); 60 endShape(CLOSE); 61 this.positionX += distance * cos(this.direction); 62 this.positionY += distance * sin(this.direction); 63 } 64 }else if(!this.downFlag){ 65 if(this.remainingTime > 0 && this.remainingTime*LINEAR_SPEED < distance){ // 描画が中途半端な時に実行される。 66 this.positionX += this.remainingTime * LINEAR_SPEED * cos(this.direction); 67 this.positionY += this.remainingTime * LINEAR_SPEED * sin(this.direction); 68 }else if(this.remainingTime*LINEAR_SPEED >= distance){ // 描画が完璧な時に実行される。 69 this.positionX += distance * cos(this.direction); 70 this.positionY += distance * sin(this.direction); 71 } 72 } 73 // END 74 this.remainingTime -= distance / LINEAR_SPEED; 75 } 76 77 void right(float angle) { 78 // BEGIN 79 if(this.remainingTime > 0 && this.remainingTime*ROTATIONAL_SPEED < angle){ // 描画が中途半端な時に実行される。 80 this.direction += this.remainingTime * ROTATIONAL_SPEED; 81 }else if(this.remainingTime*ROTATIONAL_SPEED >= angle){ // 描画が完璧な時に実行される。 82 this.direction += angle; 83 } 84 // END 85 this.remainingTime -= angle / ROTATIONAL_SPEED; 86 } 87 88 void drawCursor() { 89 strokeWeight(TURTLE_WEIGHT); 90 stroke(TURTLE_COLOR); 91 noFill(); 92 beginShape(); 93 float a = PI - TURTLE_ANGLE / 2; 94 vertex(this.positionX + TURTLE_SIZE * cos(this.direction - a), 95 this.positionY + TURTLE_SIZE * sin(this.direction - a)); 96 vertex(this.positionX, this.positionY); 97 vertex(this.positionX + TURTLE_SIZE * cos(this.direction + a), 98 this.positionY + TURTLE_SIZE * sin(this.direction + a)); 99 endShape(); 100 } 101 // END 102 103 } 104 105 int currentFrame; 106 Turtle[] turtles; 107 108 public void settings() { 109 size(500, 500); 110 } 111 112 public void setup() { 113 currentFrame = 0; 114 } 115 116 // 以下にメソッドdrawStarを定義する. 117 // BEGIN 118 public void drawStar(Turtle t){ 119 t.down(); 120 for (int j = 0; j < 5; j++){ 121 t.forward(100); 122 t.right(4 * PI / 5); 123 } 124 } 125 // END 126 127 public void draw() { 128 background(255); 129 // BEGIN 130 float r = 100 * cos(2 * PI / 5) / cos(3 * PI / 10); 131 for (int i = 0; i < 5; i++) { 132 float a = PI * (4 * i - 7) / 10; 133 float x = 250 + r * cos(a); 134 float y = 250 + r * sin(a); 135 Turtle t = new Turtle(x, y, 2 * PI * (i - 1) / 5, i); 136 turtles[i] = t; 137 } 138 for (int i = 0; i < 5; i++) { 139 // BEGIN 140 drawStar(turtles[i]); 141 turtles[i].up(); 142 turtles[i].right(2 * PI / 5); 143 turtles[i].forward(200 * cos(2 * PI / 5)); 144 // END 145 } 146 for (int i = 0; i < 5; i++) { 147 turtles[i].drawCursor(); 148 } 149 // END 150 currentFrame++; 151 } 152 153 public void keyPressed() { 154 if (key == 's') { 155 saveFrame("ObjectTurtleGraphics.png"); 156 } else if (key == 'r') { 157 currentFrame = 0; 158 } 159 } 160 161 public static void main(String[] args) { 162 PApplet.main(ObjectTurtleGraphics.class.getName()); 163 } 164 165}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Turtle[] turtlesは宣言だけしてあって、どこでも初期化されていません。
クラスのフィールド変数は参照型の場合nullが初期値となり、そのnullに対して配列のように操作をしようとしたため、NullPointerExceptionになります。

投稿2019/11/03 13:04

swordone

総合スコア20651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

xebme

2019/11/03 13:12

turtles配列を生成すれば、5角形の辺の外側に五芒星が5つ現れます。マルチスレッドプログラミングが美しい。Turtle[] turtles = new Turtle[5];
guest

0

ベストアンサー

java

1Turtle[] turtles;

で宣言だけされた後に、

java

1public void draw() { 2 background(255); 3 // BEGIN 4 float r = 100 * cos(2 * PI / 5) / cos(3 * PI / 10); 5 for (int i = 0; i < 5; i++) { 6 float a = PI * (4 * i - 7) / 10; 7 float x = 250 + r * cos(a); 8 float y = 250 + r * sin(a); 9 Turtle t = new Turtle(x, y, 2 * PI * (i - 1) / 5, i); 10 turtles[i] = t; 11 }

と、turtlesの配列を使用していますが、初期化されていないので利用できません。
後に続く処理で、配列数が0~5の6個だけ必要なのが変わるのですから、

java

1Turtle[] turtles = new Turtle[6];

にすべきでしょう。

投稿2019/11/03 13:06

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

amber_snob

2019/11/03 13:15

ありがとうございます。解決しました。
xebme

2019/11/03 13:29

要素数5でしょう。試してみてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問