开发者

Cannot make a print Methord in another class and call it in main class

I've made this method in a class called tree:

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);

I try to use it in main in the paint method:

Tree tree = new Tree();
    tree.printTree(g, 60, 50);
开发者_开发技巧

I get no errors, however when I run the application I get these errors:

    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)

If anyone can help me, I would be very pleased.


Applets run in an AppletContext whether provided by the appletviewer or a browser page. If you run an applet from a main method or load the applet into an application there will be no AppletContext and getCodeBase will return null. In this case you can use an alternate/other image loading method.

Reference

Use another way to import a picture into an applet.


There is an object you are attempting to use that is null. Use the debugger and find out which one, and find out how you can ensure that it's not null, or fix the bug that is making it null.


"I get no errors, however when I run the application I get these errors:"

You probably mean you get no compiler errors. NullPointerException is a fairly common but serious error. You must initialize the objects before you use them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