I would like to display the audio information to jList after I selected a lists of songs from JFileChooser. Here is what I get as far, but it throws a NullPointerException.
boolean openFile() throws FileNotFoundException{
JFileChooser jfc = new JFileChooser();
jfc.setMultiSelectionEnabled(true);
jfc.setDialogTitle("Open File");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setCurrentDirectory(new File ("."));
jfc.setFileFilter(audiofilter);
int result = jfc.showOpenDialog(this);
if(result == JFileChooser.CANCEL_OPTION){
return true;
}else if(result == JFileChooser.APPROVE_OPTION){
File[] file = jfc.getSelectedFiles();//fFile = jfc.getSelectedFile();
String file_string = fFile.getAbsolutePath();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
String[] filesInDirectory = file[i].list();
for(int ii=0;ii<filesInDirectory.length;ii++){
list.addElement(filesInDirectory[ii]);
}
}else{
list.addElement(file[i]);
}
}
if(file_string != null){
fTextArea.setText(file_string);
}else{
return false;
}
this.jList1.setModel(list);
}
return true;
}
the error says:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SoundsTrip.SoundBytePlaying.openFile(SoundBytePlaying.java:267)
at SoundsTrip.SoundBytePlaying.jButton1MouseClicked(SoundBytePlaying.java:210)
at SoundsTrip.SoundBytePlaying.access$000(SoundBytePlaying.java:43)
at SoundsTrip.SoundBytePlaying$2.mouseClicked(SoundBytePlaying.java:135)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6270)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
How is this caused and how can I solve it?
Read the 1st line of the exception stacktrace.
It’s been thrown in line 267 of class
SoundsTrip.SoundBytePlaying, inside theopenFile()method. Trackback it in your source code. It’ll contain something likeA
NullPointerExceptionmeans thatsomeObjectisnull. Using the period operator.in an attempt to access/invoke it will then throw this exception.There are basically two ways to fix it:
Instantiate it beforehand so that it’s not
null.Check it beforehand so that it can be bypassed when it’s
null.Which to choose depends on the sole functional requirement.
Unrelated to the concrete problem, the Java naming conventions state that package names should preferably be written in all lowercase.