-1

I have created a Boolean method called trainer() inside the class Trainer which extends the abstract class Employees(). This method returns true if the Employee inside the list is a trainer and then it prints it. But when I execute the program there seems to be an exception with the Boolean method trainer. As for the ItemEmployees this is a class which expands a class called Item().

If needed I can provide both their codes. But I do not think the problem is in those classes.

The following is written within the main class:

Node firstnode = list.getFirst(); 
ItemEmployees t = (ItemEmployees) firstnode.getValue(); 
for(Node tmp=firstnode; tmp!=null; tmp=tmp.getNext())
{
        if(tmp.getValue()!=null)
         {
                Employees e = t.keye();
                if( e.trainer() )
                {
                        e.print();
                }
        }
}

Now this is inside the Trainer() class:

public boolean trainer()
{
    return true;
}

Exception

Exception in thread "main" java.lang.NullPointerException at MainClass.main(MainClass.java:348)

Line 348 is

if( e.trainer() )
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Mar Arg
  • 1
  • 1

1 Answers1

0

Your problem is most certainly that either e is null, or failing that, trainer() is returning null. I haven't seen your entire class, but your trainer() method looks tentative but solid. Check your initialization.

If, by the time you're finished, you must have trainer() return a value that may be unknown (null), consider using Java 8's Optional class to avoid null pointer exceptions and make your code a little easier to debug. (See http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html and http://java.dzone.com/articles/optional-java-8-cheat-sheet).

Michael Macha
  • 1,729
  • 1
  • 16
  • 25