0

With this code I am trying to load a file with data into an array of objects. I haven't initalized the fields within the object properly because when I run this code I get a NullPointerException. The array is there and is even the right size but the fields aren't initialized. How should I fix this?

Here is the code:

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;
  }
}

3 Answers 3

5

Declaring an array is not enough. You must populate it with object instances.

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(); }
Sign up to request clarification or add additional context in comments.

Comments

4

The problem is that the elements of the array are not initialized, i.e. still 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.

Comments

0

Another possibility is that you can use an ArrayList or LinkedList to take the place of the array in your program.

For example,

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

int index = 0;
list.add(0, new aJob(jobFile.nextInt());

does the same work. The last line creates a new aJob object with the value pulled from the scanner object, then inserts it at location 0.

While an array is a simple structure, using a List gives you greater flexibility, especially if you don't know just how many aJob elements you'll be making. An array requires that the size be defined at instanciation, but a List has the ability to grow to handle new elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.