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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with nullpointer

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0



Hello,

can someone please tell me what i have to change in my source so the nullpointer doesnt appear??
I know it got something to do with Graphics g but i cant find the problem.
I have to finish this tomorrow so it would help me a lot!

Here is the source:

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 tekenLicht(Graphics g) //Hier zit Null-pointer,maar waar??????!!!!!!!!
{
g.setColor(kleur);
g.fillOval(this.x, this.y, this.lengte, this.hoogte);
}

}



}



Thanx in advance, Bram
 
I have a question. Was that code in German? "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
 
It would be more helpful to include the stack trace as this will usually point you in the direction of the line of code causing the problem.
bruce21.jpg
 
Hey try using this

Graphics graphics = g.create();
And use it
I hope ur probs will be solved.

Shyam Shyam
 
I can't read Dutch. :cool: "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