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 6750499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:48:51+00:00 2026-05-26T12:48:51+00:00

I am writing a project that reads a file and sorts Words. This code

  • 0

I am writing a project that reads a file and sorts “Words”. This code compiles correctly, yet then it gives me a null pointer exception. Any Ideas?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Hashtable;

public class Lab {
   Hashtable<String, Word> words = new Hashtable<String, Word>();

   public void addWord(String s, int i) {
      if (words.containsKey(s)) {
         words.get(s).addOne();
         words.get(s).addLine(i);
      } else {
         words.put(s, new Word(s));
         words.get(s).addLine(i);
      }
   }

   public void main(String[] args) {
      System.out.println("HI");
      File file = new File("s.txt");
      int linecount = 1;
      try {
         Scanner scanner = new Scanner(file);
         System.out.println("HUH");

         while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            while (line != null) {
               String word = scanner.next();
               addWord(word, linecount);
            }
            linecount++;

         }
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
   }
}

The exception’s stacktrace is:

java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27‌​1)
  • 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-26T12:48:52+00:00Added an answer on May 26, 2026 at 12:48 pm

    This whileloop is strange:

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        while (line != null) {
           String word = scanner.next();
           addWord(word, linecount);
        }
        linecount++;
    }
    

    If your input file is:

    a
    b
    

    Then scanner.nextLine() would be return a, then scanner.next() would return b, because nextLine returns the next end-line delimited String, and next returns the next token from the input file. Is this really what you want? I’d suggest trying this:

    while (scanner.hasNextLine()) {{
        String word = scanner.nextLine();
        addWord(word, linecount);
    
        linecount++;
    }
    

    Keep in mind that this would only work if there’s only a word per line. If you want to handle multiple words per line, it’d be slightly longer:

    while (scanner.hasNextLine()) {{
        String line = scanner.nextLine();
    
        Scanner lineScanner = new Scanner(line);
        while(lineScanner.hasNext()) {
            addWord(lineScanner.next(), linecount);
        }
    
        linecount++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing my first project that will use autoconf and teaching it to
I'm starting a project that involves writing mailets for Apache James . As far
My current project involves writing Perl code inside a Solaris VMWare appliance (hosted on
When writing PHP code for any given project, do you find you can write
Has anyone done something like this? How? I'm just starting a project that will
I'm using a FileManager for a project so that reading and writing is less
I'm writing a Java class that connects to a server and reads messages in
I am writing a project, which can read Japanese characters from file and do
This is a test program that I have written for a larger project that
I am writing a project in C++ for an embedded system with no OS

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.