aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "runtime error - null pointer exception" Watch "runtime error - null pointer exception" New topic
Author

runtime error - null pointer exception

ashu s
Greenhorn

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,
Ashu
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/*
<applet code = Target.class height = 400 width=400>
</applet>
*/
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();
t.addMouseListener(this);
}


public void paint(Graphics g) {

g.setColor(Color.red);
g.fillOval(0, 0, 240, 240);
g.setColor(Color.white);
g.fillOval(20, 20, 200, 200);

g.setColor(Color.red);
g.fillOval(40, 40, 160, 160);
g.setColor(Color.white);
g.fillOval(60, 60, 120, 120);

g.setColor(Color.red);
g.fillOval(80, 80, 80, 80);
g.fillOval(x, y, 4, 4);
}
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
statusBar.setText(e.getX()+","+e.getY());


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

Joined: Feb 12, 2001
Posts: 8
Mike,
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.
Art
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: runtime error - null pointer exception