You never asked a question, so no, the question is not clear.
It may be "How do I determine if an object is null?" An answer:
o == null
It may be, "How might I determine that I'm at the end of a linked list?" An answer: you have the final node when the next node would be null.
It may be, "How might I represent an end-node node in a linked list?" An answer: it's the same as any node, but the field that would refer to the next node in the list is null.
It may be, "How can I have a field that can be both a list-node and, at times, null? Do I need some magical NodeONull, or...?" An answer: the field just has the type of your node, and you just assign it it null. Read section 4.1
It may be, "How can I have a field that can be both a String member of a list and, at times, some special value that serves as a sentinel for the list?" An answer: uh, what? The 'value' field of a node has nothing at all to do with the structure of the list. That you ask this question suggests that you're way out in the wilderness with your code, and should refer back to your course material on what exactly you're to deal with.
I'm aware that you spoke of doubly-linked lists; none of these answers change for them. Yes, I think that any one of above could be the answer you're looking for.
EDIT:
and firstNode is null object
Yeah, you're out in the wilderness.
Your sentinel is not a node, except technically.
Your sentinel does not have a .data field that you're ever interested in.
What you're actually doing here - or half-doing, as you get errors - is attemping to have special 'first' and 'last' and 'middle' nodes of a list. Three separate kinds of nodes, and maybe you've also thought of an 'empty list' object. Since this is dumb, probably you're doing it only because you don't understand what you're to do.
What you're to do is rather more like
class Cons {
public String data;
public Cons prev;
public Cons next;
Cons(String data, Cons prev, Cons next) { ... }
}
An empty list: null
A one-member list: new Cons("a", null, null);
A three-member list:
Cons list = new Cons("a", null, null);
list.next = new Cons("b", list, null);
list.next.next = new Cons("c", list.next, null);
Just as nothing much changes when you move from a linked to a doubly-linked list, nothing much changes when you move from Java's provided exceptional subtype of Cons (that is, null
) to your own subtype of it.