-3

I am getting a NullPointerException running my java code.

Both lines in this method are getting the error:

    public void draw(Graphics g){
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

bg is declared like so:

 bg = new ImageIcon("D:\\Eclipse\\Workspace\\Game\\src\\GameTest1\\back.png").getImage();

a is this:

 private Animations a;

code:

package GameTest1;

import java.awt.Image;
import java.util.ArrayList;

public class Animations {

    private ArrayList scenes;
    private int sceneIndex;
    private long movieTime;
    private long totalTime;

    //CONSTRUCTOR
    public Animations(){
        scenes = new ArrayList();
        totalTime = 0;
        start();
    }
    //adds screen to array list and adds time to things
    public synchronized void addScene(Image i, long t){ 
        totalTime += t;
        scenes.add(new OneScene(i, totalTime) );
    }
    //start animation from beginning
    public synchronized void start(){
        movieTime = 0;
        sceneIndex = 0;
    }
    //change scenes
    public synchronized void update(long timePassed){
        if(scenes.size() > 1){
            movieTime += timePassed;
            if(movieTime >= totalTime){
                movieTime = 0;
                sceneIndex = 0;
            }
            while(movieTime > getScene(sceneIndex).endTime){
                sceneIndex++;
            }
        }
    }
    //get current scene(image)
    public synchronized Image getImage(){
        if(scenes.size() == 0){
            return null;
        }else{
            return getScene(sceneIndex).pic;
        }
    }
    //get scene
    private OneScene getScene(int x){
        return (OneScene)scenes.get(x);
    }
    /////////////PRIVATE CLASSCEPTION///////////////
    private class OneScene{
    Image pic;
    long endTime;

    public OneScene(Image pic, long endTime){
        this.pic = pic;
        this.endTime = endTime;
    }

}
}

I know what a nullPointerException is, so please do not link me to a post describing what one is. My problem is I can't see what is causing the error.

user3532547
  • 87
  • 1
  • 11
  • 1
    Where's the stacktrace? And what is `a`? – Justin Jasmann May 18 '14 at 01:10
  • `"I know what a nullPointerException is, so please do not link me to a post describing what one is."` -- If you know what a NPE is, then you should know already how to debug it. Have you tested to see just what is null? The key may be the Graphics object -- how are you getting it? – Hovercraft Full Of Eels May 18 '14 at 01:10
  • @JustinJasmann private Animations a; and the Animations class is now in the post – user3532547 May 18 '14 at 01:12
  • You're posting a lot of code, but none relevant to your problem. Again, how are you calling draw, where are you getting the Graphics object? Again, have you tested to see just what is null? Please let's avoid having this question closed for insufficient information. – Hovercraft Full Of Eels May 18 '14 at 01:17
  • @HovercraftFullOfEels Justin asked for the class, I'm calling draw with the CLASSNAME.run() at the top of my main class, and what do you mean by"where am I getting the Graphics object" – user3532547 May 18 '14 at 01:20

2 Answers2

3

You said:

Both lines in this method are getting the error:

public void draw(Graphics g){
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

This very clearly means that the Graphics object that you are passing to the draw() method, is null. Check the line of code that is calling draw(), and see why the Graphics object you are passing to it is null.

You should really read the answers to the post this question will inevitably be marked as a duplicate of. You have not yet learned how to debug a NPE.

Community
  • 1
  • 1
arserbin3
  • 6,010
  • 8
  • 36
  • 52
2

You are calling:

public void draw(Graphics g){
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

And getting a NullPointerException. First you must find the line throwing the NPE, and then test to see what is null. For example if it is the first line of the method:

public void draw(Graphics g){
    System.out.println("is g null? " + (g == null));
    System.out.println("is bg null? " + (bg == null));
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

Then if g is null, look back at where draw is called and see how you get the Graphics object.


Edit
You state:

Okay, found where I'm calling draw: Graphics2D g = screen.getGraphics(); draw(g);

What is screen? If it's a component, then you're trying to get the Graphics out of it before it has been rendered, and it will be null. That's not how you use Graphics anyway. You should instead be drawing in the paintComponent method of a JPanel or JComponent override as per the Swing painting tutorials.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373