0

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;
}


}
mcgee1095
  • 5
  • 3
  • What are your methods `ch.setPoint();` and `ch.setImage();`, should these be `get`? – Java Devil Jan 09 '14 at 01:47
  • @JavaDevil yes they should be get, sorry i get confused with them sometimes. they just get the points of the character and get the image for the character – mcgee1095 Jan 09 '14 at 01:53
  • Ok can you post your character class then? – Java Devil Jan 09 '14 at 01:59
  • Yup as I suspected. Your default constructor for `Character` does not set any of the Character field. Either call the other constructer in your Printer or within the default constructor – Java Devil Jan 09 '14 at 02:11
  • @JavaDevil this works to get stuff to show up perfectly with no errors thank you, but the only problem is it won't let me move my image anymore. I just added this(50,50,3,300,169) to my character constructor. So shouldn't that be using the keyListener and calling the moveIt method when a button is pressed? – mcgee1095 Jan 09 '14 at 02:47
  • Add a print statement into your key listener method and see. This could be to do with how you are running the game. Make sure you are running it on the EDT, [Like in the question](http://stackoverflow.com/questions/2548262/java-awt-swing-handling-the-event-dispatcher-thread). But what you describe would probably be best suited to a new question. – Java Devil Jan 09 '14 at 03:06

3 Answers3

2

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.

AMDG
  • 1,118
  • 1
  • 13
  • 29
  • However, If Image is null, nothing happens. A NPE is not thrown – Java Devil Jan 09 '14 at 01:39
  • I put in a print statement like you said and it prints out null so it is not in there. In my character class the image is in the image variable there but when i try to send it to the printer class(the class above) it isn't there. I have a simple `public BufferedImage getImage(){ return image; }` but it doesnt seem to be working – mcgee1095 Jan 09 '14 at 01:45
  • 1
    If you try to return an Image, perhaps try casting it off as a BufferedImage. Look at your return type. It says BufferedImage. Also, you have nothing but the word `image` in there so, it isn't going to do anything. Is this a `BufferedImage` outside of the method? – AMDG Jan 10 '14 at 12:52
1

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.

André Silva
  • 1,110
  • 8
  • 24
0

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;
}
Java Devil
  • 10,629
  • 7
  • 33
  • 48