小学生が掛け算を学ぶのに役立つ Java アプレットを作成しようとしたとき。Math.random メソッドまたは Random オブジェクトを使用して、2 つの正の 1 桁の整数を生成します。アプレットの実行時に問題が発生しています。私はJavaプログラミングにかなり慣れていないので、これは非常に簡単な修正かもしれません.
以下の私の現在のコードを参照してください。
import java.awt.*;
import java.awt.Graphics;
import java.lang.Object;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class NewJApplets extends JApplet implements ActionListener
{
public Graphics brush;
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
JLabel question = new JLabel("What is " + number1 + " times " + number2 + "?");
JLabel Result = new JLabel("");
JTextField answer = new JTextField(3);
JButton checkAnswer = new JButton("Check Answer");
Font font1 = new Font("Teen", Font.BOLD, 30);
Font font2 = new Font("Teen", Font.ITALIC, 36);
String right = "Very good!!!";
String wrong = "No. Please try again.";
Container con = getContentPane();
public void init()
{
setLayout(new FlowLayout());
con.setBackground(Color.YELLOW);
question.setLocation(20, 20);
question.setFont(font1);
Result.setLocation (0,100);
Result.setFont(font1);
con.add(question);
con.add(Result);
answer.setLocation(20, 40);
con.add(answer);
checkAnswer.setLocation(20, 60);
con.add(checkAnswer);
checkAnswer.addActionListener(new ActionListener()
{
public void paint()
{
brush.setFont(font2);
}
public void actionPerformed(ActionEvent e)
{
int ans = Integer.parseInt(answer.getText());
if(ans == number1 * number2)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(9) + 1;
int number2 = rand.nextInt(9) + 1;
brush.drawString(right, 20, 80);
Result.setText(right);
repaint();
validate();
}
else
{
answer.setText("");
Result.setText(wrong);
brush.drawString(wrong, 80, 20);
repaint();
validate();
}
}
}
);
}
@Override
public void actionPerformed(ActionEvent e)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
}
}
アプレットは問題なくコンパイルされますが、アプレットを実行しようとすると、次のエラー メッセージが表示されます。
「クラスはアプレットではないようです。」 続行またはキャンセルのオプションがあります。続行をすばやく押すと、機能しているように見えるアプリが表示されます。ただし、適切な結果が得られる代わりに、jGRASP で次のような出力が表示されます。
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at NewJApplets$1.actionPerformed(NewJApplets.java:65)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
この問題に関するヘルプは素晴らしいでしょう。