0

I keep on get this error:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at SlotMachinePanel.<init>(SlotMachinePanel.java:55)
    at SlotMachineDriver.main(SlotMachineDriver.java:16)

I am not sure what I am doing wrong, am I referencing the images to the wrong spot? I tried debugging but I'm not sure what the issue is. Please advise me on what to do.

Below is my code

//----------------------------------- 
// Slot Machine Panel 
//----------------------------------- 


import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Random; 

public class SlotMachinePanel extends JPanel 
{ 
 private JPanel buttonPanel, newSlotsPanel, primary; 
 private JButton spin, cashout; 
 private JLabel spinlabel, cashoutlabel, slotsPanelLabel1, slotsPanelLabel2; 
 private JLabel imageLabel; 
 private ImageIcon icon; 
 private int spinResult = 0; 
 private int currentTokens = 5; 

 //--------------------------------------------------------------------- 
 //Constructor: Sets up the SlotMachine GUI. 
 //--------------------------------------------------------------------- 
 public SlotMachinePanel () 
 { 
 //Creation of primary (Background Panel) 
 primary = new JPanel (); primary.setPreferredSize (new Dimension(325,275)); 
 primary.setBackground (Color.red); 

 //Creation of newSlotsPanel (Slot Screen Panel) 
 newSlotsPanel = new JPanel (); 
 newSlotsPanel.setPreferredSize (new Dimension (300,175)); 
 newSlotsPanel.setBackground (Color.white); 
 slotsPanelLabel1 = new JLabel ("Current Tokens:" + currentTokens); 
 slotsPanelLabel2 = new JLabel ("Result of Spin:" + spinResult); 

 //Creation of buttonPanel (Button Panel) 
 buttonPanel = new JPanel(); 
 buttonPanel.setPreferredSize (new Dimension(300,80)); 
 buttonPanel.setBackground (Color.blue); 

 //Creation of spin button 
 spin = new JButton("Spin"); 
 spin.addActionListener (new SpinListener()); 
 spinlabel = new JLabel ("Only costs 1 token to spin! Good luck!"); 
 spinlabel.setForeground (Color.white); 

 //Creation of cashout button 
 cashout = new JButton("Cash Out"); 
 cashout.addActionListener (new CashoutListener()); 
 cashoutlabel = new JLabel ("No Button Pushed"); 

 //Creation of image loader 

icon = (new javax.swing.ImageIcon(getClass().getResource("casino chips.jpg")));
imageLabel = new JLabel (icon); 


 //Layering of Panels 
 add (primary); 
 primary.add(newSlotsPanel); 
 primary.add(buttonPanel); 

 //Adding Labels on newSlotsPanel 
 newSlotsPanel.add(slotsPanelLabel1); 
 newSlotsPanel.add(slotsPanelLabel2); 
 newSlotsPanel.add(imageLabel); 

 //Adding Buttons and Labels on Button Panel 
 buttonPanel.add(spin); 
 buttonPanel.add(cashout); 
 buttonPanel.add(spinlabel); 

 } 

