java.lang.NullPointerException when doing .getImage -
i have been searching site , google days , tearing hair out wondering why isn't working. making 2d rpg style game ffii , able make character walk around screen using different images depending on direction , code worked fine until put image[] in new class make more usable later coding. here part of code 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 getcharacter(bard) works , in paint() draw with
drawimage(party.players[inputcounter].avatar[currentzone.avatarfacing], currentzone.avatarx, currentzone.avatary, null);
however though works when put 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 have alphaavatar avatar;
in charactersheet() have avatar = new alphaavatar();
then when getcharacter(bard) called make call avatar.setavatar(bard) , gives error @ first img[] = tempii.getimage();
any why can load images through 1 class not other appreciated.
replace
public void alphaavatar(){ img = new image[numofavatars]; } by
public alphaavatar(){ img = new image[numofavatars]; } public void alphaavatar() not constructor, method called alphaavatar returns nothing... explains why img not initialized in it.
Comments
Post a Comment