尝试将对象添加到列表中时,Java中的空异常指针不起作用

时间:2018-01-25 19:19:58

标签: java nullpointerexception

我在名为cache_level的类中有以下函数,它扩展了链表类。:

@SuppressWarnings("unchecked")
public boolean addObject(Object O)
{
    System.out.println(O);
    if(O != null)   
    {       
        boolean x = getObject(O);
        int I = 21;
        if(x == true && size() == cacheSize) //if object is found and size of list is full
        {

            remove(O);
            addFirst(I);
            cacheHit++; //increment to show we found the object
        }
        else if(x == true && size() != cacheSize)//found but not full
        {
            addFirst(I);
            cacheHit++;
        }   
        else if(x != true && size() == cacheSize) //item not found but list full
        {
            removeLast();
            addFirst(I);
        }
        else  //item not found and list not full
        {
            addFirst(I);
        }   

        return x;
    }
    else 
    {
        System.out.println("NULL OBJECT WAS FOUND");
        return false;
    }
}   

我在main中有一个函数,它使用扫描程序来读取文本文件。:

File file = new File(filename);
Scanner fileScan = new Scanner(file);
String line = "";
String token = "&";

while ((fileScan.hasNextLine())) 
{           
    line = fileScan.nextLine();

    Scanner lineScan = new Scanner(line);
    //System.out.println(token);
    while (lineScan.hasNext()) 
    {
        token = lineScan.next();
        //System.out.println(token);
        List.cacheAdd(token); //goes to cache class and initiallizes
        //System.out.println(token);
    }

    lineScan.close();
}
//System.out.println("CACHE WAS CREATED");      
fileScan.close();

由于某种原因,我给了NullPointerException,到目前为止,我所谈过的每个人都不知道为什么会这样。我有一个名为Cache的类,它初始化cache_level。阅读文本文件似乎没问题,因为我可以打印出存储到令牌中的内容,但每当我尝试将其添加到列表中时,它都会给我一个错误。

0 个答案:

没有答案