 //***************************************************************** 
 // Represents a listener for Spin Button (action) events. 
 //***************************************************************** 
 private class SpinListener implements ActionListener 
 { 
 //-------------------------------------------------------------- 
 // Updates the Spin and label when the button is pushed. 
 //-------------------------------------------------------------- 
     public void actionPerformed (ActionEvent event) 
     { 
     //Informing of pushed buttom 
     spinlabel.setText("Spin again or cash out"); 

     //Initiate Random Number choice for images 
     Random generator = new Random(); 
     int imageNum; 

     imageNum = generator.nextInt(27); 

     switch (imageNum) 
     { 
     case 0: //aaa 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aaa.png"))); 
     spinResult = 5; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 5 tokens! Nice win! Spin again!"); 
     break; 
     case 1: //aac 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aac.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 2: //aao 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aao.png"))); 
     spinResult = 1; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 1 token. Spin again!"); 
     break; 
     case 3: //aca 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aca.png"))); 
     spinResult = 2; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 2 tokens. Spin again!"); 
     break; 
     case 4: //acc 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/acc.png"))); 
     spinResult = 4; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 4 tokens. Nice win! Spin again!"); 
     break; 
     case 5: //aco 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aco.png"))); 
    spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 6: //aoa 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aoa.png"))); 
     spinResult = 1; 
     spinlabel.setText("Won 1 token. Nice! Spin again!"); 
     currentTokens = currentTokens + spinResult - 1; 
     break; 
     case 7: //aoc 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aoc.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 8: //aoo 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aoo.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 9: //caa 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/caa.png"))); 
     spinResult = 3; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 3 tokens. Nice win! Spin again!"); 
     break; 
     case 10: //cac 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cac.png"))); 
     spinResult = 2; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 2 tokens. Nice win! Spin again!"); 
     break; 
     case 11: //cao 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cao.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 12: //cca 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cca.png"))); 
     spinResult = 4; 
    currentTokens = currentTokens + spinResult - 1;
     spinlabel.setText("Won 4 tokens. Nice win! Spin again!"); 
     break; 
     case 13: //ccc 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ccc.png"))); 
     spinResult = 10; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Jackpot! Won 10 tokens. Spin again!"); 
     break; 
     case 14: //cco 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cco.png"))); 
     spinResult = 2; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 2 tokens. Nice win! Spin again!"); 
     break; 
     case 15: //coa 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/coa.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 16: //coc 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/coc.png"))); 
     spinResult = 2; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 2 tokens. Nice win! Spin again!"); 
     break; 
     case 17: //coo 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/coo.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 18: //oaa 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oaa.png"))); 
     spinResult = 1; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 1 token. Nice win! Spin again!"); 
     break; 
     case 19: //oac 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oac.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1;
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 20: //oao 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oao.png"))); 
     spinResult = -1; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Ouch... Lost 1 token. Spin again!"); 
     break; 
     case 21: //oca 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oca.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 22: //occ 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/occ.png"))); 
     spinResult = 1; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 1 token. Nice! Spin again!"); 
     break; 
     case 23: //oco 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oco.png"))); 
     spinResult = -1; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Ouch... Lost 1 token! Spin again!"); 
     break; 
     case 24: //ooa 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooa.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 25: //ooc 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooc.png"))); 
     spinResult = 0; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Won 0 tokens. Spin again!"); 
     break; 
     case 26: //ooo 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooo.png"))); 
     spinResult = -5; 
     currentTokens = currentTokens + spinResult - 1; 
     spinlabel.setText("Cmon!! Lost 5 tokens. Spin again!"); 
     break; 

     default: 
     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooo.png"))); 
     } 
     slotsPanelLabel1.setText("Current Tokens:" + currentTokens);//returns new value of current tokens 
     slotsPanelLabel2.setText("Result of Spin:" + spinResult);//returns the new result of spinning 
     } 
     } 
     //***************************************************************** 
     // Represents a listener for Spin Button (action) events. 
     //***************************************************************** 
     private class CashoutListener implements ActionListener 
     { 
     //-------------------------------------------------------------- 
     // Updates the Spin and label when the button is pushed. 
     //-------------------------------------------------------------- 
     public void actionPerformed (ActionEvent event) 
     { 
     //Informing of pushed buttom 
     spinlabel.setText("Spin to play again. Your cash out value is: "+ 
    currentTokens); 

     imageLabel.setIcon(new 
    javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/money.jpg"))); // 
     currentTokens = 5; // resets the current token amount to 5. 
     } 
     } 
    } 

I also have a driver set up

//Driver for SlotsMachinePanel 


import javax.swing.JFrame; 

public class SlotMachineDriver 
{ 
 //-------------------------------- 
 // Creates the main program frame. 
 //-------------------------------- 
 public static void main (String[] args) 
 { 
 JFrame frame = new JFrame ("Slot Machine"); 
 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

 frame.getContentPane().add(new SlotMachinePanel()); 

 frame.pack(); 
 frame.setVisible(true); 
 } 
}
  • What code is line 55? It appears to be this line: `new javax.swing.ImageIcon(getClass().getResource("casino chips.jpg"))`, but can you verify that in your IDE? – mellamokb Apr 03 '14 at 21:11
  • check whether getClass().getResource("casino chips.jpg") is return an object or null ? – harshit Apr 03 '14 at 21:11
  • I solved the problem thanks to user: peter.petrov who pointed out which line was was giving the wrong value. It appears that I had the images saved in the src folder instead of the bin folder. Moving them fixed the problem. – user3105123 Apr 03 '14 at 21:25

2 Answers2

2

Seems this is returning null.

getClass().getResource("casino chips.jpg")

I would check if so and why.

Then fix it accordingly.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Where exactly am I supposed to save the images? Would I put them in the same folder as the .java files? I think I might be saving them in the wrong spot. – user3105123 Apr 03 '14 at 21:19
  • Well, I am not good at this. I would read the javadoc, then look for some examples and simple tutorials on this topic. Try putting them in the working directory of your app to start with (the root of where your class files are). http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29 – peter.petrov Apr 03 '14 at 21:24
0

The problem is that one of the lines like (new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aaa.png")) is not finding a resource. Separate out the resource to a variable, and add in some logging or debug it.

URL file = getClass().getResource("/programming/assignment/pkg2/programming assignment2/aaa.png");
LOG.info("Trying to load " + file.toString());
ImageIcon img = new ImageIcon(file);
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • Thank you, I just tried that and it doesn't seem to work. Where exactly am I supposed to save the images for Eclipse to find the resources? – user3105123 Apr 03 '14 at 21:20
  • There are quite a few answers to this question around. I searched for "[java] Class getResource URL" and it gave me a few good matches. Try [this one](http://stackoverflow.com/questions/18205455/getresource-will-always-return-null), for example. – Paul Hicks Apr 03 '14 at 21:38