This discussion is archived
7 Replies Latest reply: Apr 29, 2011 1:17 AM by gimbal2 RSS

NullPointerException problem 2

804374 Newbie
Currently Being Moderated
From my previous post, I fixed a few bugs and my application worked. But now, instead of using animated GIF images used from Fireworks, I try to create an animation using java. But I got an error when I ran my program, that said
Exception in thread "main" java.lang.NullPointerException
     at Mario.DefineMoves.<init>(DefineMoves.java:23)
     at helper3.MainConfig.<init>(MainConfig.java:28)
     at helper3.Helper3.<init>(Helper3.java:14)
     at helper3.Helper3.main(Helper3.java:26)
This is what Mario.DefineMoves.<init>(DefineMoves.java:23) says.
public DefineMoves() {
        CurrentFrame = IRframes.getImage();///This is line 23
        x = Sprite.x = 200;
        y = Sprite.y = 200;
    }
IRframes comes from these classes.
public class GetImages {

    public ArrayList GetFrames;
    public Animation IRframesAnim;
    public Sprite IRframes;

    public void IdleRight() {

        Image IRF1 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame1.png").getImage();
        Image IRF2 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame2or4.png").getImage();
        Image IRF3 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame3.png").getImage();
        Image IRF4 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame2or4.png.png").getImage();
        Image IRF5 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame5.png").getImage();

        IRframesAnim = new Animation();
        IRframesAnim.addFrame(IRF1, 100);
        IRframesAnim.addFrame(IRF2, 100);
        IRframesAnim.addFrame(IRF3, 100);
        IRframesAnim.addFrame(IRF4, 100);
        IRframesAnim.addFinalFrame(IRF5, 100, 5, 500);

        IRframes = new Sprite(IRframesAnim);

    }
    public Animation ILframesAnim;
    public Sprite ILframes;

    public void IdleLeft() {
        Image ILF1 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame1.png").getImage();
        Image ILF2 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame2or4.png").getImage();
        Image ILF3 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame3.png").getImage();
        Image ILF4 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame2or4.png.png").getImage();
        Image ILF5 = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario_V2\\Idle\\IdleRight\\Frame5.png").getImage();

        ILframesAnim = new Animation();
        ILframesAnim.addFrame(ILF1, 100);
        ILframesAnim.addFrame(ILF2, 100);
        ILframesAnim.addFrame(ILF3, 100);
        ILframesAnim.addFrame(ILF4, 100);
        ILframesAnim.addFinalFrame(ILF5, 100, 5, 500);

        ILframes = new Sprite(ILframesAnim);

    }
}
which is built from
public class Animation {
    ///For timer

    public int TimePerFrame;
    public int MaxTime;
    public int AnimTime;
    public Timer timer;
    ///For Frames
    public ArrayList Frames;
    public int FrameIndex;
    public int MaxFrames;
    public int CurrentTime;
    public int Seconds;

    public Animation() {
        Frames = new ArrayList();
        TimePerFrame = 0;
        MaxTime = 0;
        start();
    }

    public void addFrame(Image frame, int TPF) {
        Frames = new ArrayList();
        TimePerFrame += TPF;
        Frames.add(new OneFrame(frame, TimePerFrame));

    }

    public void addFinalFrame(Image frame, int TPF, int MaxF, int MaxT) {
        TimePerFrame += TPF;
        MaxFrames += MaxF;
        MaxTime += MaxT;
        Frames = new ArrayList();
        Frames.add(new OneFrame(frame, TimePerFrame, MaxFrames, MaxTime));

    }

    public void start() {
        FrameIndex = 0;
        Seconds = 0;
    }

    public synchronized Image getImage() {
        if (Frames.size() < 0) {
            return null;
        } else {
            return getOneFrame(FrameIndex).Frame;
        }
    }

    public synchronized void whichFrame() {
        new SpriteTimer(AnimTime);
        TimePerFrame = getOneFrame(FrameIndex).endtime;
        CurrentTime = Seconds;
        if (Frames.size() > 1) {
            if (CurrentTime > 0 && CurrentTime < TimePerFrame) {
                FrameIndex = FrameIndex;
            }
            if (CurrentTime > TimePerFrame) {
                FrameIndex++;
                new SpriteTimer(AnimTime);
            }
        }
        if (Frames.size() >= MaxFrames) {
            FrameIndex = 0;
        }
    }

    public OneFrame getOneFrame(int x) {
        return (OneFrame) Frames.get(x);
    }

    public class OneFrame {

        int endtime;
        int Maxframes;
        int MaxTime;
        Image Frame;

        public OneFrame(Image frame, int t) {
            this.Frame = frame;
            this.endtime = t;
        }

        public OneFrame(Image frame, int TPF, int MaxF, int MaxT) {
            this.Frame = frame;
            this.endtime = TPF;
            this.Maxframes = MaxF;
            this.MaxTime = MaxT;
        }
    }

    public class SpriteTimer {

        public SpriteTimer(int seconds) {
            Seconds = seconds * 1000;
            timer = new Timer();
            timer.schedule(new SpriteTimerTask(), Seconds);
        }

        public class SpriteTimerTask extends TimerTask {

            @Override
            public void run() {
                timer.cancel();
            }
        }
    }
}
public class Sprite {

    public Animation a;
    public int x;
    public int y;
    public int dx;
    public int dy;

    public Sprite(Animation a) {
    }

    public void Move() {
        x = x + dx;
        y = y + dy;
    }

    public int getX(int x) {
        return this.x = x;
    }

    public int getY(int y) {
        return this.y = y;
    }

    public int getDX(int dx) {
        return this.dx = dx;
    }

    public int getDY(int dy) {
        return this.dy = dy;
    }

    public int getHeight() {
        return a.getImage().getHeight(null);
    }

    public int getWidth() {
        return a.getImage().getWidth(null);
    }

    public Image getImage() {
        return a.getImage();
    }
}
  • 1. Re: NullPointerException problem 2
    EJP Guru
    Currently Being Moderated
    Exception in thread "main" java.lang.NullPointerException
         at Mario.DefineMoves.<init>(DefineMoves.java:23)
    ...
    CurrentFrame = IRframes.getImage();///This is line 23
    So IRframes is null.
  • 2. Re: NullPointerException problem 2
    804374 Newbie
    Currently Being Moderated
    Thank you for letting me know, but I already know that IRframes is null, sorry for not being direct about my question. But the problem I am trying to solve is, that I already defined the animation and sprite for Marios animation in the GetImages class. But the problem is, that it keeps on saying, that it is null.
  • 3. Re: NullPointerException problem 2
    EJP Guru
    Currently Being Moderated
    It doesn't matter what other code you have written, it is still null. Probably you have declared it but never initialized it.
  • 4. Re: NullPointerException problem 2
    gimbal2 Guru
    Currently Being Moderated
    probably the order of execution is not what you expect. Apparently DefineMoves is constructed BEFORE GetImages.IdleRight() is ever called. Maybe you want to move the code in the DefineMoves constructor to a separate setup method that you can call at a moment where it is safe to do so. You already did something similar for your GetImages class.


    BTW: please read into and use the "java code formatting" style. Variables and methods start with a lower case character, not upper case. That makes it easier to distinguish a classname from a variable or method name for example, its a bit confusing to read through your code as it is. Its usually not a good idea to blindly copy what other people are doing, but in this case it really is!
  • 5. Re: NullPointerException problem 2
    796440 Guru
    Currently Being Moderated
    gimbal2 wrote:
    probably the order of execution is not what you expect. Apparently DefineMoves is constructed BEFORE GetImages.IdleRight() is ever called.
    Another possibility is that they are executed in the expected order, but in different threads. Since the variable is not volatile and there's no synchronization, each thread could be dealing with its own copy. This scenario is less likely, however.
  • 6. Re: NullPointerException problem 2
    morgalr Explorer
    Currently Being Moderated
    From what I see, and I'll admit that I had to get my crystal ball out to see, is you have a simple scope problem. I don't see in your code any place that you get a reference to your intended IRFrames object. While you have declared your object as public in the object you originate it in, GetImages, you do not pass a reference to in for your Mario object's scope to utilize. You can use the same name throughout your projects until hell freezes over, but each object has it's own environment and does not inherit environment, unless programmed to do so--subclass, from other objects in the project. So the senario I believe you have is:
    public class a{
      public Integer i = new Integer(50);
    }
    
    public class b{
      public Integer i;
      int myInt = i.intValue();  //This i is not the same i as in class a
    }
  • 7. Re: NullPointerException problem 2
    gimbal2 Guru
    Currently Being Moderated
    Why do you bother? The OP hasn't posted back in over a week. I am now assuming he already found the problem and is simply too lazy to mention this here, until the OP comes back and proves me wrong.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points