這不會給我任何錯誤,但是當我編譯它返回(我做了聲明所有的變量/數組):什麼不對的Java代碼的?
線48:螺紋
for (int i = 0; i < listOfFiles.length; i++)
異常「主要的」 java .lang.NullPointerException 在modmaker.GuiBlocks2.main(GuiBlocks2.java:48)
package modmaker;
import java.awt.EventQueue;
public class GuiBlocks2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public JFileChooser filePath;
FileEditor fileeditor = new FileEditor();
/**
* Launch the application.
*/
static String files;
static String fileList = "";
static String path = "ModMaker";
static File folder = new File(path);
static File[] listOfFiles = folder.listFiles();
String[] allFile = fileList.split(":");
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiBlocks2 frame = new GuiBlocks2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// Directory path here
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
if (files.endsWith(".png") || files.endsWith(".PNG"))
{
fileList += files + ":";
files = listOfFiles[i].getName();
System.out.println(files);
}
}
}
}
/**
* Create the frame.
*/
public GuiBlocks2() {
setTitle("Linkseyi's ModMaker");
setBackground(Color.LIGHT_GRAY);
getContentPane().setBackground(Color.LIGHT_GRAY);
getContentPane().setLayout(null);
JButton btnGenerateFiles = new JButton("Generate Files");
btnGenerateFiles.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileeditor.addBlock();
}
});
btnGenerateFiles.setBounds(151, 120, 123, 51);
getContentPane().add(btnGenerateFiles);
final JComboBox textureChooseBox = new JComboBox(allFile);
textureChooseBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String imgName = (String)textureChooseBox.getSelectedItem();
System.out.println(imgName);
}
});
textureChooseBox.setBounds(151, 75, 123, 20);
getContentPane().add(textureChooseBox);
JLabel label1 = new JLabel("Choose Texture");
label1.setBackground(Color.LIGHT_GRAY);
label1.setBounds(169, 38, 123, 14);
getContentPane().add(label1);
setBounds(100, 100, 450, 233);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
凡'files'設置?確保在使用它們之前,listOfFiles和文件實際上已被設置。 – Nicholas
它給你當你編譯例外?或者當你嘗試運行它? – highlycaffeinated
您提供的哪一行代碼對應於錯誤的第48行? –