Lỗi: java.lang.ArrayIndexOutOfBoundsException: 2304

sovo007007

Member
11/1/16
63
4
8
34
Em đang làm cái nhạc nhẽo và làm cái loa (biểu tượng trong Jlabel): Khi click on: xanh và chạy đi chạy lại bài hát; khi click lần nữa thì nó đỏ và tắt nhạc, click lại tiếp thì tương tự. Em đã làm và chương trình chạy khá được nhưng khi nhìn vô console thì báo lỗi sau: java.lang.ArrayIndexOutOfBoundsException: 2304. khi kiểm tra kỹ chương trình thì khi click đến lượt stop thì nó phải đợi bài hát đó hát hết thì mới stop. Em tìm hoại mà chưa ra lỗi mong các bậc tiền bối chỉ giúp.
----------
Java:
void coi() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String mp3 = "E:/Java/Applications of Java/ScanIp/Coi.mp3";
                FileInputStream fis;
                while (true) {
                    if (count % 2 == 0) {
                        try {
                            fis = new FileInputStream(mp3);
                            BufferedInputStream bis = new BufferedInputStream(fis);
                            player = new javazoom.jl.player.Player(bis);
                            jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/onAudio.png")));
                        
                        } catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (JavaLayerException ex) {
                            ex.printStackTrace();
                        }
                        try {
                            player.play();
                        } catch (JavaLayerException ex) {
                            ex.printStackTrace();
                        }
                    } else {
                        count = count % 2;
                        jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/offAudio.png")));
                        player.close();
                    }
                }
            }
        }).start();
    }
---------------
private void jlbAudioMouseClicked(java.awt.event.MouseEvent evt) {                                  
        count++;
        coi();
    }
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
2,889
1,291
113
Read this then re-edit your posting question so that people can see it better. Further you have to post the full exception message, not a piece of it.
 

sovo007007

Member
11/1/16
63
4
8
34
Bạn có thể cho mình xem full code của bạn được không? Nhìn vào đoạn code ở trên thì không thể xảy ra lỗi java.lang.ArrayIndexOutOfBoundsException được.
Ps: Lần sau bạn hãy post code vào thẻ cho cho dễ nhìn nhé. Minh vừa sửa lại rồi. Tham khảo vào viết của @Joe: https://congdongjava.com/forum/threads/how-to-post-a-question.22527
Đây A ơi:
package ScanThread;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.decoder.JavaLayerException;

import javazoom.jl.player.Player;

public class NewJFrame extends javax.swing.JFrame {

private Player player;
private int count = 1;

public NewJFrame() {
initComponents();
}

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jlbAudio = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
getContentPane().setLayout(new java.awt.GridBagLayout());

jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/offAudio.png"))); // NOI18N
jlbAudio.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jlbAudioMouseClicked(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 300, 0);
getContentPane().add(jlbAudio, gridBagConstraints);

jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ScanThread/StaticMap.jpg")));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
getContentPane().add(jLabel3, gridBagConstraints);

pack();
}// </editor-fold>

void coi() {
new Thread(new Runnable() {
@Override
public void run() {
String mp3 = "E:/Java/Applications of Java/ScanIp/Coi.mp3";
FileInputStream fis;
while (true) {
if (count % 2 == 0) {
try {
fis = new FileInputStream(mp3);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new javazoom.jl.player.Player(bis);
jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/onAudio.png")));

} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (JavaLayerException ex) {
ex.printStackTrace();
}
try {
player.play();
} catch (JavaLayerException ex) {
ex.printStackTrace();
}
} else {
count = count % 2;
jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/offAudio.png")));
player.close();
}
}
}
}).start();
}

private void jlbAudioMouseClicked(java.awt.event.MouseEvent evt) {
count++;
coi();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);

}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jlbAudio;
// End of variables declaration

}
 

sovo007007

Member
11/1/16
63
4
8
34
Bạn có thể cho mình xem full code của bạn được không? Nhìn vào đoạn code ở trên thì không thể xảy ra lỗi java.lang.ArrayIndexOutOfBoundsException được.
Ps: Lần sau bạn hãy post code vào thẻ cho cho dễ nhìn nhé. Minh vừa sửa lại rồi. Tham khảo vào viết của @Joe: https://congdongjava.com/forum/threads/how-to-post-a-question.22527
Chương trình vẫn chạy nhưng nó có 2 vấn đề ở đây:
- Khi em click để stop thì nó phải đợi hết đoạn audio mới tắt.
- trong console bào lỗi: java.lang.ArrayIndexOutOfBoundsException: 2304
 

