Java 有时,当我单击应该打开新框架的图标时,由于不可解释的原因获取空指针异常
这种情况有时会发生,但大多数时候都很好。我已经尝试捕获异常,因为异常只会打印到终端,不会影响程序的运行,但这也不起作用。这是我剩下的代码。它基本上是一个粘滞程序,当按下图标时没有更多的帧打开时,它会创建一个新的粘滞。有时,当框架打开并按下时,会发生异常,但只是有时 这是我的堆栈跟踪:Java 有时,当我单击应该打开新框架的图标时,由于不可解释的原因获取空指针异常,java,nullpointerexception,mouseevent,Java,Nullpointerexception,Mouseevent,这种情况有时会发生,但大多数时候都很好。我已经尝试捕获异常,因为异常只会打印到终端,不会影响程序的运行,但这也不起作用。这是我剩下的代码。它基本上是一个粘滞程序,当按下图标时没有更多的帧打开时,它会创建一个新的粘滞。有时,当框架打开并按下时,会发生异常,但只是有时 这是我的堆栈跟踪: public class Sticky { private JFrame frame; private JTextPane textPane; private JFileChooser ch
public class Sticky
{
private JFrame frame;
private JTextPane textPane;
private JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
private static Point POINT = new Point(0,0);
private static int frameUp = 0;
private static Clip clip;
private static boolean musicOn = false;
private boolean isPlaying = false;
public Sticky()
{
textPane = new JTextPane();
textPane.setBackground(Color.yellow);
makeFrame();
}
private void makeFrame()
{
frame = new JFrame("");
Container contentPane = frame.getContentPane();
frame.setLayout(new BorderLayout());
Dimension dem = new Dimension(250,220);
textPane.setPreferredSize(dem);
JScrollPane scrollPane = new JScrollPane(textPane);
frame.add(scrollPane, BorderLayout.CENTER);
makeMenu();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) { doThis(); }
});
frame.pack();
}
public void doThis()
{
if(isPlaying)
{
Sticky.clip.close();
Sticky.frameUp--;
Sticky.musicOn = false;
}
else
{
Sticky.frameUp--;
}
}
private void makeMenu()
{
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem newItem = new JMenuItem("New Note");
newItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { newNote(); }
});
fileMenu.add(newItem);
JMenuItem importItem = new JMenuItem("Import Text");
importItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { importText(); }
});
fileMenu.add(importItem);
JMenuItem exportItem = new JMenuItem("Export Text");
exportItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { exportText(); }
});
fileMenu.add(exportItem);
JMenu insertMenu = new JMenu("Insert...");
JMenuItem imageInsert = new JMenuItem("Image");
imageInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { addImage(); }
});
insertMenu.add(imageInsert);
JMenuItem soundInsert = new JMenuItem("Sound");
soundInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { addSound(); }
});
insertMenu.add(soundInsert);
fileMenu.add(insertMenu);
}
private void addSound()
{
int val = chooser.showOpenDialog(frame);
if(val != JFileChooser.APPROVE_OPTION)
{
return;
}
// if(!chooser.getSelectedFile().getName().contains(".wav"))
//{
//error
// return;
//}
ImageIcon icon = new ImageIcon("sound.png");
JButton button = new JButton(icon);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { playSound(chooser.getSelectedFile()); }
});
textPane.insertComponent(button);
}
private void playSound(File file)
{
if(Sticky.musicOn)
{
Sticky.musicOn = false;
isPlaying = false;
Sticky.clip.close();
return;
}
try
{
AudioInputStream audio = AudioSystem.getAudioInputStream(file);
Sticky.clip = AudioSystem.getClip();
Sticky.clip.open(audio);
Sticky.clip.start();
Sticky.musicOn = true;
isPlaying = true;
}
catch(UnsupportedAudioFileException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(LineUnavailableException e)
{
e.printStackTrace();
}
}
private void addImage()
{
int val = chooser.showOpenDialog(frame);
if(val != JFileChooser.APPROVE_OPTION)
{
return;
}
ImageIcon icon = new ImageIcon(chooser.getSelectedFile().getPath());
textPane.insertIcon(icon);
textPane.setCaretPosition(textPane.getText().length());
}
private void exportText()
{
try
{
int val = chooser.showSaveDialog(frame);
if(val != JFileChooser.APPROVE_OPTION)
{
return;
}
File file = chooser.getSelectedFile();
FileWriter writer = new FileWriter(file.getPath() + ".txt");
writer.write(textPane.getText());
writer.close();
}
catch(IOException e)
{
//jtextpane
}
}
private void importText()
{
int val = chooser.showOpenDialog(frame);
if(val != JFileChooser.APPROVE_OPTION)
{
//jtextpane
return;
}
File file = chooser.getSelectedFile();
try
{
textPane.read(new BufferedReader(new FileReader(file)), file);
textPane.setCaretPosition(textPane.getText().length());
}
catch(IOException e)
{
//jtextpane
}
}
private void newNote()
{
Sticky.POINT = frame.getLocation();
if(Sticky.POINT.getY() >= 500)
{
Sticky.POINT = new Point((int)Sticky.POINT.getX()+300, 0);
}
else
{
Sticky.POINT = new Point((int)Sticky.POINT.getX(), (int)Sticky.POINT.getY()+250);
}
Sticky stick = new Sticky();
stick.frame.setLocation(Sticky.POINT);
stick.frame.setVisible(true);
Sticky.frameUp++;
}
private static void openFrame()
{
if(Sticky.frameUp == 0)
{
Sticky stick = new Sticky();
stick.frame.setVisible(true);
Sticky.frameUp = 1;
}
}
public static void main(String args[])
{
try
{
SystemTray tray = SystemTray.getSystemTray();
ImageIcon image = new ImageIcon("note.jpg");
TrayIcon icon = new TrayIcon(image.getImage());
icon.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { openFrame(); }
});
tray.add(icon);
}
catch(AWTException e)
{
return;
}
Sticky.musicOn = false;
Sticky stick = new Sticky();
stick.frame.setVisible(true);
Sticky.frameUp = 1;
}
}
打印到终端的异常是否指示引发异常的行?堆栈跟踪如何?我自己的代码中没有一行打印出来:(无论如何,给我们看看堆栈跟踪。否则我们将无法帮助您。(除了向您指出解释如何诊断NPE的一般问答。)另外,向我们展示
Stick
类,因为您的openFrame
方法可能导致问题。好的,我更新了它
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.LightweightDispatcher.eventDispatched(Container.java:4742)
at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2425)
at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2317)
at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2275)
at java.awt.TrayIcon.dispatchEvent(TrayIcon.java:721)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:763)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)