0

I have the following function in a class named cache_level which extends the linked list class.:

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

and I have a function in main which uses a scanner to read a text file.:

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();

and for some reason that is giving me a NullPointerException and so far everyone I have talked to has no idea why that is happening. I have a class named Cache, which initializes cache_level. Reading the text file seems to be okay as I can print out the contents being stored into token but whenever I try to add it into the list, it gives me an error.

gohil90
  • 517
  • 5
  • 16
Sean M
  • 11
  • 1
  • Can you post the part of the stack where the error is given? – Noki Jan 25 '18 at 19:23
  • 1
    `O` is a really poor choice of name for a variable. It looks too much like `0`. Give your variables meaningful, unambiguous names. – Andy Turner Jan 25 '18 at 19:24
  • 1
    Don't compare a boolean to true like `if (x == true )`. Instead, just say `if (x )`, or `if (!x)` to check if it is false. It is too easy to make a typo and use a single = instead of ==, and that error is often very difficult to find. – FredK Jan 25 '18 at 19:29

0 Answers0