need help with graphics

we have a group project over inheritance and compostion. and our groupe chose to use graphics instead of millions of polygons. we cant get the pictures to draw. it gives us a error when we exicut the aplet

Exception in thread "AWT-EventQuee-1" java.lang .NullPointer Exception

here is our code

// Lab25st

// This is the student version of the Lab25 assignment.

// Lab 25 is a very open-ended lab assignment. This student version intentionally provides

// little help. It is your job to demonstrate knowledge of inheritance with this assignment.

import java.awt.*;

import java.awt.event.*;

import java.awt.Image.*;

import java.util.ArrayList;

import java.applet.Applet;

publicclass Lab25Gstextends Applet

{

Graphics g;

Image hair,nose,shoes,body,head,arms,legs;

publicvoid init()

{

hair = getImage(getCodeBase(),"hair.gif");

nose = getImage(getCodeBase(),"nose.gif");

shoes = getImage(getCodeBase(),"shoes.gif");

body = getImage(getCodeBase(),"body.gif");

head = getImage(getCodeBase(),"head.gif");

arms = getImage(getCodeBase(),"arms.gif");

legs = getImage(getCodeBase(),"legs.GIF");

}

publicvoid paint(Graphics g)

{

Clown clown =new Clown(g,hair,nose,shoes,body,head,arms,legs);

}

}

class Mammal

{

Body bodyclass;

public Mammal(Graphics g,Image body,Image head,Image arms,Image legs)

{

bodyclass =new Body(g,0,0,body,head,arms,legs);

}

}

class Humanextends Mammal

{

Hair hairclass;

public Human(Graphics g,Image hair,Image body,Image head,Image arms,Image legs)

{

super(g,body,head,arms,legs);

hairclass =new Hair(g,0,0,hair);

}

}

class Hairextends Applet

{

Image hair;

int x,y;

public Hair(Graphics g,int hairx,int hairy,Image hair)

{

x=hairx;

y=hairy;

g.drawImage(hair,x,y,this);

}

}

class Clownextends Human

{

Shoes shoesclass;

Nose noseclass;

public Clown(Graphics g,Image hair,Image nose,Image shoes,Image body,Image head,Image arms,Image legs)

{

super(g,hair,body,head,arms,legs);

shoesclass =new Shoes(g,0,0,shoes);

noseclass =new Nose(g,0,0,nose);

}

}

class Noseextends Applet

{

Image nose;

int x,y;

public Nose(Graphics g,int nosex,int nosey,Image nose)

{

x=nosex;

y=nosey;

g.drawImage(nose,x,y,this);

}

}

class Shoesextends Applet

{

Image shoes;

int x,y;

public Shoes(Graphics g,int shoesx,int shoesy,Image shoes)

{

x=shoesx;

y=shoesy;

g.drawImage(shoes,x,y,this);

}

}

class Bodyextends Applet

{

Image body;

int x,y;

Head headclass;

Arms amrsclass;

Legs legsclass;

public Body(Graphics g,int bodyx,int bodyy,Image body,Image head,Image arms,Image legs)

{

x=bodyx;

y=bodyy;

headclass =new Head(g,0,0,head);

amrsclass =new Arms(g,0,0,arms);

legsclass =new Legs(g,0,0,legs);

g.drawImage(body,x,y,this);

}

}

class Headextends Applet

{

Image head;

int x,y;

public Head(Graphics g,int headx,int heady,Image head)

{

x=headx;

y=heady;

g.drawImage(head,x,y,this);

}

}

class Armsextends Applet

{

Image arms;

int x,y;

public Arms(Graphics g,int armsx,int armsy,Image arms)

{

x=armsx;

y=armsy;

g.drawImage(arms,x,y,this);

}

}

class Legsextends Applet

{

Image legs;

int x,y;

public Legs(Graphics g,int legsx,int legsy,Image legs)

{

x=legsx;

y=legsy;

legs = getImage(getCodeBase(),"legs.GIF");

g.drawImage(legs,x,y,this);

}

}

Message was edited by:

blacklegion

