java - 从文件读入构造函数时出现空行

标签 java arrays constructor

我在读取文件时遇到问题,我可以让它读取所有行,但使用下一行的第一条信息,否则它会在末尾添加一个空行,从而混淆构造函数数组。

import java.io.*;
import java.util.*;
public class StudentMain {
/**
 * @param args
 */

//(String fn, String ln, String pn, int id, boolean ft, int phn, boolean lj, String     con)
    //array and sorting variables
 public static studentConstructor[] stuArrayOrig;
 private static String[] stuArrayIdSort;
 private static String[] stuArrayNameSort;
 private static int i = 0;
 private static int lineCount = 0;
 private static int nElms = 0;
 //studentConstructor variables 
    public static String fn; //First Name
    public static String ln; //Last Name
    public static String pn; //Preferred Name
    public static int id;     //Student Id Number
    public static boolean ft;//Full-time Boolean
    public static int phn;   //Student Phone Number
    public static boolean lj;//Loving java Boolean
    public static String con;//Continuing 


 public static void StuRead()
 {

    Scanner inFile = null;
     try
     {
         inFile = new Scanner
                (new FileReader("Res/students.txt"));
     }
     catch (FileNotFoundException e)
     {
         // TODO Auto-generated catch block
         System.out.println("File Not Found");
         e.printStackTrace();
     }

        while (inFile.hasNextLine()){
                    inFile.useDelimiter(",|\\n"); //breaks the   lines into single info

                    ln = inFile.next();
                        System.out.println(ln);

                    fn = inFile.next();
                        System.out.println(fn);

                    pn = inFile.next();
                        System.out.println(pn);

                    id = inFile.nextInt();
                        System.out.println(id);

                    ft = inFile.nextBoolean();
                        System.out.println(ft);

                    phn = inFile.nextInt();
                        System.out.println(phn);

                    lj = inFile.nextBoolean();
                        System.out.println(lj);

                    con = inFile.next();
                        System.out.println(con);

                        studentConstructor st = new studentConstructor(ln, fn, pn, id, ft, phn, lj, con);
                        System.out.println(st);
                        //System.out.println("LINE");
                        stuArrayOrig[lineCount] = st;
                        inFile.nextLine();
                        System.out.println(stuArrayOrig[lineCount]);


                    lineCount++;`

文件输入示例:

Angelon Arouca,Lucas,,1472429,TRUE,407514687,TRUE,Confirmed
Cordovilla,Wilbert,,1472430,TRUE,407514688,FALSE,Confirmed
Danks,Joshua,,1472431,TRUE,407514689,TRUE,Confirmed

我得到的输出:

Angelon Arouca
Lucas

1472429
true
407514687
true
Confirmed

studentConstructor@29ba92bb
Exception in thread "main" java.lang.NullPointerException
at StudentMain.StuRead(StudentMain.java:73)
at StudentApp.main(StudentApp.java:10)

谁能看出我哪里出错了?

最佳答案

您从未初始化过 stuArrayOrig,因此它是 null,并且尝试访问它会导致 NullPointerException

一种方法是将其初始化为一些“安全”的大值:

public static studentConstructor[] stuArrayOrig = new studentConstructor[100];

但是使用自增长的ArrayList可能会容易得多:

public static List<studentConstructor> stuArrayOrig = new ArrayList<>();

关于java - 从文件读入构造函数时出现空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24096545/

相关文章:

c# - 为什么 System.Drawing.Bitmap 构造函数中的 "stride"必须是 4 的倍数?

Java openStream() 工作时间取决于网站代码?

java - 根据每个字符串的计算值拆分数组列表

java - 使用 Java 将 PDF 转换为 Postscript

python - numpy 在几个大型数组上出现内存错误

java - 如果使用相同的值构造,则使用现有实例

java - 对于带有 JAXB 注释的类,是否有类似 PostConstruct 的东西?

java - 将我的应用程序的 http 请求重定向到 spring webflow 中的外部网站

c++ - 写入数组时,最后一个线程比第一个线程执行得慢

字符计数程序,字符作为整数问题