Estoy haciendo un reproductor de musica simple, hay un problema en el codigo el cual no localizo.
Error en cuestion:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at MainPackage.Windows.initComponents(Windows.java:275)
at MainPackage.Windows.<init>(Windows.java:27)
at MainPackage.Windows$14.run(Windows.java:505)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
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:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
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)
Windows.java:
package MainPackage;
import java.io.*;
import javax.swing.DefaultListModel;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class Windows extends javax.swing.JFrame {
ReproduceAudio pl=new ReproduceAudio();
ArrayList updateList= new ArrayList();
javazoom.jl.player.Player player;
File simpan;
/**
* Creates new form PlayerMemo
*/
public Windows() {
initComponents();
this.setLocationRelativeTo(null);
}
void updateList(){
updateList=pl.getListSong();
DefaultListModel model=new DefaultListModel();
for(int i=0;i<updateList.size();i++)
{
int j=1+1;
model.add(i,j + " | " + ((File) updateList.get(i)).getName());
}
jList1.setModel(model);
}
// panel de control
void add(){
pl.add(this);
updateList();
}
void remove(){
try {
int memoherdez=jList1.getLeadSelectionIndex();
pl.ls.remove(memoherdez);
updateList();
} catch (Exception e) {
}
}
void up(){
try {
int s1=jList1.getLeadSelectionIndex();
simpan=(File)pl.ls.get(s1);
pl.ls.remove(s1);
pl.ls.add(s1-1,simpan);
updateList();
jList1.setSelectedIndex(s1-1);
} catch (Exception e) {
}
}
void down(){
try {
int s1=jList1.getLeadSelectionIndex();
simpan=(File)pl.ls.get(s1);
pl.ls.remove(s1);
pl.ls.add(s1+1,simpan);
updateList();
jList1.setSelectedIndex(s1+1);
} catch (Exception e) {
}
}
void open(){
pl.openPls(this);
updateList();
}
void save(){
pl.saveAsPlaylist(this);
updateList();
}
File play1;
static int a=0;
void putar(){
if(a==0)
{
try {
int p1=jList1.getSelectedIndex();
play1=(File)this.updateList.get(p1);
FileInputStream fis=new FileInputStream(play1);
BufferedInputStream bis=new BufferedInputStream(fis);
player=new javazoom.jl.player.Player(bis);
a=1;
} catch (Exception e) {
System.out.println("Problema al tocar musica");
System.out.println(e);
}
new Thread()
{
@Override
public void run(){
try {
player.play();
} catch (Exception e) {
}
}
}.start();
}
else
{
player.close();
a=0;
putar();
}
}
File sa;
void next(){
if(a==0)
{
try {
int s1=jList1.getSelectedIndex()+1;
sa=(File)this.pl.ls.get(s1);
FileInputStream fis=new FileInputStream(sa);
BufferedInputStream bis=new BufferedInputStream(fis);
player=new javazoom.jl.player.Player(bis);
a=1;
jList1.setSelectedIndex(s1);
} catch (Exception e) {
System.out.println("Problema al tocar musica");
System.out.println(e);
}
new Thread()
{
@Override
public void run(){
try {
player.play();
} catch (Exception e) {
}
}
}.start();
}
else
{
player.close();
a=0;
next();
}
}
void previous(){
if(a==0)
{
try {
int s1=jList1.getSelectedIndex()-1;
sa=(File)this.pl.ls.get(s1);
FileInputStream fis=new FileInputStream(sa);
BufferedInputStream bis=new BufferedInputStream(fis);
player=new javazoom.jl.player.Player(bis);
a=1;
jList1.setSelectedIndex(s1);
} catch (Exception e) {
System.out.println("Problema al tocar musica");
System.out.println(e);
}
new Thread()
{
@Override
public void run(){
try {
player.play();
} catch (Exception e) {
}
}
}.start();
}
else
{
player.close();
a=0;
previous();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList<>();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setAutoscrolls(true);
jScrollPane1.setViewportView(jList1);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/6.png"))); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/2.png"))); // NOI18N
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/1.png"))); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/3.png"))); // NOI18N
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/4.png"))); // NOI18N
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/10.png"))); // NOI18N
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/9.png"))); // NOI18N
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/5.png"))); // NOI18N
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/7.png"))); // NOI18N
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MainPackage/img/8.png"))); // NOI18N
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jPanel2.setBackground(new java.awt.Color(0, 0, 0));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 349, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 377, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jButton10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8)
.addGap(35, 35, 35))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton9, javax.swing.GroupLayout.Alignment.TRAILING)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5)
.addGap(4, 4, 4)
.addComponent(jButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addGap(13, 13, 13)
.addComponent(jButton3)
.addGap(0, 122, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(23, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
putar(); // TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
up(); // TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
down(); // TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
add(); // TODO add your handling code here:
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
remove(); // TODO add your handling code here:
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
save(); // TODO add your handling code here:
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
open(); // TODO add your handling code here:
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
previous(); // TODO add your handling code here:
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
next(); // TODO add your handling code here:
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
player.close(); // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Windows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Windows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Windows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Windows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Windows().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JList<String> jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
private DefaultListModel DefaultListModel() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
ReproduceAudio:
package MainPackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class ReproduceAudio {
JFileChooser fc=new JFileChooser();
ArrayList ls=new ArrayList();
void add(JFrame frame){
fc.setMultiSelectionEnabled(true);
int fileValid=fc.showOpenDialog(frame);
if (fileValid==javax.swing.JFileChooser.CANCEL_OPTION) {
}else if(fileValid==javax.swing.JFileChooser.APPROVE_OPTION) {
File[] file=fc.getSelectedFiles();
ls.addAll(Arrays.asList(file));
}
}
ArrayList getListSong(){
return ls;
}
// save playlist
FileOutputStream fos;
ObjectOutputStream oos;
public void saveAsPlaylist(JFrame frame){
fc.setMultiSelectionEnabled(false);
int Valid=fc.showSaveDialog(frame);
if (Valid==javax.swing.JFileChooser.CANCEL_OPTION) {
}
else if(Valid==javax.swing.JFileChooser.APPROVE_OPTION) {
File pls=fc.getSelectedFile();
try {
fos=new FileOutputStream(pls + ".tgr");
oos=new ObjectOutputStream(fos);
for(int i=0;i<ls.size();i++){
File tmp=(File) ls.get(i);
oos.writeObject(tmp);
}
oos.close();
}
catch (IOException e) {}
}
}
FileInputStream fis;
ObjectInputStream ois;
public void openPls(JFrame frame){
fc.setMultiSelectionEnabled(false);
int Valid=fc.showOpenDialog(frame);
if (Valid==javax.swing.JFileChooser.CANCEL_OPTION) {
File pls=fc.getSelectedFile();
try {
fis=new FileInputStream(pls);
ois=new ObjectInputStream(fis);
File tmp;
while((tmp=(File) ois.readObject())==null){
ls.add(tmp);
}
if((tmp=(File) ois.readObject())==null){
ls.isEmpty();
}
ois.close();
} catch (IOException | ClassNotFoundException e) {}
} else {}
}
}
Me pide que agregue mas detalles...pero es lo que ya dije con anterioridad. Ademas de que estoy usando las dependencia de Jlayer 1.0.1