java - 继续出现此错误;线程中出现异常 "main"java.lang.NullPointerException

标签 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); 
 } 
}

最佳答案

似乎返回null

getClass().getResource("赌场筹码.jpg")

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

然后相应地修复它。

关于java - 继续出现此错误;线程中出现异常 "main"java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22849049/

相关文章:

java - 使用 Java 7 避免嵌套数据中出现 null 的更好方法

Java ConcurrentHashMap 不是线程安全的..wth?

Java 字符串等于文字警告消息

java - 私有(private)查询方式是否会增加SQL注入(inject)攻击风险?

java - 如何设置 Safari 下载位置 - Selenium WebDriver

java - SharedPreferenceclear() 抛出空指针异常

java - HBase:MiniDFSCluster.java 在某些环境中失败

java - 从一个队列铲到另一个队列后如何拒绝消息?

java jni异常访问冲突

java - 由于 web.xml,内容未显示在 profile.jsp 上?