我一直收到这个错误:
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);
}
}
问题是其中一行类似于(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);
似乎返回了null。
getClass(). getResources("赌场chips.jpg")
我会检查是否如此以及原因。
然后进行相应的修复。
我的代码中出现了这个错误。 这是我的代码: 这就是结果。错误:在线程“main”java中输入model:Exception。lang.NullPointerException在汽车上。主(车.java:10)
我对Java非常陌生,似乎遇到了一些奇怪的错误。我到处寻找解决方案,我遇到的所有解决方案都与我已有的完全相同。 我已经编写了一个类,将目的地添加到ArrayList,但它不起作用。 我得到了这个错误:“线程中的异常”main“java.lang.NullPointerException” 这是我的代码: 我试图添加到ArrayList的数据的代码是这样的: 它退出方法中的程序,并且不将目标添加到数
我有任务要做,我有一个问题,我得到了错误。我不知道如何修理它: 异常线程"main"java.lang.NullPointerExcema在test.main(test.java:29) 这是我的密码:
我在ViewModel中编写了单元测试,并与mockito协作。类,但我得到以下异常 在我的ViewModel类下面,我在其中实现了视图模型逻辑 低于存储库类 接口类下面 接口GiphyTaskApiService{ 低于我的ViewModel测试类 我试过的 1。[我尝试了以下链接][1] onCreation_fetchUserApiCalled_dataSavedToLiveDataNull
我正在通过selenium学习自动化测试教程,并且正在用java语言编写我的第一个脚本,并且在Eclipse的“控制台”中得到了这条消息。 我的代码: 教程链接:http://toolsqa.wpengine.com/selenium-webdriver/first-test-case/
当我试图让用户重新输入一个数字时,会出现这个错误。 错误显示在guess=scan的下一次迭代中。nextInt(); 输入/输出: 我只是重新学习Java,任何帮助都很感激。