I've got this applet to build with no errors but then I try to run it and when I click the button in the applet, nothing happens...at least, not in the applet. Instead, jGRASP gives me all this stuff and I don't understand a bit of it:
----jGRASP exec: appletviewer jgrasphta.htm
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at NewJApplet$1.actionPerformed(NewJApplet.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Here's the code for my applet:
import java.awt.*;
import java.awt.Graphics;
import java.lang.Object;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class NewJApplet 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 + "?");
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()
{
number1 = rand.nextInt(9) + 1;
number2 = rand.nextInt(9) + 1;
setLayout(new FlowLayout());
con.setBackground(Color.BLUE);
question.setLocation(20, 20);
question.setFont(font1);
con.add(question);
answer.setLocation(20, 40);
con.add(answer);
checkAnswer.setLocation(20, 60);
con.add(checkAnswer);
checkAnswer.addActionListener(new ActionListener()
{
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);
repaint();
validate();
}
else
{
answer.setText("");
brush.drawString(wrong, 20, 80);
repaint();
validate();
}
}
}
);
}
@Override
public void actionPerformed(ActionEvent e)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
}
}
Basically, this applet is supposed to ask what one, positive, single-digit number times another is, allow the user to input their answer into a JTextField, and have a button to click to check the answer. When the button is clicked and finds that the answer is wrong, it should say "No. Please try again." and allow the user to try as many times as it takes for them to get it right. If the answer is right, it should say "Very good!" and then reset the question with two new random numbers. Even in other attempts, I couldn't get the random numbers to reset but now it barely even runs. My deadline is tomorrow night at 10:00pm. Can anyone help me PLEASE?