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

  • Home
  • SEARCH
  • 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 7804359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:53:17+00:00 2026-06-02T01:53:17+00:00

I’m doing the iTunes U Stanford CS106a course in Java and am loving it

  • 0

I’m doing the iTunes U Stanford CS106a course in Java and am loving it but am tearing my hear out over this Name Surfer program.

When I use this code

public NameSurferEntry(String line) {
  int endOfName = line.indexOf(" ");
  name = line.substring(0,endOfName);
  String newLine = line.substring(endOfName+1);
  StringTokenizer tokenizer = new StringTokenizer(newLine);

  if (tokenizer.hasMoreTokens()) {
    name = tokenizer.nextToken();
  }

  for (int i = 0; tokenizer.hasMoreTokens(); i++) {
    array[i] = Integer.parseInt(tokenizer.nextToken());
  }
}

I get a Suspended/NullPointerException on the second line (int endOfName, etc.) that says “NameSurferEntry.(String) line: 16”.

If I try to do it this way:

public NameSurferEntry(String line) {
  StringTokenizer tokenizer = new StringTokenizer(line);
  name = tokenizer.nextToken();

  for (int i = 0; tokenizer.hasMoreTokens(); i++) {
    array[i] = Integer.parseInt(tokenizer.nextToken());
  }
}

I get a Suspended/NullPointerException with the message “StringTokenizer.(String, String, boolean) line: 182” followed by “Source not found.”

Does anybody have any ideas about what I’m doing wrong?

UPDATE

Thanks for the help.

NameSurferEntry is actually called by another class that provides a value for null. But after reading your suggestions I’ve declared line as an ivar but initialized it in the body of NameSurferEntry, and that fixes it. (Before I was both declaring and initializing line as an ivar. I don’t understand why the place I initialize it makes a difference, but there it is.)

The next error isn’t until line 48! So you may hear from me again very soon…. On my way to see whether I can figure it out on my own. Thanks again.

WHOOPS

Not so fast.

When I initialize line to an actual String in the body of NameSurferEntry, everything works out fine.

The problem is that NameSurferEntry is called by another method:

public NameSurferDataBase(String filename) {
  try {
    BufferedReader br = new BufferedReader(new FileReader(filename));

    while (true) {
      String blah = br.readLine();

      if (blah == null) {
        break;
      }

      NameSurferEntry entry = new NameSurferEntry(blah);
      hmap.put(entry.getName(), entry);
    }
    br.close();
  }
  catch (IOException ex) {
    throw new ErrorException(ex);           
  }
}

(This method is in turn called by another method that gives it an actual file for filename, so we’re okay there.)

But if I’m passing the parameter blah to NameSurferEntry, and NameSurferEntry then initializes its parameter, then I’ve lost blah, right? My impulse is then to initialize line as an ivar outside of the method, but then I get the NullPointerException message again and I’m back where I started. Argh!

Thoughts?

  • 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-06-02T01:53:19+00:00Added an answer on June 2, 2026 at 1:53 am

    Your line argument is null. That’s why you are getting a NullPointerException while trying to operate on line (using indexOf and StringTokenizer on line will raise a NullPointerException if line is null- it has nothing to operate on).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.