1

I have written this code to insert a new node at any position of the "Linked List"

I know there is build-in library in Java; I am writing this as a practice for Java.

But I get error

java.lang.NullPointerException

I don't know where the problem. Here is my code:

 public void insertAtPos(int val , int pos) {

        Node nptr = new Node(val, null);                
        Node current = start;

        /*  Crawl to the requested index */
        for (int i = 0; i < pos; i++){
            current = current.getLink();
        }

        /*  Set the new node's next-node reference to this node's next-node reference  */
        nptr.setLink(current.getLink());

        /*  Set the new node's next-node reference to the new node  */
        current.setLink(nptr);

        size++ ;
  }     

/*  Function to set link to next Node  */
public void setLink(Node n) {
    link = n;
} 

/*  Function to get link to next node  */
public Node getLink() {
    return link;
} 
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
Hengameh
  • 892
  • 7
  • 12
  • What value are you using for `pos`, and how many items are in the list? – Daniel Nugent Apr 22 '15 at 02:41
  • I have tried "pos" for 0, 1, and 2. My list has 3 nodes at first. Its should be working for any list with any number at any position. – Hengameh Apr 22 '15 at 02:44
  • @Hengameh At which line you're getting the nullpointer exception – Prudhvi Apr 22 '15 at 02:46
  • @AndersonVieira, I have already read that question. It couldn't help me. – Hengameh Apr 22 '15 at 02:47
  • when I set pos=2, this error happens. for pos=0 and pos=1, nothing happens. – Hengameh Apr 22 '15 at 02:47
  • 2
    in the middle your code is ok, @Hengameh, but please *check the bounds* ! (what happens on `pos==0`, what on `pos==size` ?..or even beyond^^) – xerx593 Apr 22 '15 at 02:47
  • 2
    @Hengameh It would help to see the error stack trace. – Anderson Vieira Apr 22 '15 at 02:48
  • The cause could be with variable "start". Where did you initialize it? – Eranda Apr 22 '15 at 02:50
  • Thank you, I will check pos==0, pos==size and start as well. does anybody know what "nptr" and "ptr" stands for? – Hengameh Apr 22 '15 at 02:54
  • Your code will throw an NPE if `start` is `null`, or if `pos` is greater than or equal to the length of the list (supposing the last element's link is `null`). If neither of those is the case, but the code presented nevertheless throws an NPE, then your links are somehow buggered. – John Bollinger Apr 22 '15 at 02:55
  • 1
    "does anybody know what "nptr" and "ptr" stands for?" There is no `ptr` in your code. There is an `nptr`, and its meaning seems clear, but if this is *your* code then how can you not know what it represents? – John Bollinger Apr 22 '15 at 02:57
  • This is the code I have written, but I see in other codes, which I am trying to get help from them, they use "nptr" for their first node, and then "ptr" for the second one. I am just wondering if they have special meaning. I searched on net as well, but I couldn't find any reasonable answer for that. – Hengameh Apr 22 '15 at 03:00
  • @AndersonVieira, "error stack trace", could you please tell me how to do that? – Hengameh Apr 22 '15 at 03:02
  • error stack trace - call stack from the start of the java thread to the point of error. When an error occur you will see this printing in your logs. What do you see other than java.lang.NullPointerException contains the error stack trace? – Eranda Apr 22 '15 at 03:09

2 Answers2

1

First you should make sure pos is no greater size. Second your insert algorithm seems wrong. To crawl to the requested index, it should be for (int i = 0; i < pos - 1; i++){ and you must handle pos == 0 to change the start node.

locoyou
  • 1,697
  • 1
  • 15
  • 19
1

If you're using an IDE (I suggest you do) e.g. Eclipse, put a line break at the start of your method. If you don't know how, you can see this link. Then try running your application in Debug Mode. By this you can go through each of your variable and inspect if it contains null value.

This link will give you better understanding: here

Community
  • 1
  • 1
anonymouse
  • 109
  • 2
  • 9