• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inner class

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java In General (Beginner) forum.

Mark
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic