my program does allow it to build with no errors but when the answer is right or wrong, it doesn't actually draw the appropriate string. I coded it in a way that I feel like would make it work but after clicking on the check answer button, nothing happens. Any advice?

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()
	{
		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 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);
					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);
	}
}

When I execute it I get the following when I press the button:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at NewJApplet$1.actionPerformed(NewJApplet.java:74)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

Look in the browser's Java console for error messages.

You do nothing with number1 and number2 generated in your actionPerformed()
You defined them there.
may be you want:
number1 = rand.nextInt(10);
number2 = rand.nextInt(10);
so it will affect your instance variables not the ones created in your actionPerformed()

This article has been dead for over six months. Start a new discussion instead.