java - 为什么我的代码会产生错误?

标签 java classloader inputstream

* A simple panel for testing various parts of our game.
 * This is not part of the game.  It's just for testing.
 */
package game;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
  * A simple panel for testing various parts of our game.
  * This is not part of the game.  It's just for testing.
  */
public class TestPanel extends JPanel
{
    private static final long serialVersionUID = 1L;  // Ignore this - It's just to get rid of a warning.

    // Instance variable(s).

    private Image backdrop;

    /**
     * Constructor - loads a background image
     */
    public TestPanel ()
    {
        try
        {
             ClassLoader myLoader = this.getClass().getClassLoader();
             InputStream imageStream = myLoader.getResourceAsStream("resources/path_1.jpg");
             backdrop = ImageIO.read(imageStream);

             // You will uncomment these lines when you need to read a text file.

              InputStream pointStream = myLoader.getResourceAsStream("resources/   path_1.txt");
              Scanner s = new Scanner (pointStream);
        }
        catch (IOException e)
        {
            System.out.println ("Could not load: " + e);
        }
    }

    /**
     * This paint meethod draws the background image anchored
     * in the upper-left corner of the panel.  
     */
    public void paintComponent (Graphics g)
    {
        g.drawImage(backdrop, 0, 0, null);        
    }

    /* Override the functions that report this panel's size
     * to its enclosing container. */

    public Dimension getMinimumSize()
    {
        return new Dimension (600, 600);
    }

    public Dimension getMaximumSize()
    {
        return getMinimumSize();
    }

    public Dimension getPreferredSize()
    {
        return getMinimumSize();
    }
}

此代码针对我正在为 Java 类(class)完成的视频游戏作业。这个类仅用于测试我们的代码。在作业的指导中,我被告知要放置 try block 中存在的代码,如上所示。显然,代码应该打开我工作区文件夹中的 JPEG 图像。但是,当我尝试代码时,它只指出:

Exception in thread "main" java.lang.NullPointerException   at
 java.io.Reader.<init>(Unknown Source)  at
 java.io.InputStreamReader.<init>(Unknown Source)   at
 java.util.Scanner.<init>(Unknown Source)   at
 game.TestPanel.<init>(TestPanel.java:43)   at
 game.TestApplication.main(TestApplication.java:24)

我不太清楚 inputStream 和 classLoaders 的作用。因此,如果您对其中任何一个有任何基本信息,那就太好了。另外,我知道构造函数方法下面的其他方法中没有代码。我的作业指示没有说明我应该在这些方法中输入什么内容。

enter code here
enter code here

最佳答案

第二个文件名中有一些额外的空格:

“资源/path_1.txt”

显然这是一个错字。然后,当您使用此流调用 getResourceAsStream 时,由于这些额外的空格,它找不到您想要的文件,因此该调用返回一个空指针,该指针将被传递到扫描仪中,并最终导致NPE。

关于java - 为什么我的代码会产生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696110/

相关文章:

java - 通过代码API访问Spring配置属性元数据

java - 对象数组实例的深拷贝,Java

java - 基于保留参数的切换按钮重新加载同一页面

java - Jena:使用输入流读取模型

java - StreamSource开启时刻

java - Java 中的 Oracle 数据库 BLOB 到 InputStream?

java - Android - 通过相机检测图像处理?

java - Android 多 dex 项目设置

java - Tomcat 8 类加载 - [WEB-INF/lib] 和 [tomcat/lib] 中 JAR 的区别

architecture - Java EE 类加载标准