【问题标题】:Keep on getting this error; Exception in thread "main" java.lang.NullPointerException [duplicate]继续收到此错误;线程“主”java.lang.NullPointerException中的异常[重复]
【发布时间】:2014-04-03 21:09:02
【问题描述】:

我不断收到此错误:

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

【问题讨论】:

  • 第 55 行是什么代码?似乎是这一行:new javax.swing.ImageIcon(getClass().getResource("casino chips.jpg")),但你能在你的 IDE 中验证吗?
  • 检查getClass().getResource("casinochips.jpg")是返回一个对象还是null?
  • 感谢用户:peter.petrov,我解决了这个问题,他指出哪一行给出了错误的值。看来我将图像保存在 src 文件夹而不是 bin 文件夹中。移动它们可以解决问题。

标签: java nullpointerexception


【解决方案1】:

似乎这正在返回null

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

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

然后相应地修复它。

【讨论】:

  • 我应该将图像保存在哪里?我会将它们放在与 .java 文件相同的文件夹中吗?我想我可能将它们保存在错误的位置。
  • 好吧,我不擅长这个。我会阅读 javadoc,然后查找有关此主题的一些示例和简单教程。尝试将它们放在应用程序的工作目录中(类文件所在的根目录)。 docs.oracle.com/javase/7/docs/api/java/lang/…
【解决方案2】:

问题是像(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);

【讨论】:

  • 谢谢,我刚试过,好像不行。我到底应该在哪里保存图像以供 Eclipse 查找资源?
  • 这个问题有很多答案。我搜索了“[java] Class getResource URL”,它给了我一些很好的匹配。例如,试试this one
猜你喜欢
  • 1970-01-01
  • 2014-12-15
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多