简体   繁体   English

Java NullPointerException在构造函数的类中

[英]Java NullPointerException In the constructor's class

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. 我做了一个Java类,其中定义了一个构造函数和一些方法,但是却收到NullPointer异常,而且我不知道如何解决它。

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. PS在论坛上看到一个用户说,要解决该错误,我应该在构造函数内创建一个ArrayList(而不修改接收的参数,这些参数应保持String idint time ),但我不明白他的意思。

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. 您不会同时为列表startBeforeStartrestricted List<Job>创建List<Job>startBeforeStart -您仅声明一个变量,该变量分配startBeforeStart指针。

Thus, whenever you try to access this List [for example: return restricted.size() != 0; 因此,每当您尝试访问此List [例如: return restricted.size() != 0; ] - you are trying to dereference a null pointer - which causes your NPE. ]-您正试图取消引用空指针-这会导致您的NPE。

You should create an instance of the List - using the new operator [probably in the constructor]. 您应该使用new运算符[可能在构造函数中]创建List的实例。

Have a look at ArrayList and LinkedList and chose which is better for you. 看一下ArrayListLinkedList然后选择哪个更适合您。

For example, if you use to use an ArrayList for both, your c'tor should be something like: 例如,如果您过去都使用ArrayList ,则您的c'tor应该类似于:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构造函数中的Java NullPointerException - Java NullPointerException in constructor 使用Mockito测试时,超类方法的构造函数中出现NullPointerException - NullPointerException in super class method's constructor when testing using mockito 在Java中,为什么Array的类构造函数是私有的? - In Java why Array's class constructor is private? Java NullPointerException-我可以告诉null对象的类吗? - Java NullPointerException - can I tell the null Object's class? 主类中的Java NullpointerException - Java NullpointerException in the main class Java日期类NullPointerException - Java Date Class NullPointerException Java-复制构造函数-java.lang.NullPointerException - Java - copy constructor - java.lang.NullPointerException 将C#转换为Java。 调用泛型类的构造函数的newInstance()始终返回NullPointerException - Converting C# to Java. Calling newInstance() of generic class' constructor always returns NullPointerException 在外部类的构造函数中实例化内部类是否危险? (JAVA) - Is it dangerous to instantiate an inner class within the outer class's constructor? (Java) 在JAVA中是否有在超类的构造函数之前执行子类的构造函数的方法? - Is there anyway in JAVA to execute Sub Class's Constructor before the Super Class's Constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM