Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class

 
kofi ofei
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following programme below compiles but when i run it nothing is displayed in the applet viewer


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class InnerClasses extends Applet{
String m;
public InnerClasses(){
MyListener m = new MyListener();
addMouseListener(m);
}
//start an inner class called MyListener
public final class MyListener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
if(e.getClickCount()== 1) m = "Single click";
else if(e.getClickCount()== 2) m = "Double click";
int mod = e.getModifiers();
if((mod & InputEvent.BUTTON1_MASK) !=0) m += "Button1";
else if((mod & InputEvent.BUTTON1_MASK)!=0) m += "Button2";
if(e.isAltDown()) m += "ALT key is down";
if(e.isControlDown()) m += "CONTROL key is down";
if(e.isShiftDown()) m += "SHIFT key is down";
repaint();
}
}
public void paint(Graphics g){
g.setFont(new Font("Serif",Font.PLAIN,18));
g.drawString(m, 10, 80);
}
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java In General (Beginner) forum.

Mark
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think when the applet starts, it tries to "paint" itself, but at that point in time, 'm' is still null. So the applet throws a NullPointerException and dies.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, formatting your code will help people who want to try to help you.

[ May 22, 2005: Message edited by: Marilyn de Queiroz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic