0

I have made a Java class where I have defined a constructor and some methods but I get a NullPointer Exception, and I don't know how I could fix It.

public class Job {

    String idJob;
    int time;
    int timeRun;
    Job j1;

    List<Job> startBeforeStart;
    List<Job> restricted;

    Job(String idJob, int time){
        this.idJob=idJob;
        this.time=time;

    }


    public boolean isRestricted() {
        return restricted.size() != 0;
    }

    public void startsBeforeStartOf(Job job){
            startBeforeStart.add(job);
            job.restricted.add(this);
    }

    public void startsAfterStartOf(Job job){
            job.startsBeforeStartOf(this);
    }

    public void checkRestrictions(){

        if (!isRestricted()){
            System.out.println("+\n");
            }
        else{
            Iterator<Job> itR = restricted.iterator();
            while(itR.hasNext()){
                 Job j1 = itR.next();
                 if(time>timeRun){
                     System.out.println("-\n");
                     time--;
                 }
                 else {
                     restricted.remove(j1);
                 }
            }
        }
    }






    @Override
    public boolean equals(Object obj) {
        return obj instanceof Job && ((Job) obj).idJob.equals(idJob);
    }



     public void run() {
         timeRun++;
     }



}

PS Looking in a forum a user says that to fix the error I should make an ArrayList inside the constructor (without modify the received parameters that should remain String id and int time), but I haven't understand what He mean.

3
  • The title of this question is totally misleading. Your constructor can't throw a NPE Commented Mar 25, 2012 at 11:31
  • "Looking in a forum" On the web? The web is a big place, care to share a link to this thread? Commented Mar 25, 2012 at 11:41
  • I have solved, with the answer of Amit. @stefan The title doesn't says that the constructor should throw a NPE. Commented Mar 25, 2012 at 11:49

1 Answer 1

7

You are not creating an instrance of List<Job> for both the lists startBeforeStart and restricted - you only declare a variable, which is assigned with a null pointer.

Thus, whenever you try to access this List [for example: return restricted.size() != 0;] - you are trying to dereference a null pointer - which causes your NPE.

You should create an instance of the List - using the new operator [probably in the constructor].

Have a look at ArrayList and LinkedList and chose which is better for you.

For example, if you use to use an ArrayList for both, your c'tor should be something like:

Job(String idJob, int time){
    this.idJob=idJob;
    this.time=time;
    startBeforeStart = new ArrayList<Job>();
    restricted= new ArrayList<Job>();

}
Sign up to request clarification or add additional context in comments.

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.