简体   繁体   中英

java.lang.NullPointerException when doing .getImage

I have been searching this site and Google for days and tearing my hair out wondering why this isn't working. I am making a 2D rpg style game like FFII and am able to make a character walk around the screen using different images depending on the direction and the code worked fine until I put the Image[] in a new class to make it more usable for later coding. Here is the part of the code that works perfectly:

package alpharpg;

import java.awt.Image;
import javax.swing.ImageIcon;


public class AlphaCharacterSheet extends AlphaConstants{                

    Image[] avatar;

public AlphaCharacterSheet() {      

            avatar = new Image[NUMOFAVATARS];
}

public void GetCharacter (int c){

        switch (c){
                case BARD:

                    ImageIcon tempii;


                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKMENU));
                    avatar[MENUAVATAR] = tempii.getImage();
                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKBATTLE));
                    avatar[BATTLEAVATAR] = tempii.getImage();
                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKFRONT));
                    avatar[FRONTAVATAR] = tempii.getImage();
                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKBACK));
                    avatar[BACKAVATAR] = tempii.getImage();
                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKLEFT));
                    avatar[LEFTAVATAR] = tempii.getImage();
                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKFRONT));
                    avatar[FRONTWALKAVATAR] = tempii.getImage();
                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKBACK));
                    avatar[BACKWALKAVATAR] = tempii.getImage();
                    tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKLEFT));
                    avatar[LEFTWALKAVATAR] = tempii.getImage();


                default:
                break;
            }

    }

The call to GetCharacter(BARD) works and in paint() I draw it with

drawImage(party.players[inputcounter].avatar[currentzone.avatarfacing], currentzone.avatarx, currentzone.avatary, null);

HOWEVER even though this works perfectly when I put it into

public Image[] img;

public void AlphaAvatar(){

    img = new Image[NUMOFAVATARS];

}

public void SetAvatar (int avatarid){


    switch (avatarid){

        case BARD:

        ImageIcon tempii;
        tempii = new ImageIcon(this.getClass().getResource(FRANTIKBATTLE));
        img[BATTLEAVATAR] = tempii.getImage();
        tempii = new ImageIcon(this.getClass().getResource(FRANTIKFRONT));
        img[FRONTAVATAR] = tempii.getImage();
        tempii = new ImageIcon(this.getClass().getResource(FRANTIKBACK));
        img[BACKAVATAR] = tempii.getImage();
        tempii = new ImageIcon(this.getClass().getResource(FRANTIKLEFT));
        img[LEFTAVATAR] = tempii.getImage();
        tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKFRONT));
        img[FRONTWALKAVATAR] = tempii.getImage();
        tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKBACK));
        img[BACKWALKAVATAR] = tempii.getImage();
        tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKLEFT));
        img[LEFTWALKAVATAR] = tempii.getImage();

Now in CharacterSheet I have AlphaAvatar avatar;

In CharacterSheet() I have avatar = new AlphaAvatar();

Then when GetCharacter(BARD) is called I make a call to avatar.SetAvatar(BARD) and it gives the error at the first img[] = tempii.getImage();

Any help as to why I can load the images through one class but not the other would be greatly appreciated.

Seems like your ImageIcon (tempii) is null. Have you tried stepping through your code with a debugged to check the state of your objects?

Replace the

public void AlphaAvatar(){

    img = new Image[NUMOFAVATARS];

}

by

public AlphaAvatar(){

    img = new Image[NUMOFAVATARS];

}

public void AlphaAvatar() is not a constructor, it is a method called AlphaAvatar that returns nothing... It explains why img was not initialized in it.

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