为什么我的图像不会加载?

时间:2013-05-03 21:29:29

标签: java bufferedimage ioexception javax.imageio

编辑答案:好的,问题是我的控制器为空。上面的一切工作正常,并正确加载。我得到了nullpointerexception,因为我在调用它以使图像绘制后初始化了我的控制器。感谢DoorKnob和SubOptimal的帮助!

问题:我不能为我的生活,弄清楚为什么我的图像不会加载。无论我做什么,我都会获得IOExceptions。 有人能给我一个关于我做错的提示吗?我将实际图像文件更改为“在此处插入文件”顺便说一句。不应该有所作为

所以在我的gui类中是以下代码,它应该调用图像并在屏幕上显示它。但它没有。它只是输出“无法加载文件”。我假设这个错误是因为文件没有加载,但我不确定,也无法搞清楚。

public void paintComponent(Graphics g) {
                g = frame.getGraphics();
                try {
                    g.drawImage(controller.returnChar().returnImage(0), 100, 100, null);
                } catch (NullPointerException e) {
                    System.out.println("couldnt load file");
                }
            }

下面是图像加载类。

package SimplePlatformer;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class MyCharacter {
    //set the character sprite image here

    private BufferedImage charSprite;
    private int rows = 10;
    private int cols = 10;
    private BufferedImage[] charSpriteFrames = new BufferedImage[rows*cols];
    private int width = 32;
    private int height = 10;
    //for deciding which frames to show
    private boolean isRunning = false;
    private boolean isWalking = true;
    private boolean isJumping = false;
    //for deciding which direction to show
    private boolean goingRight = true;
    //character location
    private int myX = 0;
    private int myY = 600;

    //constructor
    public MyCharacter() throws IOException {
        //set name
        //load the sprite image
        this.charSprite = ImageIO.read(new File("insert any image file here"));
        //set the frame locations in the array
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                charSpriteFrames[(i * cols) + j] = charSprite.getSubimage(
                        j * width,
                        i * height,
                        width,
                        height);
            }
        }
    }

    public BufferedImage returnImage(int x) {
        return charSpriteFrames[x];
    }
}

以下是我在netbeans中获得的输出。

run:
java.lang.NullPointerException
couldnt load file
couldnt load file
    at SimplePlatformer.PlatformerGUI$3.paintComponent(PlatformerGUI.java:76)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1512)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1443)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3877)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    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)
java.lang.NullPointerException
    at SimplePlatformer.PlatformerGUI$3.paintComponent(PlatformerGUI.java:76)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1512)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1443)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3877)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    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)

0 个答案:

没有答案