Hi, why wont this work!!! >.<
I get this error
Exception in thread "Thread-2" java.lang.NullPointerException
at main.render(main.java:104)
at main.run(main.java:84)
at java.lang.Thread.run(Unknown Source)import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; public class main extends Canvas implements Runnable { private static final long serialVersionUID = 1L; public static final int WIDTH = 240; public static final int HEIGHT = WIDTH/12*9; public static final int SCALE = 3; public static final String NAME = "Half Life 3"; private JFrame frame; public boolean running = false; public int tickCount = 0; private Graphics g ; private ImageIcon img = new ImageIcon("res\background.jpg"); private Image back = img.getImage(); public main(){ setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE)); setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE)); setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE)); frame = new JFrame(NAME); ImageIcon icon = new ImageIcon("res/Half-life.png"); frame.setIconImage(icon.getImage()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(this,BorderLayout.CENTER); frame.pack(); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void run(){ long lastTime = System.nanoTime(); double nsPerTick = 1000000000D/60D; //Nano Sec per tick D represents its type double same as doing .0 int ticks = 0; int frames = 0; long lastTimer = System.currentTimeMillis(); double delta = 0 ; // How many nano seconds have gone by so far. Once we hit one, we will -1 from it. init(); while(running){ long now = System.nanoTime(); delta += (now - lastTime) / nsPerTick; lastTime = now; boolean shouldRender = true; while(delta >= 1){ ticks++; tick(); delta -=1; shouldRender = true; } try{ Thread.sleep(2); }catch(Exception e){} if(shouldRender){ frames++; render(); } if(System.currentTimeMillis() - lastTimer >= 1000){ lastTimer += 1000; System.out.println(ticks+" Ticks, "+frames+" Frames"); frames = 0; ticks = 0; } } } public void tick(){ tickCount++; } public void render(){ // g.drawImage(back,0,0,WIDTH/2,HEIGHT/2,null); g.drawRect(0, 0, WIDTH, HEIGHT); } public void init(){ int index = 0; //loops through reds green and blues; } public synchronized void start() { running = true; new Thread(this).start(); } public synchronized void stop() { running = false; } public static void main(String [] args){ new main().start(); } }