wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner class" Watch "inner class" New topic
Author

inner class

kofi ofei
Ranch Hand

Joined: May 22, 2005
Posts: 39
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

Moving to Java In General (Beginner) forum.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Also, formatting your code will help people who want to try to help you.

[ May 22, 2005: Message edited by: Marilyn de Queiroz ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class