This discussion is archived
6 Replies Latest reply: Mar 24, 2009 7:57 AM by 843789 RSS

Null pointer exception- with array of images

843789 Newbie
Currently Being Moderated
Hi , i am having some problems with my program. I am trying to create an array of images( which i dont really know how to do) and i keep getting this error
Exception in thread "main" java.lang.NullPointerException
at DiceRoller.<init>(DiceRoller.java:35)
at RollDice.main(RollDice.java:19)
Here is my code, any help would be appreciated, i have switched things around and i still can't figure out what is wrong
/**
 * @(#)RollDice.java
 *
 * RollDice application
 *
 * @author 
 * @version 1.00 2009/3/23
 */
import java.util.Scanner; 
import javax.swing.*;
import java.awt.*; 
import java.awt.event.*;

public class RollDice {
    
    
    public static void main(String[] args) {
         
         DiceRoller diceroller  = new DiceRoller();
         //Default Close operations
         diceroller.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
         //Size
        diceroller.setSize( 121, 121 ); // set frame size
        //Dont want anyone to mess with it 
        diceroller.setResizable(false); // Cant resize
        diceroller.setVisible( true ); // display frame
   
    
    }
   
}
/*
 * Dice.java
 */

 //Main dice Class
import java.util.Random;
import java.io.*;
public class Dice
{
     public      Random r = new Random();
     private int sides;
     private int rolls;
     public int x;


     public Dice()
     {
          
     }
     
     /*
     Roll method
     */
     public int roll()
     {
          
          x=0;
          x = (r.nextInt(6)+1);
          return x;
     }  

}
/** DiceRoller.java
 * 
 * Roll, print, Gui
 *
 */
 
import javax.swing.*;
     
public class DiceRoller extends JFrame
{
          
      private ImageIcon[] image = null ; 
      public String[] images = { "empty", "dice1.jpg",
               "dice2.jpg", "dice3.jpg", "dice4.jpg",
               "dice5.jpg", "dice6.jpg" }; 
     public Dice die;
     private int rollVal;
     public int rollNum;
     private JLabel j1;
     public DiceRoller(){
      
       die =new Dice();
       int rollVal = 0; 
       rollVal = die.roll();     
       //Images     
       image = new ImageIcon[images.length];
      for(int i = 0; i < images.length; i++){
      image[i] = new ImageIcon(images);
}//end for

// setImage(rollVal);

     if (image!=null){
          j1.setIcon(image[rollVal]);
     }
     
          
     System.out.println("Roll = "+die.roll());
          
     }
     
}