Null pointer exception

i am getting a run time exception in the following program that says:

Exception in thread "main" java.lang.NullPointerException

at Home1.launchframe(Home1.java:58)

at Home1.main(Home1.java:77)

i would be extremely grateful if you could tell me where i have gone wrong.

import java.awt.*;

import java.awt.event.*;

public class Home1

{

private Frame f;

private Button b1,b2;

private Panel p;

public Home1()

{

Frame f=new Frame("custom");

p=new Panel();

b1=new Button("laugh");

b2=new Button("cry");

}

class painting extends Canvas

{

private int w;

private int h;

public void paint(Graphics d)

{

d.setColor(Color.YELLOW);

w=getWidth();

h=getHeight();

d.fillOval(w/6,h/8,2*w/3,3*h/4);

d.setColor(Color.BLACK);

d.drawOval(w/3,5*h/24,w/8,h/4);

d.drawOval(11*w/20,5*h/24,w/8,h/4);

int[] x;

int[] y;

x=new int[3];

y=new int[3];

x[0]=w/2;

x[1]=9*w/20;

x[2]=11*w/20;

y[0]=3*h/8;

y[1]=5*h/8;

y[2]=5*h/8;

d.drawPolygon(x,y,3);

d.drawLine(5*w/12,2*h/3,7*w/12,2*h/3);

d.drawArc(5*w/12,2*h/3-h/22,w/6,h/12,0,-180);

d.drawLine(28*w/60+w/100,h/2,32*w/60-w/100,h/2);

d.drawLine(57*w/120,5*h/12+h/15,63*w/120,5*h/12+h/15);

d.fillArc(w/3,h/4,w/8,h/5+h/60,0,-180);

d.fillArc(w/3,h/3-h/27,w/8,h/8,0,180);

d.fillArc(11*w/20,h/4,w/8,h/5+h/60,0,-180);

d.fillArc(11*w/20,h/3-h/27,w/8,h/8,0,180);

d.fillOval(19*w/40,h/6,w/20,h/20);

d.drawLine(w/2,3*h/20,w/2,7*h/30);

d.drawLine(19*w/40-w/60,23*h/120,19*w/40+w/15,23*h/120);

}}

public void launchframe()

{

f.setBackground(Color.BLUE);

f.setLayout(new BorderLayout());

p.setLayout(new FlowLayout());

painting w=new painting();

w.repaint();

f.add(w,BorderLayout.CENTER);

p.add(b1);

p.add(b2);

f.add(p,BorderLayout.SOUTH);

f.pack();

f.setVisible(true);

}

public static void main(String[] args) {

Home1 s=new Home1();

s.launchframe();

}

}

thanking you,

neavin

[2246 byte] By [neavin_samuela] at [2007-11-27 9:57:20]
# 1
Either f, p or w is null and you're trying to call a method on that null reference. This happens on (your) line 58.
prometheuzza at 2007-7-13 0:27:36 > top of Java-index,Java Essentials,Java Programming...
# 2

public class Home1

{

private Frame f;

private Button b1,b2;

private Panel p;

public Home1()

{

Frame f=new Frame("custom");

p=new Panel();

b1=new Button("laugh");

b2=new Button("cry");

}

// etc

In the first line of your constructor you declare and initialise another Frame f that masks the class member variable. Later (on line 58) when you try and use f it will be null. Hence the error.

pbrockway2a at 2007-7-13 0:27:36 > top of Java-index,Java Essentials,Java Programming...