Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 未正确初始化的对象数组_Java_Arrays_Generics_Object_Nullpointerexception - Fatal编程技术网

Java 未正确初始化的对象数组

Java 未正确初始化的对象数组,java,arrays,generics,object,nullpointerexception,Java,Arrays,Generics,Object,Nullpointerexception,通过这段代码,我试图将一个包含数据的文件加载到一个对象数组中。我没有正确初始化对象中的字段,因为当我运行这段代码时,我得到了一个NullPointerException。数组在那里,甚至大小正确,但字段未初始化。我该如何解决这个问题 代码如下: public class aJob { public int job; { job = 0; } public int dead; { dead = 0; } public int profit; {

通过这段代码,我试图将一个包含数据的文件加载到一个对象数组中。我没有正确初始化对象中的字段,因为当我运行这段代码时,我得到了一个NullPointerException。数组在那里,甚至大小正确,但字段未初始化。我该如何解决这个问题

代码如下:

public class aJob {
  public int job;
  {
    job = 0;
  }
  public int dead;
  {
    dead = 0;
  }
  public int profit;
  {
    profit = 0;
  }
}

public class Main {
  public static void main(String[]args) throws IOException {
    File local = readLines();
    Scanner getlength = new Scanner(local);
    int lines = 0; 

    while (getlength.hasNextLine()) {
      String junk = getlength.nextLine();
      lines++;
    }
    getlength.close();

    Scanner jobfile = new Scanner(local);  // check if empty                            

    aJob list[] = new aJob[lines];
    aJob schedule[] = new aJob[lines];
    int index = 0;
    list[index].job = jobfile.nextInt();
  }

  public static File readLines() throws IOException 
  {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      // ignore exceptions and continue
    }

    JFileChooser chooser = new JFileChooser();
    try {
      int code = chooser.showOpenDialog(null);
      if (code == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile(); 
      }
    } catch (Exception f) {
      f.printStackTrace();
      System.out.println("File Error exiting now.");
      System.exit(1);
    }
    System.out.println("No file selected exiting now.");
    System.exit(0);
    return null;
  }
}

问题是数组的元素没有初始化,即仍然为空

aJob list[] = new aJob[lines]; // creates an array with null values.
for(int i=0;i<lines;i++) list[i] = new aJob(); // creates elements.

声明数组是不够的。必须用对象实例填充它

aJob list[] = new aJob[lines];
aJob schedule[] = new aJob[lines];

for (int i = 0; i < lines; i++){ list[i] = new aJob(); schedule[i] = new aJob(); }

另一种可能性是,您可以使用ArrayList或LinkedList来代替程序中的数组

比如说,

ArrayList<aJob> list = new ArrayList<aJob>(lines);
ArrayList<aJob> schedule = new ArrayList<aJob>(lines);

int index = 0;
list.add(0, new aJob(jobFile.nextInt());
做同样的工作。最后一行使用从扫描仪对象提取的值创建一个新的aJob对象,然后将其插入位置0

虽然数组是一种简单的结构,但使用列表可以提供更大的灵活性,特别是当您不知道要生成多少个aJob元素时。数组要求在实例化时定义大小,但列表可以扩展以处理新元素