Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java错误:NullPointerException_Java_Nullpointerexception - Fatal编程技术网

Java错误:NullPointerException

Java错误:NullPointerException,java,nullpointerexception,Java,Nullpointerexception,我在这个世界上是全新的。我想做一个简单的文件移动程序。它只适用于1个文件,直到我为多个文件添加新代码为止。但是我想要更多,我在JFileChooser中添加了多个文件选择。为了移动文件,我在网上搜索,发现一些用户要求类似的东西。我试图将其放入我的代码中,但我得到了如下错误: 线程“main”java.lang.NullPointerException中出现异常 位于jfile.main(jfile.java:27) 第27行是:for(int i=0;i 阅读并理解OOP概念第27行是哪一行?第

我在这个世界上是全新的。我想做一个简单的文件移动程序。它只适用于1个文件,直到我为多个文件添加新代码为止。但是我想要更多,我在
JFileChooser
中添加了多个文件选择。为了移动文件,我在网上搜索,发现一些用户要求类似的东西。我试图将其放入我的代码中,但我得到了如下错误:

线程“main”java.lang.NullPointerException中出现异常 位于jfile.main(jfile.java:27)

第27行是:for(int i=0;i 这是代码,谢谢你,很抱歉我的英语不好

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JFileChooser;
import org.apache.commons.io.FileUtils;
public class jfile {
public static void main (String[] args) throws IOException{

    System.out.println("Creado por: MarcosCT7");

    if (new File(System.getProperty("user.home"), "\\AppData\\Roaming\\.minecraft\\mods").exists());{
        System.out.println("Seleccione el mod a instalar:");
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(true);
        int returnVal = chooser.showOpenDialog(chooser);

        if(returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println("Se está instalando " + chooser.getSelectedFile().getName());
                    File fuente = new File(chooser.getSelectedFile().getAbsolutePath());
                    File destino = new File(System.getProperty("user.home"), "\\AppData\\Roaming\\.minecraft\\mods");

                    File[] files = fuente.listFiles(); //thats new added
                    for (int i = 0; i < files.length; i++) {
                        File destFile = new File(destino.getAbsolutePath()+File.separator+files[i].getName().replace(",", "")
                                .replace("[", "") 
                                .replace("]", "")
                                .replace(" ", "")); //until here its new added
                        FileUtils.moveFileToDirectory(files[i], destFile, true); //changed to multiple move, before it was: FileUtils.moveFileToDirectory(fuente, destino, true);
                    }

                }

         else {
            if(returnVal == JFileChooser.CANCEL_OPTION) {
            System.out.println("No se ha seleccionado ningun mod. Adios.");
            }
        }
    }
}
}
导入java.io.File;
导入java.io.IOException;
导入java.nio.file.Files;
导入javax.swing.JFileChooser;
导入org.apache.commons.io.FileUtils;
公共类jfile{
公共静态void main(字符串[]args)引发IOException{
System.out.println(“Creado por:MarcosCT7”);
如果(新文件(System.getProperty(“user.home”),“\\AppData\\Roaming\\.minecraft\\mods”).exists(){
System.out.println(“Seleccione el mod a installar:”);
JFileChooser chooser=新的JFileChooser();
chooser.setFileSelectionMode(仅限JFileChooser.FILES_);
选择器。setMultiSelectionEnabled(真);
int returnVal=chooser.showOpenDialog(选择器);
if(returnVal==JFileChooser.APPROVE_选项){
System.out.println(“Se estáinstalando”+chooser.getSelectedFile().getName());
File fuente=新文件(chooser.getSelectedFile().getAbsolutePath());
File destino=新文件(System.getProperty(“user.home”),“\\AppData\\Roaming\\.minecraft\\mods”);
File[]files=fuente.listFiles();//这是新添加的
对于(int i=0;i
在使用循环遍历文件之前,请使用if语句进行检查:

if(files==null){
    System.out.println("Files not found");
}
else{
    for (int i = 0; i < files.length; i++) {
                    File destFile = new File(destino.getAbsolutePath()+File.separator+files[i].getName().replace(",", "")
                            .replace("[", "") 
                            .replace("]", "")
                            .replace(" ", "")); //until here its new added
                    FileUtils.moveFileToDirectory(files[i], destFile, true); //changed to multiple move, before it was: FileUtils.moveFileToDirectory(fuente, destino, true);
                }
}
if(文件==null){
System.out.println(“未找到文件”);
}
否则{
对于(int i=0;i
NullPointerException
表示变量不包含对对象的任何引用。我所指的引用是,例如:

String path="";
File f=new File(path);
if(f.exists()) {
   // do something
}
f是File类型的变量,它保存对由
path
现在你可以使用变量f,就像其他变量调用方法一样。 另一个例子

File f;

if(f.exists()) {
   // do something
}
现在,如果(f.exists()),您将在第
行中获得NullPointerException,因为f不包含任何引用

代码>新的< /COD>关键字被用来分配新的引用。JVM将负责所有的低层次细节。它类似于C和C++中的指针。在爪哇中,你不必显式删除对象。JVM垃圾收集器会处理这些事情。java是面向对象语言< /P>
阅读并理解OOP概念

第27行是哪一行?第27行是:for(int i=0;i