我做了一類叫做樹這個方法:不能使打印Methord另一個類,並調用它的主類
public void printTree(Graphics g, int x, int y) {
MediaTracker mt = new MediaTracker(this);
tree = getImage(getCodeBase(), "tree.png");
mt.addImage(tree, 0);
g.drawImage(tree, x, y, this);
我嘗試在paint方法來使用它在主:
Tree tree = new Tree();
tree.printTree(g, 60, 50);
我沒有錯誤,但是當我運行該應用程序我得到這些錯誤:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Unknown Source)
at Tree.printTree(Tree.java:12)
at Main.paint(Main.java:146)
at Main.update(Main.java:187)
at sun.awt.RepaintArea.updateComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
如果有人能幫助我,我會版本很高興。
我們可以看到'getCodeBase()'的內容嗎? – mre 2011-06-14 17:17:53
這就是Java文檔告訴我:URL java.applet.Applet.getCodeBase() getCodeBase 公共URL getCodeBase() 獲得基URL。這是包含此applet的目錄的URL。 返回: 包含此applet的目錄的基本URL。 另請參見: getDocumentBase() – Derek 2011-06-14 17:21:50