This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes runtime error - null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "runtime error - null pointer exception" Watch "runtime error - null pointer exception" New topic

runtime error - null pointer exception

ashu s

Joined: Feb 12, 2001
Posts: 8
I'm woking on assignemnt which includes 5 concentric circles like target sign. One white circle one red and so on. If user click anyone of them i want to get those x , y points. and user should see small dot on that target sign. I wrote some code as follows. Right now I can see concentric circles but when I click on those circle I'm getting null pointer exception at runtime. Can somebody please explain me why?
Thanks in advance,
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

<applet code = Target.class height = 400 width=400>
public class Target extends Applet implements MouseListener {
private Label statusBar;
int x,y;
public void init() {
Label statusBar = new Label();
public Target() {
Target t = new Target();

public void paint(Graphics g) {

g.fillOval(0, 0, 240, 240);
g.fillOval(20, 20, 200, 200);

g.fillOval(40, 40, 160, 160);
g.fillOval(60, 60, 120, 120);

g.fillOval(80, 80, 80, 80);
g.fillOval(x, y, 4, 4);
public void mouseClicked(MouseEvent e)
x = e.getX();
y = e.getY();

public void mousePressed(MouseEvent e){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

this works.

One thing to note: When I first pasted your code into my editor and ran it, I noticed that the DOS Window went nuts with some message that I forget now.
But... inside a constructor, *never* call that same constructor, as this is an infinite loop. I've commented out that nasty line. You were thinking of "instantiate the class inside of main" for writing applications I think.
Also, it's not enough to make a new label object.. you must add it to the applet in order for it to show up. In this applet, I've used drawString instead, but if you want the Label, in your init(), remember to add(statusBar) and you will have to use a LayoutManager to position it.
ashu s

Joined: Feb 12, 2001
Posts: 8
Thanks a lot.
You are damn right what I was thinking.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Ashu.
A couple of other points about your original code:
You originally had:

You originally declare that "statusBar" refers to a Label, then within init() you once again declare that "statusBar" refers to a Label, then here you assign "statusBar" to a new Label. Your second declaration of the variable "statusBar" within the init() method is hiding Target's instance variable, "statusBar". What this means is that anything you would have done to statusBar within init() would have applied only to this hiding Label "statusBar", and not to the hidden instance variable, "statusBar". To avoid this hiding behavior in init(), that line should have read

which is legal, because you had earlier already declared statusBar to be a Label.
Another thing, Ashu, in your original code, you had

Although this setText displays the information you want it to, do you see that you are making an extra, unnecessary call to getX() and getY()? You've already placed the ints returned from e.getX() and e.getY() in instance variables x and y, so why not use x and y in setText(), namely

Also, as a minor thing, why not make the "hits" a color different from red (say, black?) so that you can see "hits" that happen to fall on the red rings?
And finally, Ashu, you may be noticing that clicking your mouse on the target removes any target hit that had previously existed. I don't know if this behavior is what you want: after all, x and y are instance variables, meaning each Target will only have one x and one y. If the current behavior is not what you were after, here's a hint: the reason you're not seeing prior hits is because you're not telling Java to reapply them in paint().
Learning Java is a lot like target practice, but practice makes perfect. Good luck, Ashu.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347

ashu s,
Your name does not comply with the Javaranch naming guidelines which can be found at
please register again with a valid name.
more specifically your last name must be more than 1 letter

Visit my download page
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: runtime error - null pointer exception