When posting exceptions, make sure you include the full stack trace along with the "Line" on which the exception has occurred.
Anyways, I have highlighted the problematic part of the posted code:
if(temp.num<n)
{
current= temp;
current.rlink.num=n;
if(temp.rlink==null)
{
temp.rlink.num=n;
System.out.println("Inserted at the right"+current.rlink.num);
}
My comments:
-
current.rlink.num=n;
: This part of code makes the assumption that the "current node" always has a right link which is non-null; this assumption is flawed. You don't even need this statement. -
temp.rlink.num=n;
: Again, this code is flawed since this piece is placed inside the IF checkif(tmp.rlink == null)
and hence will *always* cause NullPointerException.
When I say insert the value, you need to assign the "Node" object to rlink/llink instead of playing around with the "num" field. Like:
if(tmp.num < n) {
if(tmp.rlink == null) {
tmp.rlink = newNode;
} else {
tmp = tmp.rlink;
}
}