Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Getting a NullPointerException error in the print statement, it is printing everything i input and need in the driver. But right after it causes the NullPointException error. I need this to work, I can not make any more methods afterwards because of this error.

I have a test on this tmr and I have no clue why it's causing this exception error. Please help! Thank you.

What I have tried:

Java
import java.util.Scanner;
import java.util.*;
import java.io.*;

public class StringNode 
{
   //attributes
   private String data;
   private StringNode link;
   //nodes counter
   int numNodes =0;
   
   Scanner kb = new Scanner(System.in);
   
   //constructor
   public StringNode(String iData, StringNode iLink)
   {
      data = iData;
      link = iLink;
   }
   
   //create empty list 
   StringNode list = null;
   
   
   
   //*********Create method: addToFrontFILI
   public void addToFrontBM(String newN)
   {
      list = new StringNode(newN, list);
      System.out.println("A node has been added to the front.");
      numNodes++;
      System.out.println("NumNodes:" +numNodes);
   }

   //---------------------Works but causes Nullpointer Exception------------
   //Create method: printLLFILI
   public void printLLBM() 
   {
      StringNode tptr = list;
      
      while(tptr.link != list)
      {
         System.out.println(tptr.data);
         tptr = tptr.link;
      }
      
      System.out.println(tptr.data);
      
   }
Posted
Updated 5-Dec-18 18:08pm
v2
Comments
Mohibur Rashid 5-Dec-18 23:59pm    
In a circular linked list you also need to know your previous link, your definition missing one.

1 solution

Quote:
Getting a NullPointerException error in the print statement, it is printing everything i input and need in the driver. But right after it causes the NullPointException error.

We can'tuse this piece of code to reproduce the problem because it is not autonomous, we can't run that code.
Change the code so that it run and generate problem without user input.
Quote:
I have a test on this tmr and I have no clue why it's causing this exception error.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900