简体   繁体   English

我收到错误“ AWT-EventQueue-0” java.lang.NullPointerException,我不明白为什么在我的上下文中

[英]I get the error “AWT-EventQueue-0” java.lang.NullPointerException and i do not understand why in my context

I am making a game and have a class to print out everything on the screen. 我正在做一个游戏,有一堂课可以打印屏幕上的所有内容。 I get an error on this line: g.drawImage(chImage, imageP.x*scale, imageP.y*scale, null); 我在这条线上收到错误: g.drawImage(chImage, imageP.x*scale, imageP.y*scale, null); is it because i have null at the end of it? 是因为我在结尾处添加了null吗? I have looked up this error and tried to figure it and i understand that something is null in that line but i can not figure out what is null or how to fix that. 我已经查找了此错误并试图找出它,但我知道该行中的某些内容为空,但是我无法弄清什么为空或如何解决。 If you need me to post the other classes i can. 如果您需要我张贴其他课程,我可以。 From my google searches so far i think that it might have something to do with the way that my variables are initialized. 从我到目前为止的谷歌搜索,我认为这可能与初始化变量的方式有关。

package Game;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Printer extends JPanel{
private Map map;
private Character ch;
private int scale;
private Point imageP;
private BufferedImage chImage;
private ArrayList<Rectangle> part;

public Printer(int scale1){
    scale = scale1;
    map = new Map();
    ch = new Character();
    imageP = ch.getPoint();
    chImage = ch.getImage();
    part = map.setPart();
}


protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawImage(chImage, imageP.x*scale, imageP.y*scale, null);

    for(int i=0; i<part.size(); i++){
        Rectangle temp = new Rectangle(part.get(i));
        g.drawRect(temp.x, temp.y, temp.width, temp.height);
        g.fillRect(temp.x, temp.y, temp.width, temp.height);
    }


}

}


package Game;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

//Creates a image for a character and allows him to walk around the screen
public class Character extends JPanel{

private BufferedImage image;
private Point imageP;
private int speed;
private int scale;
private int width;
private int height;

public Character(){

}

public Character(int x, int y, int scale1, int w, int h){
   super();
   try {
        image = ImageIO.read(new File("F:\\Programming\\Final Project\\Top_down\\narwhal.png"));
   } catch (IOException ex) {

   }
   scale = scale1;
   imageP = new Point(x,y);
   speed = 10;
   width = w;
   height = h;
   addKeyListener(new KeyAdapter(){
       @Override
   public void keyPressed(KeyEvent evt){
           moveIt(evt);
       }
   });
}


public void moveIt(KeyEvent evt){
    switch(evt.getKeyCode()){
    case KeyEvent.VK_S:
        if(imageP.y <= height-33)
            imageP.y += 1*speed;
        break;
    case KeyEvent.VK_W:
        if(imageP.y >=0+5)
            imageP.y -= 1*speed;
        break;
    case KeyEvent.VK_A:
        if(imageP.x >=0+5)
            imageP.x -= 1*speed;
        break;
    case KeyEvent.VK_D:
        if(imageP.x <= width-30)
            imageP.x += 1*speed;
        break;
    }
    repaint();
}


public Point getPoint(){
    //g.drawImage(image, imageP.x*scale, imageP.y*scale, null);  IGNORE THIS
    return this.imageP;
}

public BufferedImage getImage(){
    return this.image;
}


}

Make sure your chImage is actually an Image . 确保您的chImage实际上是Image In other words, a simple test would be to print to System.out here is an example: 换句话说,一个简单的测试将是打印到System.out这是一个示例:

    System.out.println("chImage: " + chImage);

If chImage,when in the output, prints something like AwtImage@17089 , then it is loaded. 如果chImage在输出中输出类似AwtImage@17089 ,则将其加载。 If if says null in that spot, there is no loaded Image. 如果在该位置说null,则没有加载的Image。 Check your Image path if that happens, or that the Image name was spelled correctly. 检查您的图像路径是否发生,或者图像名称拼写正确。

How about you do some simple if(component == null) to all of them. 您如何对所有这些对象执行一些简单的if(component == null)。 That way you can see which of them is null. 这样,您可以看到其中哪个为空。 A pretty straight forward solution, in my opinion. 我认为这是一个非常简单的解决方案。

That or you debug your code and check the variable's contents through the Debugger. 那或者您调试代码并通过调试器检查变量的内容。

The NullPointerExcpetion is most likely coming from the statements imageP.x*scale and imageP.y*scale . NullPointerExcpetion最有可能来自语句imageP.x*scaleimageP.y*scale If the point is null then trying to access the x and y will throw the NPE. 如果该点为空,则尝试访问x和y将抛出NPE。

This suggests that your point imageP is not being set. 这表明未设置您的点imageP

I see in your Printer method you have 我在您的打印机方法中看到

ch = new Character();
imageP = ch.getPoint();
chImage = ch.getImage();

So your default constructor for a Character does not set the point and image (or anything in the Character class) so your getters will return null, meaning the point is null. 因此,您默认的Character构造函数不会设置点和图像(或Character类中的任何内容),因此您的吸气剂将返回null,这意味着该点为null。

You need to either call the constructor of your Character class that does all this or within your default constructor Character() call the other constructor with some default values. 您需要调用完成所有这些操作的Character类的构造函数,或者在默认构造函数内, Character()调用具有某些默认值的另一个构造函数。

Either in Printer do this 在打印机中都可以执行此操作

ch = new Character(0,0,1,10,10); //Or what ever defaults
imageP = ch.getPoint();
chImage = ch.getImage();

On in Character change your default Constructor 在“字符”中,更改默认的构造函数

public Character(){
    this(0,0,1,10,10);
}

Just a note on getters and setters: 只是关于获取器和设置器的注释:

Convention for methods with these names would make the type of these method be void and accept an argument like: 具有这些名称的方法的约定会使这些方法的类型为void并接受类似以下的参数:

public void setPoint(Point p)
{
    this.characterPoint = p;
}

This would then give you a compilation error as if you try to call the set method and assign it variable then you know there is something wrong; 然后,这将给您带来编译错误,就好像您尝试调用set方法并将其分配给变量一样,那么您就知道出了点问题。

Should these be getPoint() as by convention these would not take an agrument but return an object of some type like 如果按照约定,它们应该是getPoint() ,则它们不会花费太多时间,但会返回某种类型的对象,例如

public Image getImage()
{
    return this.characterImage;
}

暂无
暂无

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

相关问题 我在线程“AWT-EventQueue-0”java.lang.NullPointerException 错误中遇到异常。 我应该怎么办? - I got Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Error. What should I do? “AWT-EventQueue-0”java.lang.NullPointerException错误的原因 - Reason for “AWT-EventQueue-0” java.lang.NullPointerException error 错误:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - ERROR : Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Jframe错误:异常:“ AWT-EventQueue-0” java.lang.NullPointerException - Jframe Error: Exception: “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException-我应该重置数组吗? 如何? - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException - Should I Reset Array? and How? 在线程“AWT-EventQueue-0”java.lang.NullPointerException中我被困在这个问题上几天Exception - I've been stuck on this issue for days Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 我是Java的新手,并尝试使用JFrameb添加图像,但在线程“ AWT-EventQueue-0” java.lang.NullPointerException中得到错误Exception - I am new to java and trying to add an image using JFrameb ut i get the error Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java-线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Getter和Setter Java AWT-EventQueue-0 java.lang.NullPointerException - Getter and Setter Java AWT-EventQueue-0 java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM