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?

Recommended Answers

All 12 Replies

the value of variable brush is null; it is not initialized.

hope it helps

the value of variable brush is null; it is not initialized.

hope it helps

Where or how do I initialize it? I understand how that works with other types of variables but not sure I understand how this one works.

May be you need that:

public void init()
	{
brush=this.getGraphics();
//......

}

May be you need that:

public void init()
	{
brush=this.getGraphics();
//......

}

That lets it run but it still doesn't show the strings that I need it to draw after checking the answer. I'm trying different things but no luck so far.

you need may be to iverride the pain method

you need may be to iverride the pain method

I took out my repaint() statements and I can at least get the string, "No. Please try again." but that comes up whether the answer is right or wrong and then doesn't seem to go away. If you have any suggestion about this, let me know. In the meantime, I'll see about overriding the paint method. Unfortunately, that's something else that I don't understand much about.

I'v made here some littel change to you code above:

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 = this.getGraphics();
    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();
    String result = "";

    @Override
    public void init() {
        brush = this.getGraphics();
        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;
                    result=right;
                    repaint();
                    validate();
                } else {
                    answer.setText("");
                    result = wrong;
                    repaint();
                    validate();

                }
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        answer.setText("");
        Random rand = new Random();
        int number1 = rand.nextInt(10);
        int number2 = rand.nextInt(10);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawString(result, 20, 80);
    }
}

Try it it works.
Hope it helps

please remove this declaration:

public Graphics brush = this.getGraphics();

ANd this Instruction:

brush = this.getGraphics();

I'v made here some littel change to you code above:

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 = this.getGraphics();
    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();
    String result = "";

    @Override
    public void init() {
        brush = this.getGraphics();
        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;
                    result=right;
                    repaint();
                    validate();
                } else {
                    answer.setText("");
                    result = wrong;
                    repaint();
                    validate();

                }
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        answer.setText("");
        Random rand = new Random();
        int number1 = rand.nextInt(10);
        int number2 = rand.nextInt(10);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawString(result, 20, 80);
    }
}

Try it it works.
Hope it helps

It still will only display the string for an incorrect answer. Maybe I need repaint() but I'm not sure where. I took it out just to make it show the strings. I'll try to see what that does.

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 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 = getGraphics();
					brush.drawString(right, 20, 80);
					validate();
					
				}

				else
				{
					answer.setText("");
					brush = getGraphics();
					brush.drawString(wrong, 20, 80);
					validate();
				}
			}
		}
		);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		answer.setText("");
		Random rand = new Random();
		int number1 = rand.nextInt(10);
		int number2 = rand.nextInt(10);
	}
}

I took out the repaint methods, you can reinsert them, i also Took out redeclaration of the number integers in the init method. However this code works. Since you were re-declaraing the number integers in the init, the numbers being displayed when the program runs weren't what the number integers were equal to.

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 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 = getGraphics();
					brush.drawString(right, 20, 80);
					validate();
					
				}

				else
				{
					answer.setText("");
					brush = getGraphics();
					brush.drawString(wrong, 20, 80);
					validate();
				}
			}
		}
		);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		answer.setText("");
		Random rand = new Random();
		int number1 = rand.nextInt(10);
		int number2 = rand.nextInt(10);
	}
}

I took out the repaint methods, you can reinsert them, i also Took out redeclaration of the number integers in the init method. However this code works. Since you were re-declaraing the number integers in the init, the numbers being displayed when the program runs weren't what the number integers were equal to.

Thanks. It works better but now what's happening is that if you guess wrong, and then guess again, the "Very good!!!" string is drawn right on top of the "No. Please try again string." but I need the appropriate string to replace the other each time. I put repaint() back in there but probably in the wrong place because with it in there, the strings just flash for 1/2 a second and are gone. I'm so frustrated. I can't get this thing fixed and I haven't even addressed how to reset my random numbers once the correct answer is entered.:confused::@

Maybe create an event that repaints when the text in the textfield is changed instead of in the buttonclick action event. That way it repaints it and clears off the text everytime you enter a number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.