Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:35:58+00:00 2026-05-26T09:35:58+00:00

Im having trouble with a constructor for a linked list. it takes a string

  • 0

Im having trouble with a constructor for a linked list. it takes a string and is supposed to create a node for every character.

i get a nullpointerexception everytime i try to print out the list. Does that mean that not even the first node is being created?

below is my node class and the list constructor.

class CharNode { 

   private char letter; 
   private CharNode next; 
   public CharNode(char ch, CharNode link)
    { 
        ch = letter;
        link = next;
    }

   public void setCharacter(char ch) 
    { 
        ch = letter;
    }

   public char getCharacter() 
    { 
    return letter;
    }
   public void setNext(CharNode next) 
    { 
    this.next = next;
    }

   public CharNode getNext() 
    {
    return next;
    }    
} 

and this is my constructor

   // constructor from a String 
   public CharList(String s) { 

    CharNode newNode = head;

    for(int i = 0; i <s.length(); i++)
    {
        newNode = new CharNode(s.charAt(i), null);
        newNode.setNext(newNode);
    }

    }

am i constructing it correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:35:58+00:00Added an answer on May 26, 2026 at 9:35 am

    As pcalcao said, = assigns the value on the right to the variable on the left. You’ll need to change ch = letter; to letter = ch; and link = next; to next = link;

    Now, the line CharNode newNode = head; doesn’t mean anything unless you’ve specified what head is prior to the code you’ve given, but it doesn’t look like it. Remember that when creating a linked list, you don’t start with anything, so even “special” nodes like head must be created (instantiated, if you prefer). The idea is to create the first node (the head), and then assign head to that first node. For every node after the first one, this step isn’t required, as you simply are adding to the end of the list.

    Finally, during construction, you’ll need a reference to both a new node (like you have now), and the previous node, in order to do proper appending. Your code now is simply setting the next node in the list to the current node, which means that once you create a new newNode, you will lose the reference to the previous newNode.

    A good way to approach linked lists when one is first getting started is to draw out what you want to do step by step, then try and translate that into code. Hopefully this is helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble implementing a nested class who's constructor is initialized with some of
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I am having trouble using a container with my custom class. Every time I
I'm having trouble adding a copy constructor to this class: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html I need to
I am having trouble understanding the principles of what a constructor is and how
I'm having trouble with my program crashing. I get that Program.exe has stopped working
This is a simple C++ constructor concept I'm having trouble with. Given this code
I'm having trouble calling a C++ constructor in my Visual Basic code. The DLL
I am having trouble initializing a parameterless constructor with a defined game. Somehow it
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.