File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner class" Watch "inner class" New topic

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();
//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";
public void paint(Graphics g){
g.setFont(new Font("Serif",Font.PLAIN,18));
g.drawString(m, 10, 80);
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17260

Moving to Java In General (Beginner) forum.


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

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

"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

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

[ May 22, 2005: Message edited by: Marilyn de Queiroz ]
wood burning stoves
subject: inner class