quydtkt

Administrator
1/11/19
309
30
28
26
Lỗi nằn ở đây
Java:
            public void run() {
                String mp3 = "E:/Java/Applications of Java/ScanIp/Coi.mp3";
                FileInputStream fis;
                while (true) {
                    if (count % 2 == 0) {
                        try {
                            fis = new FileInputStream(mp3);
                            BufferedInputStream bis = new BufferedInputStream(fis);
                            player = new javazoom.jl.player.Player(bis);
                            jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/onAudio.png")));

                        } catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (JavaLayerException ex) {
                            ex.printStackTrace();
                        }
                        try {
                            player.play();
                        } catch (JavaLayerException ex) {
                            ex.printStackTrace();
                        }
                    } else {
                        count = count % 2;
                        jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/offAudio.png")));
                        player.close();
                    }
                }
            }
Vì vòng lặp while (true) chạy liên tục, khi click button stop thì ngoài dừng nhạc, nó còn chạy tiếp 1 lần nửa để mở bài khác. Do vậy, nhạc sẽ không bị dừng hoàn toàn và sẽ xảy ra lỗi java.lang.ArrayIndexOutOfBoundsException

Sửa lại thành
Java:
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import javax.swing.ImageIcon;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class NewJFrame extends javax.swing.JFrame {

    private Player player;
    private boolean played;

    public NewJFrame() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jlbAudio = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/offAudio.png"))); // NOI18N
        jlbAudio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlbAudioMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 300, 0);
        getContentPane().add(jlbAudio, gridBagConstraints);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ScanThread/StaticMap.jpg")));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(jLabel3, gridBagConstraints);

        pack();
    }// </editor-fold>

    void coi() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!played) {
                    String mp3 = "E:/Java/Applications of Java/ScanIp/Coi.mp3";
                    FileInputStream fis;
                    try {
                        fis = new FileInputStream(mp3);
                        BufferedInputStream bis = new BufferedInputStream(fis);
                        player = new Player(bis);
                        jlbAudio.setIcon(new ImageIcon(getClass().getResource("/pictures/offAudio.png")));

                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (JavaLayerException ex) {
                        ex.printStackTrace();
                    }
                    try {
                        played = true;
                        player.play();
                    } catch (JavaLayerException ex) {
                        ex.printStackTrace();
                    }
                } else {
                    played = false;
                    jlbAudio.setIcon(new ImageIcon(getClass().getResource("/pictures/onAudio.png")));
                    player.close();
                }
            }
        }).start();
    }

    private void jlbAudioMouseClicked(java.awt.event.MouseEvent evt) {
        coi();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jlbAudio;
// End of variables declaration

}
 
Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
2,889
1,291
113
@sovo007007

1) You should format your codes so that those who want to help could read it clearly. I won't help because of your unformatted codes .
Đây A ơi:
package ScanThread;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.decoder.JavaLayerException;
....
2) People are not soothsayers who can "predict" what was wrong in your codes if you just post a piece of codes where you think that it contains the error . If you know WHERE the error is then why don't correct it? Next time you should post the WHOLE part that leads to the problem.

Hope you understand and please don't do again with the unformatted codes and a small "piece" of codes. OK?


The codes are TOO complicate and spaghetti. A proposal:
PHP:
public class NewJFrame extends javax.swing.JFrame {

    private Player player;
    private boolean played = false;  // preset FALSE
    ...
    private void initComponents() {
        ...
        jlbAudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/offAudio.png"))); // NOI18N
        jlbAudio.addMouseListener(e->{   // lambda Expression
            played = !played; // reversed the current status
            coi();
        });
        ...
        pack();
    }
    // </editor-fold>
    void coi() {
        new Thread () { // direct without Runnable
            @Override
            public void run() {            
                try {
                    jlbAudio.setIcon(new ImageIcon(getClass().getResource("/pictures/offAudio.png")));
                    String mp3 = "E:/Java/Applications of Java/ScanIp/Coi.mp3";
                    FileInputStream fis = new FileInputStream(mp3);
                    while (played)  {
                      player = new Player(new BufferedInputStream(fis));
                      player.play();                
                    }
                    jlbAudio.setIcon(new ImageIcon(getClass().getResource("/pictures/onAudio.png")));
                    player.close();
                } catch (JavaLayerException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
    ...
    // End of variables declaration
}
 
Sửa lần cuối:
  • Like
Reactions: sovo007007