简体   繁体   中英

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); is it because i have null at the end of it? 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 . In other words, a simple test would be to print to System.out here is an example:

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

If chImage,when in the output, prints something like AwtImage@17089 , then it is loaded. If if says null in that spot, there is no loaded 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. 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 . If the point is null then trying to access the x and y will throw the NPE.

This suggests that your point imageP is not being set.

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.

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.

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:

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;

Should these be getPoint() as by convention these would not take an agrument but return an object of some type like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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