NullPointerException - createImage()
In this prgram i get NullpoinerExceptiion. Program reports it in lin marked ***, but the problem one line above (marked **). After calling createImage mamImage is still null.
What is the problem in how to fix it?
Code:
import java.awt.*;
import javax.swing.*;
public class Panela extends JPanel implements Runnable{
int korak=0;
int zakasnitev=50;
boolean running=false;
Image memImage;
Graphics memGraphics;
Thread nit;
Panela(){
**memImage= createImage(500,500);
***memGraphics = memImage.getGraphics();
setVisible(true);
start();
}
public void run(){
while(running){
long cas=System.currentTimeMillis();
korak++;
repaint();
cas=System.currentTimeMillis();
try{
if(cas<zakasnitev)
Thread.sleep(zakasnitev-(int) cas);
}
catch(InterruptedException e){e.printStackTrace(); System.exit(-1);}
}
}
public void start(){
if(!running) {
nit= new Thread(this);
nit.start();
running=true;
}
}
public void paint(Graphics g){
update(g);
}
public void update(Graphics g){
memGraphics.setColor(getBackground());
memGraphics.fillRect(0, 0, getWidth(), getHeight());
memGraphics.setColor(Color.black);
int odmik= korak % 20;
for(int x=0; x<getWidth()+getHeight(); x+=20){
memGraphics.drawLine(x, 0, 0, x);
}//for
g.drawImage(memImage, 0, 0, this);
}
}