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.NullPointerException
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,glasses,beard,outfit,accessories;
Image aHair,bHair,cHair,dHair;
Image aHat,bHat,cHat,dHat;
Image aBeard,bBeard,cBeard;
Image aOutfit,bOutfit,cOutfit;
Image aAcess,bAcess,cAcess;
Image aGlass,bGlass;
int hOption,htOption,bOption,oOption,aOption,gOption;
BorderLayout borderLayout1 = new BorderLayout();
//Construct the applet
public PotatoHead () {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
hair = new Choice();
hair.add("None");
hair.add("Fro");
hair.add("Clown");
hair.add("PinkGirl");
hat = new Choice();
hat.add("None");
hat.add("Boulder Derbie");
hat.add("Wizzard");
hat.add("Fez");
glasses = new Choice();
glasses.add("None");
glasses.add("EyePatch");
beard = new Choice();
beard.add("None");
beard.add("Handlebar");
beard.add("FumanChu");
outfit = new Choice();
outfit.add("Biker");
outfit.add("Fancy");
outfit.add("Barney");
accessories = new Choice();
accessories.add("None");
accessories.add("scar");
accessories.add("unibrow");
hair.addItemListener(this);
add(hair);
aHair = getImage(getDocumentBase(),"Campion Start.GIF");
bHair = getImage(getDocumentBase(),"boyHair.gif");
cHair = getImage(getDocumentBase(),"boyHair.gif");
dHair = getImage(getDocumentBase(),"boyHair.gif");
hat.addItemListener(this);
add(hat);
aHat = getImage(getDocumentBase(),"WOMANHAIR.GIF");
bHat = getImage(getDocumentBase(),"boyHair.gif");
cHat = getImage(getDocumentBase(),"boyHair.gif");
dHat = getImage(getDocumentBase(),"boyHair.gif");
beard.addItemListener(this);
add(beard);
aBeard = getImage(getDocumentBase(),"WOMANHAIR.GIF");
bBeard = getImage(getDocumentBase(),"boyHair.gif");
cBeard = getImage(getDocumentBase(),"boyHair.gif");
outfit.addItemListener(this);
add(outfit);
aOutfit = getImage(getDocumentBase(),"WOMANHAIR.GIF");
bOutfit = getImage(getDocumentBase(),"boyHair.gif");
cOutfit = getImage(getDocumentBase(),"boyHair.gif");
glasses.addItemListener(this);
add(glasses);
aGlass = getImage(getDocumentBase(),"WOMANHAIR.GIF");
bGlass = getImage(getDocumentBase(),"boyHair.gif");
accessories.addItemListener(this);
add(accessories);
aAcess= getImage(getDocumentBase(),"WOMANHAIR.GIF");
bAcess = getImage(getDocumentBase(),"boyHair.gif");
cAcess = getImage(getDocumentBase(), "boyHair.gif");
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
public void paint(Graphics g){
//================================================== ===
// Hair Option
//================================================== ===
if (hOption == 1){
g.drawImage(aHair, 50, 50, 100, 100, this);
}
if (hOption == 2){
g.drawImage(bHair, 50, 50, 100, 100, this);
}
if(hOption == 3){
g.drawImage(aHair, 50, 50, 100, 100, this);
}
if (hOption == 4){
g.drawImage(bHair, 50, 50, 100, 100, this);
}
//================================================== ========
// Hat Options
//================================================== ========
if (htOption == 1){
g.drawImage(aHat, 50, 50, 100, 100, this);
}
if (htOption == 2){
g.drawImage(bHat, 50, 50, 100, 100, this);
}
if(htOption == 3){
g.drawImage(cHat, 50, 50, 100, 100, this);
}
if (htOption == 4){
g.drawImage(dHat, 50, 50, 100, 100, this);
}
//================================================== ========
// Glasses Option
//================================================== ========
if (gOption == 1){
g.drawImage(aGlass, 50, 50, 100, 100, this);
}
if (gOption == 2){
g.drawImage(bGlass, 50, 50, 100, 100, this);
}
//================================================== ========
// Beard Options
//================================================== ========
if (bOption == 1){
g.drawImage(aBeard, 50, 50, 100, 100, this);
}
if (bOption == 2){
g.drawImage(bBeard, 50, 50, 100, 100, this);
}
if(bOption == 3){
g.drawImage(cBeard, 50, 50, 100, 100, this);
}
//================================================== ========
// Accessories Options
//================================================== ========
if (aOption == 1){
g.drawImage(aAcess, 50, 50, 100, 100, this);
}
if (aOption == 2){
g.drawImage(bAcess, 50, 50, 100, 100, this);
}
if(aOption == 3){
g.drawImage(cAcess, 50, 50, 100, 100, this);
}
//================================================== ========
// Outfit Options
//================================================== ========
if (oOption == 1){
g.drawImage(aOutfit, 150, 150, 100, 100, this);
}
if (oOption == 2){
g.drawImage(bOutfit, 150, 150, 100, 100, this);
}
if(oOption == 3){
g.drawImage(cOutfit, 150, 150, 100, 100, this);
}
}
//Main method
public static void main(String[] args) {
PotatoHead applet = new PotatoHead();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(600, 600);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( (d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
// Hair
if (e.getSource()== hair){
String hairChoice = e.getItem().toString();
if(hairChoice.equals("None")){
hOption=1;
}
if(hairChoice.equals("Fro")){
hOption=2;
}
if(hairChoice.equals("Clown")){
hOption=3;
}
if(hairChoice.equals("PinkGirl")){
hOption=4;
}
// Hat
if (e.getSource()== hat){
String hatChoice = e.getItem().toString();
if (hatChoice.equals("None")) {
htOption = 1;
}
if (hatChoice.equals("BoulderDerbie")) {
htOption = 2;
}
if (hatChoice.equals("Wizzard")) {
htOption = 3;
}
if (hatChoice.equals("Fez")) {
htOption = 4;
}
}
// Glasses
if (e.getSource()== glasses){
String glassesChoice = e.getItem().toString();
if(glassesChoice.equals("None")){
gOption=1;
}
if(glassesChoice.equals("EyePatch")){
htOption=2;
}
}
// Beard
if (e.getSource()== beard){
String beardChoice = e.getItem().toString();
if(beardChoice.equals("None")){
bOption=1;
}
if(beardChoice.equals("Handlebar")){
bOption=2;
}
if(beardChoice.equals("FumanChu")){
bOption=3;
}
}
// Outfit
if (e.getSource()== outfit){
String outfitChoice = e.getItem().toString();
if(outfitChoice.equals("Biker")){
oOption=1;
}
if(outfitChoice.equals("Fancy")){
oOption=2;
}
if(outfitChoice.equals("Barney")){
oOption=3;
}
}
// Acessesories
if (e.getSource()== accessories){
String accessoriesChoice = e.getItem().toString();
if(accessoriesChoice.equals("Nonw")){
aOption=1;
}
if(accessoriesChoice.equals("scar")){
aOption=2;
}
if(accessoriesChoice.equals("unibrow")){
aOption=3;
}
}
}
repaint();
}
public void actionPerformed(ActionEvent e) {
}
}