Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nullpointer!!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

can somebody please tell me why i get a nullpointer when i press a button?????
Here is the code:

package opdrachtweek2;

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

public class Applet2 extends Applet {

Rails railsLinks, railsRechts;
Stoplicht stoplicht1, stoplicht2;
Lichtsein lichtseinRood1, lichtseinRood2, lichtseinRood3, lichtseinRood4,
lichtseinGroen1, lichtseinGroen2, lichtseinGroen3, lichtseinGroen4;
//Wissel wisselRechtdoor, wisselAfbuigend;
Button knopRechtdoor, knopAfbuigen;
Graphics g;

public void init()
{
setLayout(null);

railsLinks = new Rails(0,150,0,200,240,10);
railsRechts = new Rails(320,150,240,200,200,10);

stoplicht1 = new Stoplicht(7,7,60,120);
stoplicht2 = new Stoplicht(333,7,60,120);

lichtseinRood1 = new Lichtsein( 12, 13, 50, 50, Color.gray);
lichtseinRood2 = new Lichtsein( 12, 70, 50, 50, Color.red);
lichtseinRood3 = new Lichtsein( 338, 13, 50, 50, Color.gray);
lichtseinRood4 = new Lichtsein( 338, 70, 50, 50, Color.red);

lichtseinGroen1 = new Lichtsein( 12, 13, 50, 50, Color.green);
lichtseinGroen2 = new Lichtsein( 12, 70, 50, 50, Color.gray);
lichtseinGroen3 = new Lichtsein( 338, 13, 50, 50, Color.green);
lichtseinGroen4 = new Lichtsein( 338, 70, 50, 50, Color.gray);

knopRechtdoor = new Button("Rechtdoor");
knopAfbuigen = new Button("Afbuigen");

add(knopRechtdoor);
add(knopAfbuigen);

knopRechtdoor.setBounds(122,230,87,27);
knopAfbuigen.setBounds(212,230,87,27);

}


public void paint(Graphics g)
{
railsLinks.teken( g );
railsRechts.teken( g );

stoplicht1.teken( g );
stoplicht2.teken( g );

tekenGroen( g );
}


public boolean action ( Event e, Object o)
{
if( e.target == knopRechtdoor )
{
tekenGroen(g);
repaint();
return true;
}

if( e.target == knopAfbuigen )
{
tekenRood(g);
repaint();
return true;
}
return false;
}

public void tekenGroen(Graphics g)
{
lichtseinGroen1 = new Lichtsein(12, 13, 50, 50, Color.green);
lichtseinGroen1.tekenLicht( g );
lichtseinGroen2 = new Lichtsein( 12, 70, 50, 50, Color.gray);
lichtseinGroen2.tekenLicht( g );
lichtseinGroen3 = new Lichtsein(338, 13, 50, 50, Color.green);
lichtseinGroen3.tekenLicht( g );
lichtseinGroen4 = new Lichtsein(338, 70, 50, 50, Color.gray);
lichtseinGroen4.tekenLicht( g );
}

public void tekenRood(Graphics g)
{
lichtseinRood1 = new Lichtsein( 12, 13, 50, 50, Color.gray);
lichtseinRood1.tekenLicht( g );
lichtseinRood2 = new Lichtsein(12, 70, 50, 50, Color.red);
lichtseinRood2.tekenLicht( g );
lichtseinRood3 = new Lichtsein(338, 13, 50, 50, Color.gray);
lichtseinRood3.tekenLicht( g );
lichtseinRood4 = new Lichtsein(338, 70, 50, 50, Color.red);
lichtseinRood4.tekenLicht( g );
}



class Rails
{
int xSpoor1;
int ySpoor1;
int xSpoor2;
int ySpoor2;
int lengte;
int hoogte;


Rails(int xSpoor1, int ySpoor1, int xSpoor2, int ySpoor2, int lengte, int hoogte)
{
this.xSpoor1 = xSpoor1;
this.ySpoor1 = ySpoor1;
this.xSpoor2 = xSpoor2;
this.ySpoor2 = ySpoor2;
this.lengte = lengte;
this.hoogte = hoogte;
}

void teken(Graphics g)
{
g.setColor(Color.black);
g.fillRect(this.xSpoor1, this.ySpoor1, this.lengte, this.hoogte);
g.fillRect(this.xSpoor2, this.ySpoor2, this.lengte, this.hoogte);
}

}

class Stoplicht
{
private int x;
private int y;
private int lengte;
private int hoogte;


Stoplicht(int x, int y, int lengte, int hoogte)
{
this.x = x;
this.y = y;
this.lengte = lengte;
this.hoogte = hoogte;
}

public void teken(Graphics g)
{
g.setColor(Color.black);
g.fillRect(this.x, this.y, this.lengte, this.hoogte);
}

}

class Lichtsein
{
private int x;
private int y;
private int lengte;
private int hoogte;
private Color kleur;

Lichtsein(int x, int y, int lengte, int hoogte, Color kleur)
{
this.x = x;
this.y = y;
this.lengte = lengte;
this.hoogte = hoogte;
this.kleur = kleur;
}

public void tekenLich(Graphics g) {
g.setColor(kleur);
g.fillOval(this.x, this.y, this.lengte, this.hoogte);
}

}

}
 
The problem is you only have access the Graphics object in paint(), you are trying to access it everywhere else. Just by putting Graphics g as a parameter doesn't mean you are magically going to have access to the ACTUAL Graphics object. You probably got a cannot resolve symbol compilation error when trying to access Graphics g, right? So you added it as an class variable. Well, no where in your Applet do you actually set this variable. So in all those places where you are manually trying to pass in the Graphics object you are getting a NullPointer Exception. Wushutwist
 
I have a question. What is this Applet sopposed to do? X-) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top