I cant get it to work! NullPointerExeption at first awt.Graphics Object
I am writing a game with AWT having it as an applet. I intend NOT to use anything outside of Java 1.1
For my Game i thought that i ATLEAST need the following classes:
MainGame, Level, Unit.
I Started writing Level. wrote a fewe methods for it such as
drawGras(int grastype)
And started to write MainGame to see if they would work together. it's is/is going to be my first Java application where i actually know what object orienting is... I have already wrote a game in Java but with out knowing what a class or what object orienting realy is(the game works rather good to) but this time i wanted to use object orienting but came up with problems =( if first posted in "New to Javaprogramming" but they couldn't help me and advice me here
My previous post: http://forum.java.sun.com/thread.jsp?forum=54&thread=290558
--
Here's my code:
import java.awt.*;
publicclass MainGameextends java.applet.Applet
implements Runnable{
Thread runner;
Image ImgLevel, ImgUnit;
public MainGame(){
Level nr1 =new Level();
nr1.init();
nr1.setGround(300);
nr1.drawGras(1);
nr1.CalcGraphics();
nr1.drawSky(1);
Image ImgLevel = nr1.getLevelImage();
}
publicvoid paint(Graphics g){
g.drawImage(ImgLevel,0,0,this);
}
publicvoid update(Graphics g){
paint(g);
}
publicvoid init(){
MainGame Game =new MainGame();
}
publicvoid stop(){
runner =null;
System.exit(1);
}
publicvoid start(){
if(runner ==null){
runner =new Thread(this);
runner.start();
}
}
void Pause(int time){
try{
Thread.sleep(time);
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
publicvoid run(){
Thread thisThread = Thread.currentThread();
while(thisThread == runner){
}
}
}
class Levelextends java.applet.Applet{
Font BigText =new Font("Arial", Font.PLAIN, 36);
Font Normal =new Font("Arial", Font.PLAIN, 14);
Font Bold =new Font("Arial", Font.BOLD, 14);
Graphics GrLevel;
int pattern, sky, GroundtoWalk, Ground;
Color BgColor =new Color(166,202,240);
Color gras1 =new Color(0,255,0);
Color gras2 =new Color(0,200,0);
Color gras3 =new Color(0,100,0);
Image ImgLevel;
void setBgColor(int r,int g,int b){
BgColor =new Color(r,g,b);
}
void CalcGraphics(){
ImgLevel = createImage(800,600);
GrLevel = ImgLevel.getGraphics();
}
void setGround(int GrounD){
Ground = GrounD;
}
void GroundToWalkADD(int addToGround){
GroundtoWalk = Ground + addToGround;
}
void setGroundToWalk(int GrToWalk){
GroundtoWalk = GrToWalk;
}
publicvoid init(){
System.out.println("Init 1");// This was placed here when i was checking how far it got when debuging
CalcGraphics();
System.out.println("Init 2");
drawGras(1);
System.out.println("Init 3");
repaint();
System.out.println("Init 4");
}
Image getLevelImage(){
return ImgLevel;
}
publicvoid paint(Graphics g){
g.drawImage(ImgLevel,0,0,this);
}
publicvoid update(Graphics g){
paint(g);
}
void drawGras(int pt){
GrLevel.setColor(BgColor);
GrLevel.fillRect(0,0,800,600);
switch(pt){
case 1:
GrLevel.setColor(gras1);
GrLevel.fillRect(0,GroundtoWalk,800,300);
GrLevel.setClip(0,GroundtoWalk,800,300);
for(int i = 0; 890>i;i=i+5){
GrLevel.setColor(gras2);
GrLevel.drawLine(0+i,GroundtoWalk,-190+i,GroundtoWalk+300);
}
for(int i=950; 0<i; i=i-5){
GrLevel.setColor(gras3);
GrLevel.drawLine(890-i,GroundtoWalk,1000-i,GroundtoWalk+300);
}
break;
case 2:
GrLevel.setColor(gras2);
GrLevel.fillRect(0,GroundtoWalk+5,800,300);
for(int i = 0; 890>i;i=i+5){
GrLevel.setColor(gras3);
GrLevel.drawLine(0+i,GroundtoWalk,-190+i,GroundtoWalk+300);
}
for(int i=950; 0<i; i=i-5){
GrLevel.setColor(gras1);
GrLevel.drawLine(890-i,GroundtoWalk,1000-i,GroundtoWalk+300);
}
break;
case 3:
GrLevel.setColor(gras3);
GrLevel.fillRect(0,GroundtoWalk+10,800,300);
for(int i = 0; 800>i; i=i+20){
GrLevel.fillArc(0+i,GroundtoWalk,20,20,180,-180);
}
break;
}
pattern = pt;
}
int getGrasPattern(){
return pattern;
}
void drawSky(int cl){
GrLevel.setColor(BgColor);
GrLevel.fillRect(0,0,800,155);
switch(cl){
case 1:
for(int y = 0;y<7;y++){
for(int x = 0;x<800;x=x+20){
GrLevel.setColor(Color.white);
GrLevel.drawArc(x,y*20,20,20,180,180);
GrLevel.setColor(Color.red);
GrLevel.drawArc(x,y*20+4,20,20,180,180);
GrLevel.setColor(Color.yellow);
GrLevel.drawArc(x,y*20+2,20,20,180,180);
GrLevel.setColor(Color.green);
GrLevel.drawArc(x,y*20+8,20,20,180,180);
GrLevel.setColor(Color.blue);
GrLevel.drawArc(x,y*20+12,20,20,180,180);
GrLevel.setColor(Color.pink);
GrLevel.drawArc(x,y*20+16,20,20,180,180);
}
}
break;
case 2:
GrLevel.setColor(Color.gray);
break;
}
sky = cl;
GrLevel.setFont(BigText);
GrLevel.setColor(Color.black);
GrLevel.fillRect(0,0,800,50);
GrLevel.setColor(Color.white);
GrLevel.drawString("Score Tabel - comming someday",((size().width)/2)-260, 40);
}
int getSkyTyp(){
return sky;
}
}
}
I tested running the code of class Level by puting it into Level.java and compiled it...ran it with appletviewer and it worked as it was intended to.
But when i compiled it with MainGame... and in the HTML started MainGame.class,.....applet didn't intializeand i got the following error messege:
Init 1
java.lang.NullPointerException
at Level.CalcGraphics(MainGame.java:84)
at MainGame.<init>(MainGame.java:11)
Line 82:void CalcGraphics() {
Line 83:ImgLevel = createImage(800,600);
Line 84:GrLevel = ImgLevel.getGraphics(); // The First awt.Graphics object
Line 85:}
Line 9:public MainGame() {
Line 10:Level nr1 = new Level();
Line 11:nr1.CalcGraphics(); // calling the method above -> same error first awt.Graphics Object.
-
If i change it os that "drawGras" comes first it's first awt.Graphics Line will show the same error
-
Hope some one can and will help me

