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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:45:13+00:00 2026-06-15T17:45:13+00:00

I am getting the below error: Exception in thread main java.lang.NullPointerException at HashMap.CountLetters.tallyPrint(CountLetters.java:12) at

  • 0

I am getting the below error:

Exception in thread “main” java.lang.NullPointerException at
HashMap.CountLetters.tallyPrint(CountLetters.java:12) at
HashMap.CountLetters.main(CountLetters.java:21)

The goal is to get store the occurrences of each letter in a HashMap. The key is the letter and the value is the # of occurrences.

package HashMap;

import java.util.HashMap;

public class CountLetters {
    public HashMap tallyPrint(String phrase) {
        int count = 0;
        HashMap<String, Integer> fav = new HashMap<String, Integer>();
        for (int i = 0; i<phrase.length(); i++)
        {
            if (fav.containsKey(phrase.substring(i,i+1)))
                fav.put("" + phrase.substring(i,i+1), fav.get(phrase)+1);
            else
                fav.put("" + phrase.substring(i,i+1),1);
        }
        return fav;
    }

    public static void main(String[] args) {
        CountLetters x = new CountLetters();
        System.out.println(x.tallyPrint("my feet smell and my nose runs"));
    }
}
  • 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-15T17:45:14+00:00Added an answer on June 15, 2026 at 5:45 pm

    Replace
    fav.put("" + phrase.substring(i,i+1), fav.get(phrase)+1);

    with:

    fav.put("" + phrase.substring(i,i+1), fav.get(phrase.substring(i,i+1))+1);

    Side note1: You should extract that substring and store it in a temp variable to be more efficient so you don’t keep calling substring over and over for the same substring.

    Side note2: Thanks for posting the whole (with the main method) in your question. It’s make it simpler if all the askers did it like this.

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

Sidebar

Related Questions

I'm getting the error Exception in thread main java.lang.NumberFormatException: For input string: scores.txt at
I am getting below error in logcat. > E/AndroidRuntime( 3197): Uncaught handler: thread main
I am getting following excpetion Exception in thread AWT-EventQueue-0 java.lang.NoClassDefFoundError: org/divxdede/text/TimeFormat at org.divxdede.swing.busy.ui.BasicBusyLayerUI.<init>(BasicBusyLayerUI.java:102) at
i'm getting exception on Transformer transformer = tFactory.newTransformer(StreamXSL); but the error below is not
I have added the library ksoap2-android-assembly-2.4-jar-with-dependencies.jar but i am getting below error : Exception
I am getting the below error 12-30 05:40:40.484: ERROR/AndroidRuntime(413): Uncaught handler: thread Thread-10 exiting
I am having ListView with SimpleCursorAdapter Error I am getting :- java.lang.IllegalStateException: The content
while running: rake snorby:setup getting below error: rake aborted! You have already activated rake
I am getting the below error when attempting to connect to a Sybase server
I am getting the below error when executing my application on a Windows XP

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.