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); } }
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: 9033
10
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.