1

I just started learning about java applets and came upon this error. Can someone help me?

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;


public class MakeDots extends Applet implements MouseListener{

    private Graphics graphics = null;

    public void init() {
        this.addMouseListener(this);
    }

    public void paint(Graphics g) {
        this.setSize(800, 600);
        g.create();
    }
    public void drawDot(int x, int y) {
        int r = (int)(Math.random()*256);
        int b = (int)(Math.random()*256);
        int g = (int)(Math.random()*256);
        Color color = new Color(r, g, b);
        graphics.setColor(color);
        graphics.fillOval(x, y, 3, 3);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int mouseX = e.getX();
        int mouseY = e.getY();
        drawDot(mouseX, mouseY);
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

This error comes up:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MakeDots.drawDot(MakeDots.java:26)
    at MakeDots.mouseClicked(MakeDots.java:34)
    at java.awt.Component.processMouseEvent(Component.java:6417)
    at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
    at java.awt.Container.dispatchEventImpl(Container.java:2142)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I don’t know why!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Quintec
  • 1,064
  • 12
  • 22
  • 1
    `private Graphics graphics = null;` -- and you wonder why you're getting a NPE? – Hovercraft Full Of Eels May 10 '14 at 19:03
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson May 11 '14 at 07:42

3 Answers3

3

This is not how you do Swing or AWT drawing:

private Graphics graphics = null;

Instead if an AWT application, you should draw in the paint method of a Component or child of this class) and use the Graphics object provided by the JVM.

Much better would be to create a Swing application and draw in the paintComponent(Graphics g) method of a JComponent or child of this, again using the Graphics object provided by the JVM. Most important, Google and read the tutorials. I can speak from experience by telling you that you shouldn't guess at this stuff as you'll invariably guess wrong.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

You're trying to call a method on the null pointer graphics. Graphics must be instantiated and point to an instance before you can call methods on it.

See more here What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
Kon
  • 10,702
  • 6
  • 41
  • 58
  • Doesn't g.create make the graphics? – Quintec May 11 '14 at 13:42
  • @thecoder16: Always check the API for such questions. The [Graphics API](http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html) will tell you that `g.create()` creates a copy of the Graphics object, and I've found this useful when I want to change the properties of a Graphics object but don't want the changes to affect the drawing of the rest of my GUI, but simply calling this method as you're doing will have no benefit to your program. – Hovercraft Full Of Eels May 11 '14 at 13:48
0

Although both the answers were helpful, none of them gave me a solution. Kon’s answer gave me what was wrong:
g.create();
Then I found out it should be:
graphics = g.create();

Again, thanks for all the suggestions!

Quintec
  • 1,064
  • 12
  • 22