Need help figuring out why this won run

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deezle
    New Member
    • Nov 2007
    • 2

    #1

    Need help figuring out why this won run

    Hi everyone,
    I am making a quick potato head applet for my java class. I set it up but for some reason it keeps giving me a java.lang.NullP ointerException
    and i dont know what to do. Here is my script and if any fixes or sugestions can be offered it would be appreciated. dont mind that the images are all the same i just havent changed that
    =============== =============== =============== ======

    package potato;

    import java.awt.*;
    import java.awt.event. *;
    import java.applet.*;

    public class PotatoHead
    extends Applet implements ActionListener, ItemListener {
    boolean isStandalone = false;
    Choice hair,hat,glasse s,beard,outfit, accessories;
    Image aHair,bHair,cHa ir,dHair;
    Image aHat,bHat,cHat, dHat;
    Image aBeard,bBeard,c Beard;
    Image aOutfit,bOutfit ,cOutfit;
    Image aAcess,bAcess,c Acess;
    Image aGlass,bGlass;
    int hOption,htOptio n,bOption,oOpti on,aOption,gOpt ion;
    BorderLayout borderLayout1 = new BorderLayout();


    //Construct the applet
    public PotatoHead () {
    }

    //Initialize the applet
    public void init() {
    try {
    jbInit();
    }
    catch (Exception e) {
    e.printStackTra ce();
    }
    }

    //Component initialization
    private void jbInit() throws Exception {
    hair = new Choice();
    hair.add("None" );
    hair.add("Fro") ;
    hair.add("Clown ");
    hair.add("PinkG irl");

    hat = new Choice();
    hat.add("None") ;
    hat.add("Boulde r Derbie");
    hat.add("Wizzar d");
    hat.add("Fez");

    glasses = new Choice();
    glasses.add("No ne");
    glasses.add("Ey ePatch");

    beard = new Choice();
    beard.add("None ");
    beard.add("Hand lebar");
    beard.add("Fuma nChu");

    outfit = new Choice();

    outfit.add("Bik er");
    outfit.add("Fan cy");
    outfit.add("Bar ney");

    accessories = new Choice();
    accessories.add ("None");
    accessories.add ("scar");
    accessories.add ("unibrow");

    hair.addItemLis tener(this);
    add(hair);
    aHair = getImage(getDoc umentBase(),"Ca mpion Start.GIF");
    bHair = getImage(getDoc umentBase(),"bo yHair.gif");
    cHair = getImage(getDoc umentBase(),"bo yHair.gif");
    dHair = getImage(getDoc umentBase(),"bo yHair.gif");

    hat.addItemList ener(this);
    add(hat);
    aHat = getImage(getDoc umentBase(),"WO MANHAIR.GIF");
    bHat = getImage(getDoc umentBase(),"bo yHair.gif");
    cHat = getImage(getDoc umentBase(),"bo yHair.gif");
    dHat = getImage(getDoc umentBase(),"bo yHair.gif");

    beard.addItemLi stener(this);
    add(beard);
    aBeard = getImage(getDoc umentBase(),"WO MANHAIR.GIF");
    bBeard = getImage(getDoc umentBase(),"bo yHair.gif");
    cBeard = getImage(getDoc umentBase(),"bo yHair.gif");


    outfit.addItemL istener(this);
    add(outfit);
    aOutfit = getImage(getDoc umentBase(),"WO MANHAIR.GIF");
    bOutfit = getImage(getDoc umentBase(),"bo yHair.gif");
    cOutfit = getImage(getDoc umentBase(),"bo yHair.gif");


    glasses.addItem Listener(this);
    add(glasses);
    aGlass = getImage(getDoc umentBase(),"WO MANHAIR.GIF");
    bGlass = getImage(getDoc umentBase(),"bo yHair.gif");

    accessories.add ItemListener(th is);
    add(accessories );
    aAcess= getImage(getDoc umentBase(),"WO MANHAIR.GIF");
    bAcess = getImage(getDoc umentBase(),"bo yHair.gif");
    cAcess = getImage(getDoc umentBase(), "boyHair.gi f");




    }

    //Get Applet information
    public String getAppletInfo() {
    return "Applet Information";
    }
    public void paint(Graphics g){
    //=============== =============== =============== ========
    // Hair Option
    //=============== =============== =============== ========
    if (hOption == 1){
    g.drawImage(aHa ir, 50, 50, 100, 100, this);
    }
    if (hOption == 2){
    g.drawImage(bHa ir, 50, 50, 100, 100, this);
    }
    if(hOption == 3){
    g.drawImage(aHa ir, 50, 50, 100, 100, this);
    }
    if (hOption == 4){
    g.drawImage(bHa ir, 50, 50, 100, 100, this);
    }
    //=============== =============== =============== =============
    // Hat Options
    //=============== =============== =============== =============
    if (htOption == 1){
    g.drawImage(aHa t, 50, 50, 100, 100, this);
    }
    if (htOption == 2){
    g.drawImage(bHa t, 50, 50, 100, 100, this);
    }
    if(htOption == 3){
    g.drawImage(cHa t, 50, 50, 100, 100, this);
    }
    if (htOption == 4){
    g.drawImage(dHa t, 50, 50, 100, 100, this);
    }
    //=============== =============== =============== =============
    // Glasses Option
    //=============== =============== =============== =============
    if (gOption == 1){
    g.drawImage(aGl ass, 50, 50, 100, 100, this);
    }
    if (gOption == 2){
    g.drawImage(bGl ass, 50, 50, 100, 100, this);
    }
    //=============== =============== =============== =============
    // Beard Options
    //=============== =============== =============== =============
    if (bOption == 1){
    g.drawImage(aBe ard, 50, 50, 100, 100, this);
    }
    if (bOption == 2){
    g.drawImage(bBe ard, 50, 50, 100, 100, this);
    }
    if(bOption == 3){
    g.drawImage(cBe ard, 50, 50, 100, 100, this);
    }
    //=============== =============== =============== =============
    // Accessories Options
    //=============== =============== =============== =============
    if (aOption == 1){
    g.drawImage(aAc ess, 50, 50, 100, 100, this);
    }
    if (aOption == 2){
    g.drawImage(bAc ess, 50, 50, 100, 100, this);
    }
    if(aOption == 3){
    g.drawImage(cAc ess, 50, 50, 100, 100, this);
    }
    //=============== =============== =============== =============
    // Outfit Options
    //=============== =============== =============== =============
    if (oOption == 1){
    g.drawImage(aOu tfit, 150, 150, 100, 100, this);
    }
    if (oOption == 2){
    g.drawImage(bOu tfit, 150, 150, 100, 100, this);
    }
    if(oOption == 3){
    g.drawImage(cOu tfit, 150, 150, 100, 100, this);
    }

    }




    //Main method
    public static void main(String[] args) {
    PotatoHead applet = new PotatoHead();
    applet.isStanda lone = true;

    Frame frame;
    frame = new Frame();
    frame.setTitle( "Applet Frame");

    frame.add(apple t, BorderLayout.CE NTER);

    applet.init();
    applet.start();
    frame.setSize(6 00, 600);
    Dimension d = Toolkit.getDefa ultToolkit().ge tScreenSize();
    frame.setLocati on( (d.width - frame.getSize() .width) / 2,
    (d.height - frame.getSize() .height) / 2);
    frame.setVisibl e(true);
    }

    public void itemStateChange d(ItemEvent e) {

    // Hair
    if (e.getSource()= = hair){
    String hairChoice = e.getItem().toS tring();
    if(hairChoice.e quals("None")){
    hOption=1;
    }
    if(hairChoice.e quals("Fro")){
    hOption=2;
    }
    if(hairChoice.e quals("Clown")) {
    hOption=3;
    }
    if(hairChoice.e quals("PinkGirl ")){
    hOption=4;
    }
    // Hat
    if (e.getSource()= = hat){
    String hatChoice = e.getItem().toS tring();
    if (hatChoice.equa ls("None")) {
    htOption = 1;
    }
    if (hatChoice.equa ls("BoulderDerb ie")) {
    htOption = 2;
    }
    if (hatChoice.equa ls("Wizzard")) {
    htOption = 3;
    }
    if (hatChoice.equa ls("Fez")) {
    htOption = 4;
    }
    }
    // Glasses
    if (e.getSource()= = glasses){
    String glassesChoice = e.getItem().toS tring();
    if(glassesChoic e.equals("None" )){
    gOption=1;
    }
    if(glassesChoic e.equals("EyePa tch")){
    htOption=2;
    }
    }
    // Beard
    if (e.getSource()= = beard){
    String beardChoice = e.getItem().toS tring();
    if(beardChoice. equals("None")) {
    bOption=1;
    }
    if(beardChoice. equals("Handleb ar")){
    bOption=2;
    }
    if(beardChoice. equals("FumanCh u")){
    bOption=3;
    }
    }
    // Outfit
    if (e.getSource()= = outfit){
    String outfitChoice = e.getItem().toS tring();
    if(outfitChoice .equals("Biker" )){
    oOption=1;
    }
    if(outfitChoice .equals("Fancy" )){
    oOption=2;
    }
    if(outfitChoice .equals("Barney ")){
    oOption=3;
    }
    }
    // Acessesories
    if (e.getSource()= = accessories){
    String accessoriesChoi ce = e.getItem().toS tring();
    if(accessoriesC hoice.equals("N onw")){
    aOption=1;
    }
    if(accessoriesC hoice.equals("s car")){
    aOption=2;
    }
    if(accessoriesC hoice.equals("u nibrow")){
    aOption=3;
    }
    }







    }
    repaint();
    }

    public void actionPerformed (ActionEvent e) {
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code
    2.) I could look at your code and tell you which variable is null and being derefenced but better is that you look at your stacktrace again. It tells you the exact line number where the exception was thrown.

    Comment

    Working...