public class KeybroadandmouseDemo {
Frame f = null;
Button b = null;
Desktop dt = null ;
File file = null;
public void init()
{
f = new Frame("my Frame");
file = new File("C:\\Users\\yy\\Desktop\\练习四.txt");
}
public void myEvent() {
// TODO Auto-generated method stub
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b.addMouseListener(new MouseAdapter(){
private int count = 0 ;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入button范围!"+count++);
}
public void mouseClicked(MouseEvent e)
{
System.out.println("我点!开启浏览器!");
try {
dt.open(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void main(String args[]){
KeybroadandmouseDemo make = new KeybroadandmouseDemo();
make.init();
make.myEvent();
}
问题:为什么会报错,应该是我想打开一个文件,但是报错。能告诉我原因和解决方法么?急等作者: The_king丶 时间: 2014-6-24 10:38
本来想打开UC浏览器,但是不行就换了个txt作者: The_king丶 时间: 2014-6-24 10:42
报错:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI.KeybroadandmouseDemo$3.mouseClicked(KeybroadandmouseDemo.java:66)
at java.awt.Component.processMouseEvent(Component.java:6508)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Button.processEvent(Button.java:380)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)