试图在读取文件并获取错误时在循环中创建对象

发布于 2025-02-01 05:43:56 字数 2225 浏览 4 评论 0原文

我正在为学校创建一个读取文本文件,将数据存储到变量中的程序,然后创建一个将数据传递到以后使用的对象。目前,我正在使用扫描仪课程进行文件阅读。但是我有一个错误。

Exception at java.lang.NullPointerException
at FileRead.readfile(FileRead.java:22)
at FileRead.init(FileRead.java:73)
at FileRead.main(FileRead.java:13)

我不太确定什么是null指针异常是什么,但这是我来自fileread的代码:

主要方法并不那么有趣,但是这里是

public class FileRead {    
    public static void main(String args[]) throws FileNotFoundException {    
    final File folder = new File("/home/work/txt");    

    init(folder);        
}

我的init()方法,

public static void init(final File folder) throws FileNotFoundException {    
    String foo, bar;
    int slime, grit, ball, funk;
    
    for (final File fileEntry: folder.listFiles()) {
        if(fileEntry.isDirectory()) {
            init(fileEntry);    
        } else {
            readfile(fileEntry);
        }    
    }

readfile()

public static void readfile(final File folder) throws FileNotFoundException  {        
    int count = folder.list().length;        
    DT[] dts = new DT[count];    
    File file = folder;    
    String foo, bar;    
    int slime, grit, ball, funk;    
    Scanner input = new Scanner(file);        
    int i = 0;
    
    while (input.hasNextLine()) {        
        foo = input.nextLine();        
        bar = input.nextLine();
        slime = input.nextInt();
        grit = input.nextInt();
        ball = input.nextInt();
        funk = input.nextInt();

        dts[i] = new DT(foo, bar, slime, grit, ball, funk);
        i++;
        break;    
    }            
}

这里是 文件读取器,另一个类要简单得多。这是DT:

私人字符串foo,bar; 私人int粘液,砂砾,球,放克;

public DT(String foo, String bar, int slime, int grit, int ball, int funk) {
    this.foo = foo;
    this.bar = bar;
    this.slime = slime;
    this.grit = grit;
    this.ball = ball;
    this.funk = funk;
    
    ds(foo, bar, slime, grit, ball, funk);
}

public static void ds(String t, String g, int a, int st, int sn, int s) {    
    System.out.println(t + "\n" + g + "\n" + a + "\n" + st + "\n" + sn + "\n" + s);           
}

如果有帮助,我正在使用Ubuntu VM运行此操作。我真的很感谢您的帮助,谢谢!

I'm creating a program for school that reads a text file, stores the data into variables, and then creates an object to pass the data into for later use. I'm using a scanner class for file reading at this moment. I am getting an error however.

Exception at java.lang.NullPointerException
at FileRead.readfile(FileRead.java:22)
at FileRead.init(FileRead.java:73)
at FileRead.main(FileRead.java:13)

I'm not quite sure what null pointer exception is, but here is my code from FileRead:

The main method isn't all that interesting but here it is

public class FileRead {    
    public static void main(String args[]) throws FileNotFoundException {    
    final File folder = new File("/home/work/txt");    

    init(folder);        
}

Here is my init() method

public static void init(final File folder) throws FileNotFoundException {    
    String foo, bar;
    int slime, grit, ball, funk;
    
    for (final File fileEntry: folder.listFiles()) {
        if(fileEntry.isDirectory()) {
            init(fileEntry);    
        } else {
            readfile(fileEntry);
        }    
    }

Here is readfile()

public static void readfile(final File folder) throws FileNotFoundException  {        
    int count = folder.list().length;        
    DT[] dts = new DT[count];    
    File file = folder;    
    String foo, bar;    
    int slime, grit, ball, funk;    
    Scanner input = new Scanner(file);        
    int i = 0;
    
    while (input.hasNextLine()) {        
        foo = input.nextLine();        
        bar = input.nextLine();
        slime = input.nextInt();
        grit = input.nextInt();
        ball = input.nextInt();
        funk = input.nextInt();

        dts[i] = new DT(foo, bar, slime, grit, ball, funk);
        i++;
        break;    
    }            
}

This is all for the file reader, the other class is much simpler. Here is DT:

private String foo, bar;
privates int slime, grit, ball, funk;

public DT(String foo, String bar, int slime, int grit, int ball, int funk) {
    this.foo = foo;
    this.bar = bar;
    this.slime = slime;
    this.grit = grit;
    this.ball = ball;
    this.funk = funk;
    
    ds(foo, bar, slime, grit, ball, funk);
}

public static void ds(String t, String g, int a, int st, int sn, int s) {    
    System.out.println(t + "\n" + g + "\n" + a + "\n" + st + "\n" + sn + "\n" + s);           
}

I'm running this on an Ubuntu VM if that helps. I would really appreciate help with with this, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-02-08 05:43:56

您的DT课让我想知道,应该是数据类,不是吗?
但是您的数据类没有实例变量,也只提供静态方法来设置内容。关键字static将方法指定为类方法,这意味着只能通过dt.methodname()而不是通过对象访问它,也只能编辑静态变量,这意味着您不能将它们用于数据类。
NullPoInterException可能发生在此行中:
dts [i] .set(foo,bar,slime,grit,ball,funk);
即使您在此行中创建了一个固定尺寸的数组:
dt [] dts = new dt [count];
您还必须在每个索引上创建DT-Object。
像这样:
dts [i] = new dt();
NullPoInterException告诉您,一个变量没有分配的任何内容,意思是DTS [i]什么都指向(null),这就是为什么您首先必须为其分配一些东西才能使用方法Set()
我会为您的数据类推荐您这样的东西:

public class DT {
    private String foo, bar;
    private int slime, grit, ball, funk;
    public void ds() {
        System.out.println(foo + "\n" + bar + "\n" + slime + "\n" + grit + "\n" + ball + "\n" + funk);
    }
    public DT(String foo, String bar, int slime, int grit, int ball, int funk) {
        this.foo = foo;
        this.bar = bar;
        this.slime = slime;
        this.grit = grit;
        this.ball = ball;
        this.funk = funk;
    }
}

然后创建这样的对象:
dts [i] = new DT(foo,bar,slime,grit,ball,funk);
最后但并非最不重要的一点是,您应该使用类,方法和变量名称,这些名称更有意义,以便其他人知道该类是

错误的,因为folder.list.list()返回null,因为它是文件,您可以先对此进行测试。在带有.isdirectory()的init()中,而不是带有子目录/文件的目录。
要解决此问题,您可以输入固定的数字或根本不使用数组,因为您只有一个元素,因为在读取一个DT之后,您会破坏循环,

while (input.hasNextLine()) {        
        foo = input.nextLine();        
        bar = input.nextLine();
        slime = input.nextInt();
        grit = input.nextInt();
        ball = input.nextInt();
        funk = input.nextInt();

        dts[i] = new DT(foo, bar, slime, grit, ball, funk);
        i++;
        break;    // here you close the loop, regardless if input has a next line or not
    } 

但是,如果您在一个文件中有多个DTS,删除断点并使用arraylist

更新 - 用于新的错误nosuchelementException
此例外意味着您正在尝试读取一行,但是文件中没有剩下的行。
但是,您的文件中没有所有必需的元素,但是,如果要使代码更强大的使用异常处理,在此示例中,您需要类似的内容:

try{
    // your code here (readLine(), etc)
} catch(NoSuchElementException ex){
    // exception handling, like printing that the file doesn't have the correct format
    Syste.out.println("Wrong data format in file "+folder);
}

如我的示例,您可以看到您的可变名称实际上是误导的,因为“文件夹”实际上是文件
另外,正如我已经提到的那样,如果您的文件包含多个DTS,则在段循环中删除断点,或者一个文件仅包含一个DT,请使用简单的DT对象而不是列表

Your DT class got me wondering, it should be a data class, shouldn't it?
But your data class has no instance variables and also only provides static methods to set the content. The keyword static specifies a method as a class method, meaning that it can only be accessed over DT.methodName() and not over the object, also it can only edit static variables, meaning you can't use them for a data class.
The NullPointerException probably occurs in this line:
dts[i].set(foo, bar, slime, grit, ball, funk);
Even though you created a fixed-sized array in this line:
DT[] dts = new DT[count];
you also have to create the DT-object on each index.
Like this:
dts[i] = new DT();
A NullPointerException tells you, that a variable doesn't have anything assigned to it, meaning dts[i] points to nothing (null), that's why you first have to assign something to it to use the method set()
I would recommend you something like this for your data class:

public class DT {
    private String foo, bar;
    private int slime, grit, ball, funk;
    public void ds() {
        System.out.println(foo + "\n" + bar + "\n" + slime + "\n" + grit + "\n" + ball + "\n" + funk);
    }
    public DT(String foo, String bar, int slime, int grit, int ball, int funk) {
        this.foo = foo;
        this.bar = bar;
        this.slime = slime;
        this.grit = grit;
        this.ball = ball;
        this.funk = funk;
    }
}

and then create your object like this:
dts[i] = new DT(foo, bar, slime, grit, ball, funk);
Last but not least, you should use class, method and variable names that are more meaningful so that other people know what the class is for

The error occurs because folder.list() returns null, cause it is a file, you test this before in init() with .isDirectory(), and not a directory with sub-directorys/files.
To solve this problem you can just enter a fixed number or don't use an array at all, as you only have one element in it, because you break the loop after reading one DT

while (input.hasNextLine()) {        
        foo = input.nextLine();        
        bar = input.nextLine();
        slime = input.nextInt();
        grit = input.nextInt();
        ball = input.nextInt();
        funk = input.nextInt();

        dts[i] = new DT(foo, bar, slime, grit, ball, funk);
        i++;
        break;    // here you close the loop, regardless if input has a next line or not
    } 

However, if you have multiple DTs in one file, remove the break and use an ArrayList instead

Update - for new error NoSuchElementException
This exception means that you are trying to read a line, but there is no line left in the file.
Maybe your file doesn't have all the required elements in it, however, if you want to make your code more robust use exception handling, in this example you would need something like this:

try{
    // your code here (readLine(), etc)
} catch(NoSuchElementException ex){
    // exception handling, like printing that the file doesn't have the correct format
    Syste.out.println("Wrong data format in file "+folder);
}

as you can see at my example, your variable name is actually misleading, as the "folder" is in fact a file
also, as I already mentioned, if you're file contains multiple DTs remove the break in the while loop, or if one file only contains one DT, use a simple DT object instead of a list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文