[8194 byte] By [blacklegiona] at [2007-10-3 4:49:06]
# 1
Why do all of your classes extends Applet?
CaptainMorgan08a at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 2
that was the only way i could get it to compile because of the draw image
blacklegiona at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 3

This is as close to a complete disaster as I think it could get.

Please scrap this and start over. 25 or whatever number of clases

extending Applet (for no reason) is not the way to do this.

The fact is that even you were helped with this you would still fail because it's just all very wrong.

If you need help coming up with a design then post your requirements

along with what you currently think you should do to implement them and we can work from there.

cotton.ma at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 4

I'm pretty certain you weren't supposed to make everything extend Applet.

For one thing, drawImage doesn't require you to extend Applet. It's the getImage stuff. But you really don't need to extend Applet to do a getImage. Just use a Toolkit object.

Secondly, I'm 99% sure (the other 1% is in case the teacher is a *****) that you were supposed to create a general class for all things you're going to draw, and then have the various parts extend that.

paulcwa at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 5
we have to show 3 levels of compostion and 3 level of inheratance and our teacher wanted us to use a bunch of different clasesMessage was edited by: blacklegion
blacklegiona at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 6

> we have to show 3 levels of compostion and 3 level of

> inheratance and our teacher wanted us to use a bunch

> of different clases

Which you're not doing because everything extends Applet.

What is a Clown? Is it a kind of monkey or a kind of human? What is a human? Is it a kind of mammal or a kind of reptile?

paulcwa at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 7
not everything extends appletclow extends human and human extends mammal
blacklegiona at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 8
Whoops.Anyway, so there you go. Do the same thing with clothes and body parts.And composition: my guess is that you're supposed to compose the objects about living things from objects about parts and clothes.
paulcwa at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 9
i have the requirements the problem is when i try to exicute the applet it doesnt draw the images
blacklegiona at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 10

That's because you're not actually doing any painting. In your paint() method, all you do is instantiate a Clown object.

A more normal approach would be to create the Clown object in your constructor (or init method, for an applet) and then drawn it in your paint method. For example, your paint method might consist only of this:

public void paint(Graphics g) {

myClown.drawSelf(g);

}

By the way why are you making this as an applet? Arguably that only makes it more complicated at this stage. It would be easier just to make it a regular standalone app.

paulcwa at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 11
You shouldn't be passing the Graphics object into the constructor, either.
paulcwa at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 12
so how should i get the images into each class?
blacklegiona at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 13

when i moves clown myclown to the init method i got this error during exicution

java.lang.NullPointerException

at Head.<init><lab25gst.java:123>

at Body.<init><lab25gst.java:107>

at Mammal.<init><lab25gst.java:37>

and i use applets cause thats all i know how to use cause our teacher hasnt taught us anything else

blacklegiona at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 14

> when i moves clown myclown to the init method i got

> this error during exicution

>

> java.lang.NullPointerException

>at Head.<init><lab25gst.java:123>

> at Body.<init><lab25gst.java:107>

>at Mammal.<init><lab25gst.java:37>

Well, what's on line 123?

paulcwa at 2007-7-14 22:53:36 > top of Java-index,Java Essentials,Java Programming...
# 15

> so how should i get the images into each class?

The Graphics object isn't what you should use (or can use?) to load images, if that's what you're saying.

Like I said you can use Toolkit to load images. Or the applet itself -- just the single applet, not every single class extending applet -- can be used to do so.

paulcwa at 2007-7-21 10:44:33 > top of Java-index,Java Essentials,Java Programming...
# 16
g.drawImage(head,x,y,this);
blacklegiona at 2007-7-21 10:44:33 > top of Java-index,Java Essentials,Java Programming...
# 17

Well, don't try to draw things in your constructor. Like I hinted at before, create a drawSelf method for all drawable objects. Pass a Graphics object to that, and do your drawing there.

The whole point of object oriented programming is that objects represent a bundle of state and operations on that state. It doesn't make sense to create an object just so its constructor will draw something as a side effect.

paulcwa at 2007-7-21 10:44:33 > top of Java-index,Java Essentials,Java Programming...