このコードを使用して、データを含むファイルをオブジェクトの配列にロードしようとしています。このコードを実行すると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;
}
}