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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:57:57+00:00 2026-06-12T15:57:57+00:00

I have written one simple program in which there is a string array which

  • 0

I have written one simple program in which there is a string array which contains names.

This program searches for the name given by the user in the string array. If it is present, then it says name found otherwise not found.

When the I’m giving the name, i.e. already present in the string, then the program is working perfectly, but when I’m giving the name i.e. is not present in the string it shows the error.

import java.util.Scanner;

class Work {

    Scanner in = new Scanner(System.in);

    String e_name;
    String name[]=new String [50];

    void getname()
    {
         System.out.println("enter the name");
          e_name=in.nextLine();
    }

    int search()
    {
        System.out.println("name to be searched"+" "+e_name);
        for(int i=0;i<name.length;i++){
         if(name[i].equals(e_name))
         return i;
      }
        return -1;
    }
}

public class Array {

    public static void main(String args[])
    {
        Work ob1=new Work();
        int search_res;
        ob1.name[0]="aditya";
        ob1.name[1]="ankit";

        ob1.getname();

        search_res=ob1.search();
        System.out.println(search_res);

        if(search_res!=-1)
        {
            System.out.println("name found");
        }
        else if (search_res==-1)
        {
            System.out.println("name not found");
        }
    }
}

error

   enter the name
     manoj
     Exception in thread "main" java.lang.NullPointerException
 at Work.search(Array.java:24)
 at Array.main(Array.java:56)
     name to be searched manoj
  • 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-12T15:57:58+00:00Added an answer on June 12, 2026 at 3:57 pm

    You’re iterating over every value in the name array. That array looks like this:

    { "aditya", "ankit", null, null, null, ... }
    

    So for the first two iterations, it’ll be fine – but after that, when i is 2, this line:

    name[i].equals(e_name)
    

    will be calling equals on null, hence the exception.

    Leaving aside any issues about encapsulation, good design etc, the cleanest fix for this particular problem would be to use a List<String>:

    List<String> names = new ArrayList<String>();
    

    Then these lines:

    ob1.name[0]="aditya";
    ob1.name[1]="ankit";
    

    would become:

    ob1.names.add("aditya");
    ob1.names.add("ankit");
    

    And your loop would become:

    for (int i = 0; i < names.size(); i++) {
        if (names.get(i).equals(e_name)) {
            return i;
        }
    }
    

    Alternatively, you could stick with an array, and just reverse the equality check:

    if(e_name.equals(name[i]))
    

    Given that e_name will never be null (with the way you’re running it), this will never throw an exception. It’s still odd to have a hard-coded number of names though – a List<String> would be a better solution.

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

Sidebar

Related Questions

I have written this simple C program that changes the rotation file name. Eg:
I have written a simple CRUD form which has one select list. However the
I have written a simple database program in android. It runs fine, there is
I have a program written in C, which includes 2 functions, one function is
I have written simple j2me program with LWUIT package.I have added one Form in
I have written a simple memory game. You click on one of 16 cards
I am reading one file using ruby in which I have written regexp in
Here is my situation. I have written a WCF service which calls into one
I have a method somewhat similar to the one written bellow(this is just a
I have a very simple VB.net Windows Service written using VS.net 2008. The program

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.