继续得到这个错误;线程" main"中的例外情况显示java.lang.NullPointerException

时间:2014-04-03 21:09:02

标签: java nullpointerexception

我继续得到这个错误:

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)

我不确定我做错了什么,我是否将图像引用到了错误的位置?我试过调试,但我不确定问题是什么。请告诉我该怎么做。

以下是我的代码

//----------------------------------- 
// 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. 
     } 
     } 
    } 

我也设置了驱动程序

//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); 
 } 
}

2 个答案:

答案 0 :(得分:2)

似乎这是返回null

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

我会检查是否以及为什么。

然后相应地修复它。

答案 1 :(得分:0)

问题是像(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aaa.png"))这样的行之一没有找到资源。将资源分离到变量,并添加一些日志记录或调试它。

URL file = getClass().getResource("/programming/assignment/pkg2/programming assignment2/aaa.png");
LOG.info("Trying to load " + file.toString());
ImageIcon img = new ImageIcon(file);