This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

runtime error - null pointer exception

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){ }

Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
You are damn right what I was thinking.
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 4716
Scala Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic