获得异常java.lang.NullPointerException。无法找出原因

时间:2015-05-01 05:45:36

标签: java exception null

这里的第一篇文章。但我有一些问题,我认为这将是一个简单的"程序"写。

它是一个程序,它接受.text文件并由#34;播放器"组织它。数。球员是棒球运动员,并按球员号码(球衣背面的数字)进行聆听。

这是fileMenuHandler文件

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileMenuHandler implements ActionListener {
   JFrame jframe = new JFrame();
public FileMenuHandler (JFrame jf) {
   jframe = jf;}
  public void actionPerformed(ActionEvent event) {
  String  menuName = event.getActionCommand();
  File fileName = null;
  if (menuName.equals("Open"))
     readSource(fileName); 
  else if (menuName.equals("Quit"))
     System.exit(0);} //actionPerformed

private void openFile( ) {
   JFileChooser chooser = new JFileChooser( );
   int status;
   status = chooser.showOpenDialog(null);
   chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
   chooser.showOpenDialog(null);
   if (status == JFileChooser.APPROVE_OPTION) 
      readSource(chooser.getSelectedFile());
   else 
      JOptionPane.showMessageDialog(null, "Open File dialog canceled");
} //openFile

private void readSource(File chosenFile) {
   String chosenFileName = chosenFile.getName();
   TextFileInput inFile = new TextFileInput(chosenFileName);
   String names;
   int subscript = 0;
   TextArea myTextArea = new TextArea();
   TextArea mySubscripts = new TextArea();
   names = inFile.readLine();
   while (names != null) {
      mySubscripts.append(Integer.toString(subscript++)+"\n");
      myTextArea.append(names+"\n");
      names = inFile.readLine();
   } //while
 jframe.setVisible(true);  
 }}

我收到错误:

String chosenFileName = chosenFile.getName();
File fileName = null;

我觉得这是一个非常愚蠢的错误,但我需要一些指示。 所有的帮助将非常感激,随意取笑我:) lol

错误日志:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FileMenuHandler.readSource(FileMenuHandler.java:32)
at FileMenuHandler.actionPerformed(FileMenuHandler.java:14)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:157)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

2 个答案:

答案 0 :(得分:1)

由于这些行,你得到NullPointerException

File fileName = null;
  if (menuName.equals("Open"))
     readSource(fileName); 

在这里,您将filename设置为null,并在另一种方法中使用filename

喜欢:String chosenFileName = chosenFile.getName();

会导致NullPointerException

一个简单的修复方法是将文件对象分配给此变量。喜欢:

File file=new File(<pathtofile>);

答案 1 :(得分:0)

为什么要在此行设置空文件名:

File fileName = null;
  if (menuName.equals("Open"))
     readSource(fileName); 

这导致Null指针异常。

相反,您应该提供您正在阅读的文件名。例如:

File fileName = new File("/path-to-file/hello.txt");