在debbuger中,来自fisier
的变量poze = Arrays.<File>asList(fisiere);
显示为null
,但我无法弄清楚原因。
任何帮助或建议都将不胜感激。
我已在此处粘贴了我的文件:
GUI http://pastebin.com/ZhHAGNxK
电话簿课程http://pastebin.com/KUk05eRd
控制台错误:
Exception in thread "Timer-0" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3807)
at java.util.Arrays.asList(Arrays.java:3794)
at cartedetelefon.GUI.GUI.displayBanner(GUI.java:656)
at cartedetelefon.GUI.GUI.access$000(GUI.java:27)
at cartedetelefon.GUI.GUI$2.run(GUI.java:73)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
BUILD SUCCESSFUL (total time: 7 seconds)
修改
SSCCE中的错误。
下面的代码package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
class testClass extends javax.swing.JFrame {
private List<File> poze;
private Timer timer1 = new Timer();
public testClass() {
initComponents();
javax.swing.Timer timer2 = new javax.swing.Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
}
});
timer2.start();
TimerTask task = new TimerTask() {
@Override
public void run() {
displayBanner();
}
};
timer1.schedule(task, 1000, 4000);
}
/**
* 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() {
jLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(224, Short.MAX_VALUE)
.addComponent(jLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void displayBanner(){
URL url = getClass().getResource("/GUI"); //edited, the path was incorrect
File dir = new File(url.getPath());
File[] fisiere = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".jpg");
}
});
poze = Arrays.<File>asList(fisiere);
int pic = (int) (Math.random()*3);
File f = poze.get(pic);
ImageIcon i = new ImageIcon(f.getAbsolutePath());
jLabel.setText("");
jLabel.setIcon(i);
}
/**
* @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(testClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testClass.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 testClass().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel;
// End of variables declaration
}