0

当我使用 Java 程序在 Ubuntu 中创建文件时,出现此错误:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at actions.create.actionPerformed(create.java:18)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

显然该文件没有创建,甚至尝试以root身份运行它但仍然没有。这是我的代码:

    File shortcut;
    shortcut = new File(global.location + "/" + global.name + ".desktop");

    try {
    FileWriter fWrite = new FileWriter(shortcut.getAbsoluteFile());
    BufferedWriter bWrite = new BufferedWriter(fWrite);
    shortcut.createNewFile();

    bWrite.write("[Desktop Entry]\nType=" + global.type + "\nTerminal=" + global.term + "\nIcon=" + global.icon + "\nExec=" + global.exec + "\nName=" + global.name);
    bWrite.close();
    } catch (IOException e1) {}

有人可以帮忙解决这个问题吗?我在使用 java 创建文件时总是遇到这个问题。

4

3 回答 3

0

你应该做

System.out.println(shortcut.getAbsoluteFile());

并仔细检查该目录是否真的存在。我有一种感觉,shortcut.createNewFile();因此抛出了 NPE。

于 2013-04-10T10:00:54.390 回答
0

实话告诉你,我不知道你在这里做了什么,看来你是在摸黑摸摸做档案。我想向您展示一个可以帮助您解决此问题的课程(我创建的):

package your.package;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileHelper
{
    public static boolean createFile(String dir)
{
    return FileHelper.createFile(dir, false);
}

public static boolean createFile(String dir, boolean isDirectory)
{
    boolean returning = false;
    boolean created = false;

    Path p = Paths.get(dir);
    try
    {
        if (Files.exists(p))
        {
            returning = true;
        }
        else if (isDirectory)
        {
            Files.createDirectory(p);
            created = true;
            returning = true;
        }
        else
        {
            Files.createFile(p);
            created = true;
            returning = true;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    if (returning)
    {
        Util.println((created ? "Created New " : "") + (isDirectory ? "Directory" : "File") + (created ? "" : " Already Exists") + " In: " + p.toString());
    }
    return returning;
}

public static boolean writeToFile(String fileName, String stuff)
{
    return FileHelper.writeToFile(fileName, stuff, true);
}

public static boolean writeToFile(String fileName, String stuff, boolean newLine)
{
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true)))
    {
        writer.write(stuff);
        if (newLine) writer.newLine();
        return true;
    }
    catch (IOException x)
    {
        System.err.format("IOException: %s%n", x);
        x.printStackTrace();
        return false;
    }
}

public static BufferedReader getFileReader(String fileName)
{
    Charset c = Charset.forName("US-ASCII");
    Path p = Paths.get(fileName);

    try
    {
        return Files.newBufferedReader(p, c);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return null;
}

public static boolean deleteDirectory(File dir)
{
    Util.requireNonNull(dir);

    if (!dir.exists() || !dir.isDirectory())
    {
        throw new IllegalArgumentException("\"dir\" Must exist!");
    }

    String[] files = dir.list();

    for (int i = 0, len = files.length; i < len; i++)
    {
        File f = new File(dir, files[i]);

        if (f.isDirectory())
        {
            deleteDirectory(f);
            Util.println("Found new directory to delete: " + f.getPath() + " Deleting now...");
        }
        else
        {
            f.delete();
            Util.println("Deleted File: " + f.getPath());
        }
    }
    Util.println("Deleted Directory: " + dir.getPath());
    return dir.delete();
}
}

让我知道这是否对您的问题有帮助!顺便说一句,你不需要为这个文件给我任何信用。它现在是公众!祝你好运,编码愉快!

于 2014-03-29T02:22:43.983 回答
0

尝试输入父文件夹和文件名的文件创建器:

File subFolder=new File(folderName,subFolder);
File f=new File(folderName,filename;)
System.out.println(f.getAbsolutePath()); //this to see the actual path of the file
subFolder.mkdirs();
BufferedWriter writer=new BufferedWriter(new FileWriter(f));
于 2013-04-10T10:06:49.813 回答