简体   繁体   English

获取图像时发生NullPointerException

[英]NullPointerException while getting an Image

I am having trouble setting images in my newest game. 我在最新的游戏中无法设置图像。 When I call the method getImage(String) , and I get the Image like so: 当我调用方法getImage(String) ,我像这样得到Image:

Image head = getImage("Torso_model_01.png");

I get the following error message: 我收到以下错误消息:

Err: java.lang.NullPointerException
  At PB2Main.Body(Body.java : 27)
  ...

and so on... 等等...

On this tutorial , it explains how to get an image using ImageIcon like so: 本教程中 ,它说明了如何使用ImageIcon来获取图像,如下所示:

String imgFile = "Images/" + img;
URL imgURL = getClass().getClassLoader().getResource(imgFile);
ImageIcon imageIcon;
Image image;
 if(imgURL != null){
  imageIcon = new imageIcon(imgURL);
  image = imageIcon.getImage();
 }

 final Image anImage = image;

I made a method for this: 我为此做了一个方法:

public URL getURL(String img){
  String imgFile = "Images/" + img;
  URL imgURL = getClass().getClassLoader().getResource(imgFile);
  return imgURL;
}

Then I made a method called getImage(String) 然后我做了一个叫做getImage(String)的方法

public Image getImage(String img) {
  ImageIcon imageIcon;
  Image image;
  URL imgURL = getClass().getClassLoader().getResource(getURL(img));
  if(imgURL != null){
   imageIcon = new ImageIcon(imgURL);
   image = imageIcon.getImage();
   return image;
  }
   System.err.println("Unable to Locate Image: " + imgURL);
}

Now, I have a class called Body . 现在,我有一个名为Body的课程。 In that class, I have a constructor: 在该类中,我有一个构造函数:

public Body(float x, float y, String head, String torso){//atm this is just so i can                    get the image to actually draw on the screen
Image Head = debugger.getImage(head);// debugger doubles as a library and debugger
//i also can't have this class extend debugger otherwise it will create a window :/
// is that a glitch or something in Java? :L perhaps i just don't understand
// inheritance very well and what happens exactly when you inherit a class :(
Image Torso = debugger.getImage(torso);

 g2.drawImage(Head, canvas.geWidth()/ 2,canvas.getHeight()/2, null)// canvas: the window to  
 //draw to
 // can someone also quickly explain in their answer what an image observer is please?
 g2.drawImage(Torso, Head.getX() - 5, Head.getY() - 5, null);
}

The compiler gives me the following error message: 编译器给我以下错误消息:

java.lang.NullPointerException
At PlazmaBurst2.Body(Body.java: 37)

//the code it brings me to is line 1 in the constructor:
/* null: */ Image Head = debugger.getImage(img);

I don't understand where this NullPointerException is coming from. 我不明白此NullPointerException的来源。 I did it exactly how they do it in the Custom Graphics Programming section of the same site . 我完全按照他们在同一站点的“自定义图形编程”部分中操作来做的。

The code works fine if I just copy and paste the code, but not if I use the method getImage(String) . 如果仅复制并粘贴代码,则代码工作正常,但如果使用方法getImage(String)则代码无法正常工作。

You're problem is on line 3 of getImage(String) : 您的问题是在getImage(String)第3行上:

URL imgURL = getClass().getClassLoader().getResource(getURL(img));

This should be changed to: 这应该更改为:

URL imgURL = getURL(img);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM