Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ive created a DynamicNode class and a DynamicList class everything compiles prop

ID: 3670152 • Letter: I

Question

ive created a DynamicNode class and a DynamicList class everything compiles properly but this displayNth() method is giving me a null pointer everytime i run it. any idea whats wrong in the logic? THANKS!

this is my DynamicNode class (minus the getters and setters)

public class DynamicNode {
   int info;
   DynamicNode next;

   public DynamicNode(int x, DynamicNode n) {
       info = x;
       next = n;
   }

public void displayNth(int index) { //display the n-th node of the list

   DynamicNode current = list;

   while (current.info != index) {

      if (current.info == index) {

         system.out.print(current);

          return;

      }

      current = current.getNext();

    }

    system.out.print(“not found”);

}

Explanation / Answer

Answer:

Display N'th Node of the list check the below code with changes:

public void displayNth(int index)
{
DynamicNode current = list;
int count=0;
while (current.info != null)
{
if (count== index){
   System.out.println(current.info);
return;}
count++;
current = current.next;
}

  
System.out.println("Not Found");
  
}