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

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

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

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

コンパイル

コンパイルとは、プログラミング言語のテキストソース(ソースコード)をコンピュータ上で実行可能な形式(オブジェクトコード)に変換することをいいます

Q&A

1回答

1249閲覧

ボタンを押すと速度が加算されるものを作りたい

asfrofjasf

総合スコア0

Java

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

コンパイル

コンパイルとは、プログラミング言語のテキストソース(ソースコード)をコンピュータ上で実行可能な形式(オブジェクトコード)に変換することをいいます

0グッド

0クリップ

投稿2020/07/19 04:38

前提・実現したいこと

自作でボタンを押すと速度が加算されるものを作っています。しかしエラーが出てしまうのでなぜなのか教えていただきたいです。

コンパイルするとエラーが出ます

発生している問題・エラーメッセージ

Sample.javaのupvx()やdownvx()などの4つでそれぞれ同じエラーが出ます

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Sample.actionPerformed(Sample.java:66) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342) at java.desktop/java.awt.Component.processEvent(Component.java:6401) at java.desktop/java.awt.Container.processEvent(Container.java:2263) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)

該当のソースコード

java

1//Sample.java 2import java.awt.*; 3import java.awt.event.*; 4import javax.swing.*; 5 6public class Sample extends JPanel implements ActionListener{ 7 8 static int xPanelSize; 9 static int yPanelSize; 10 11 12 double x,y,r,vx,vy,ax,ay; 13 BallPropagator b1; 14 Color c1; 15 16 static JButton btn1 = new JButton("↑"); 17 static JButton btn2 = new JButton("↓"); 18 static JButton btn3 = new JButton("→"); 19 static JButton btn4 = new JButton("←"); 20 Sample(int xPanelSize, int yPanelSize){ 21 this.xPanelSize = xPanelSize; 22 this.yPanelSize = yPanelSize; 23 btn1.addActionListener(this); 24 btn1.setBounds(100,100,50,100); 25 26 27 btn2.addActionListener(this); 28 btn2.setBounds(100,50,50,100); 29 30 31 btn3.addActionListener(this); 32 btn3.setBounds(0,100,100,100); 33 34 35 btn4.addActionListener(this); 36 btn4.setBounds(200,100,100,100); 37 38 39 40 41 } 42 //イベント(タイマーによる呼び出し)が発生したときの処理 43 public void dctionPerformed(ActionEvent e){ 44 45 //ボールb1 の位置の更新 46 b1.xUpdate(); 47 b1.yUpdate(); 48 //ボールb1 の速度の更新 49 b1.vxUpdate(); 50 b1.vyUpdate(); 51 52 53 // 再描画 54 repaint(); 55 56 } 57 public void actionPerformed(ActionEvent e){ 58 JButton selectedButton = (JButton)e.getSource(); 59 if(selectedButton==btn1){ 60 System.out.println("up"); 61 b1.upvy(); 62 63 repaint(); 64 }else if(selectedButton==btn2){ 65 System.out.println("down"); 66 b1.downvy(); 67 68 repaint(); 69 }else if(selectedButton==btn3){ 70 System.out.println("left"); 71 b1.leftvx(); 72 73 repaint(); 74 }else{ 75 System.out.println("right"); 76 b1.rightvx(); 77 78 repaint(); 79 } 80 } 81 82 public void paintComponent(Graphics g){ 83 super.paintComponent(g); 84 85 g.setColor(c1); 86 87 g.fillOval((int)(b1.a(x) - b1.c(r)), (int)(b1.b(y) - b1.c(r)), 88 (int)(2*b1.c(r)), (int)(2*b1.c(r))); 89 90 91 92 } 93 94 95 96 public static void main(String[] args) { 97 BallMain panel = new BallMain(640,480); 98 Sample sample=new Sample(640,480); 99 100 BallPropagator.dt=0.1; 101 BallPropagator.o=5.0; 102 103 JFrame frame = new JFrame(); 104 105 106 107 108 panel.setBackground(Color.white); 109 panel.setPreferredSize(new Dimension(xPanelSize,yPanelSize)); 110 panel.add(btn1); 111 panel.add(btn2); 112 panel.add(btn3); 113 panel.add(btn4); 114 115 BallPropagator.xmin = 0; 116 BallPropagator.xmax = panel.xPanelSize; 117 BallPropagator.ymin = 0; 118 BallPropagator.ymax = panel.yPanelSize; 119 120 121 frame.add(panel); 122 frame.pack(); 123// 124 frame.setTitle("反射するボール"); 125 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 126 //frame.setResizable(true); 127 frame.setVisible(true); 128 129 130 Timer timer = new Timer(10, panel); 131 timer.start(); 132 133 134 } 135} 136 137
BallMain.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BallMain extends JPanel implements ActionListener{ static int xPanelSize; static int yPanelSize; double x,y,r,vx,vy,ax,ay; BallPropagator b1; Color c1; BallMain(int xPanelSize, int yPanelSize){ this.xPanelSize = xPanelSize; this.yPanelSize = yPanelSize; b1 = new BallPropagator(10, 100, 100, 30, 40, 0, 9.8); ///////////////////////////////////////////// c1=Color.black; ///////////////////////////////////////////// } public void actionPerformed(ActionEvent e){ b1.xUpdate(); b1.yUpdate(); b1.vxUpdate(); b1.vyUpdate(); repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(c1); g.fillOval((int)(b1.a(x) - b1.c(r)), (int)(b1.b(y) - b1.c(r)), (int)(2*b1.c(r)), (int)(2*b1.c(r))); } }

java

1BallPropagator.java 2public class BallPropagator{ 3 4 public static double dt; 5 public static int xmin; 6 public static int xmax; 7 public static int ymin; 8 public static int ymax; 9 10 11 private double r; 12 private double x, y; 13 private double vx, vy; 14 private double ax, ay; 15 public static double o; 16 double p=0.8; 17 18 19 20 BallPropagator(double r,double x,double y,double vx,double vy,double ax,double ay){ 21 this.r=r; 22 this.x=x; 23 this.y=y; 24 this.vx=vx; 25 this.vy=vy; 26 this.ax=ax; 27 this.ay=ay; 28 } 29 30 public void xUpdate() { 31 this.x = this.x + this.vx * dt; 32 33 if(this.x < (double)xmin+this.r ){ 34 // this.vx = ?????????? 35 this.vx *= -p; 36 this.x = (double)xmin+this.r; 37 }else if((double)xmax-this.r < x ){ 38 // this.vx = ?????????? 39 this.vx *= -p; 40 this.x = (double)xmax-this.r; 41 } 42 } 43 44 public void yUpdate() { 45 this.y = this.y + this.vy * dt; 46 47 if(this.y < (double)ymin + this.r ){ 48 // this.vy = ?????????? 49 this.vy *= -p; 50 this.y = (double)ymin+this.r; 51 }else if((double)ymax-this.r < this.y ){ 52 // this.vy = ?????????? 53 this.vy *= -p; 54 this.y = (double)ymax-this.r; 55 } 56 } 57 58 public void vxUpdate(){ 59 60 this.vx=this.ax*dt+this.vx; 61 62 } 63 64 public void vyUpdate(){ 65 this.vy=this.ay*dt+this.vy; 66 // ???????? 67 // ???????? 68} 69 public void upvy(){ 70 this.vy=this.vy+this.o; 71 } 72 public void downvy(){ 73 this.vy=this.vy-this.o;} 74 public void leftvx(){ 75 this.vx=this.vx-this.o;} 76 public void rightvx(){ 77 this.vx=this.vx+this.o;} 78 79 80 81 82 83 public double a(double x){ 84 return x=this.x; 85} 86 public double b(double x){ 87 return x=this.y; 88 } public double c(double x){ 89 return x=this.r; 90 } 91}

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

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

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

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

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

TN8001

2020/07/20 08:44

[Java - ボタンを押しても反応しないです|teratail](https://teratail.com/questions/278736 [Java - ボタンを押すと速度が加算されるものを作りたい|teratail](https://teratail.com/questions/278819 [Java - javaの関数が反応しない|teratail](https://teratail.com/questions/279029 3つとも同じコードに見えます。 これは3人とも同じ課題に取り組んでいるということでしょうか?? (申し上げにくいのですがあまりにもひどいコードなので)とても課題とは思えず、同一人物の投稿かと思ったのですがそうではないのですね?
guest

回答1

0

Sampleのb1がどこでも初期化されていないから、ですね。

java

1public class Sample extends JPanel implements ActionListener{ 2... 3 BallPropagator b1; 4... 5 public void actionPerformed(ActionEvent e){ 6... 7 b1.upvy();

BallMainのb1は初期化されているようですが、BallMainを使っている場所が、みたらない。

java

1public class BallMain extends JPanel implements ActionListener{ 2... 3 BallPropagator b1; 4... 5 b1 = new BallPropagator(10, 100, 100, 30, 40, 0, 9.8);

投稿2020/07/19 04:54

shiketa

総合スコア3971

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

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

asfrofjasf

2020/07/19 05:21

ご返事ありがとうございます。初期化してないからエラーが出てしまったのですね。しかし、これを実行するとvxup()などが機能しなくなるのですがこれは初期化したからでしょうか?
shiketa

2020/07/19 05:49

ナニをどのように修正したのかわからないのに、わたしにその理由がわかるわけはありません。一般的なNullPointerExcptionの原因を指摘しただけですので、動作については自力で解決してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問