开发者

What's Wrong with this Java Code?

This doesn't give me any errors, but when I compile it returns(I DID declare all of the variables/arrays):

line 48:

for (int i = 0; i < listOfFiles.length; i++)

Exception in thread "main" java.lang.NullPointerException at modmaker.GuiBlocks2.main(GuiBlocks2.java:48)

package modmaker;

import java.awt.EventQueue;

public class GuiBlocks2 extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
public 开发者_Go百科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);
}
}


Listoffiles was not instantiated or files. Hard to say since we don't know what line 48 is. Update. With your new code modmaker cannot be found that is why it is null. You probably want /modmaker


In line 48, listOfFiles is null. It is defined as

static File[] listOfFiles = folder.listFiles(); 

From the Java API docs:

public File[] listFiles()

Returns: An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

That seems to be your error...


folder.listOfFiles();

didn't return back an array, it returned something that was null. So when you hit

static File[] listOfFiles = folder.listFiles();

the File arrray, listOfFiles was assigned to null. That means on line 48

for (int i = 0; i < listOfFiles.length; i++)

the subportion of that line

listOfFiles.length

throws a null pointer exception, because you just asked for

'null'.length

and null has no methods or properties to read.


It looks like listOfFiles is null.

Add check before the loop:

if (listOfFiles == null) {
    return;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