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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:31:53+00:00 2026-06-14T21:31:53+00:00

java.util.Hashtable’s null pointer exception is occuring in the following code when neither of the

  • 0

java.util.Hashtable’s null pointer exception is occuring in the following code when neither of the arguments to put() are null :

import java.util.Hashtable;

interface action
{
   void method();
}

class forclass implements action
{
  public void method()
  {
    System.out.println("for(...){");
  }
}

class ifclass implements action
{
  public void method()
  {
    System.out.println("if(..){");
  }
}

public class trial
{
  static Hashtable<String,action> callfunc;
  //a hashtable variable
  public static void init()
  {
    //System.out.println("for"==null); //false
    //System.out.println(new forclass() == null); //false
    callfunc.put("for",new forclass()); //exception occuring here
    callfunc.put("if",new ifclass());
    //putting values into the hashtable
  }
  public static void main(String[] args)
  {
    init(); //to put stuff into hashtable
    action a = callfunc.get("for");
    //getting values for specified key in hashtable
    a.method();
    callfunc.get("if").method();
  }
}

Exception in thread “main” java.lang.NullPointerException –

at trial.init(trial.java:33)

at trial.main(trial.java:38)

why is this exception occuring? how do i fix it?

  • 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-14T21:31:54+00:00Added an answer on June 14, 2026 at 9:31 pm

    You have not initialized your Hashtable: –

    static Hashtable<String,action> callfunc; // Reference points to null
    

    when neither of the arguments to put() are null

    You should rather use HashMap, which allows 1 null key, to avoid getting NPE when using put with null key, method which in case of Hashtable throws NPE, because it does not allow null keys, or value.

    So, change your declaration to: –

    static Hashtable<String,action> callfunc = new Hashtable<String, action>();
    

    or even better: –

    static Map<String, action> callfunc = new HashMap<String, action>();
    

    As a side note, you should follow Java Naming Convention in your code. All the class name and interface name, should start with UpperCase letter, and follow CamelCasing thereafter.

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

Sidebar

Related Questions

I am trying to run following program: package jndi; import java.util.Hashtable; import javax.naming.Context; import
import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event>
package datastrcutures; import java.util.*; public class java_hashtable { public static void ratingofcity() { Hashtable
I just saw the code of contains method in java.util.Hashtable.java. It has a loop
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.util.*; import java.io.*; public String recToString (boolean format) { Date date = new
import java.util.Arrays; import java.util.ArrayList; import java.util.Scanner; import java.util.Collections; import java.util.List; public class TennisTournament {
Is there any special reason for having the class names java.util.Hashtable with a small
I'm maintaining a Java web application that uses a java.util.Hashtable to cache values. This
As far as I know, java.util.Hashtable synchronizes each and every method in the java.util.Map

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.