1

I had to creat a visit card program in java.

package week21;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class _03_ {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Scanner scanner1 = new Scanner(System.in);
        int o1 = 0, i = 1;
        Map <String,Map<String,String>> Final = new HashMap<>();
        data1 info = new data1();
        while (o1 != 3) {
            System.out.println("What would you like to do:");
            System.out.println("1. Add");
            System.out.println("2. Search");
            System.out.println("3. Exit");
            o1 = scanner1.nextInt();
            if(o1 == 1){
                Map<String,String> save = new HashMap<>();
                System.out.println("Name of owner:");
                String Name = scanner.nextLine();
                save.put("Name", Name);
                System.out.println("Email:");
                String Mail = scanner.nextLine();
                save.put("Mail", Mail);
                System.out.println("Phone number:");
                String Nr = scanner.nextLine();
                save.put("Number", Nr);
                System.out.println("Address:");
                String Address = scanner.nextLine();
                save.put("Address", Address);
                System.out.println("Name of company:");
                String CName = scanner.nextLine();
                Final.put(CName, save);
            }
            else
            if (o1 == 2) {
                System.out.println("What would you like to do:");
                System.out.println("1. A piece of information");
                System.out.println("2. Everything about a company");
                System.out.println("3. Everything");
                int o = scanner.nextInt();
                if (o == 1) {
                    Search(Final);
                } else if (o == 2) {
                    Search2(Final);
                } else if (o == 3) {
                    Search3(Final);
                }
            }
        }
    }
    public static void Search (Map<String, Map<String, String>> Final){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Name of the company");
        String a = scanner.nextLine();
        System.out.println("What information would you like:");
        String b = scanner.nextLine();
        System.out.println(Final.get(a).get(b));
    }
    public static void Search2 (Map<String, Map<String, String>> Final){
        Scanner scanner = new Scanner(System.in);
        System.out.println("What do you know:");
        String s = scanner.nextLine();
        int c = 0;
        for (int i = 0; i < Final.size(); i++) {
            if(Final.get("Name").equals(s)){
                System.out.println(Final.get(i));
                c++;
            } else if (Final.get("Mail").equals(s)) {
                System.out.println(Final.get(i));
                c++;
            } else if (Final.get("Address").equals(s)) {
                System.out.println(Final.get(i));
                c++;
            } else if (Final.get("Phone number").equals(s)) {
                System.out.println(Final.get(i));
                c++;
            }
        }
        if(c == 0){
            System.out.println("It doesn't exist");
        }
    }
    public static void Search3 (Map<String, Map<String, String>> Final){
        System.out.println(Final);
    }
}
class data1{
    String Name;
    String Mail;
    String Nr;
    String Address;
    String CName;
}

When I try to run it it gives me this error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Map.equals(Object)" because the return value of "java.util.Map.get(Object)" is null at week21.03.Search2(03.java:69) at week21.03.main(03.java:48)

How can I fix it?

1 Answers1

2

You defined your map as Map <String,Map<String,String>> Final (please rename your variables, they should start with a lower letter).

As I read your add function correctly the inner map holds some information about a person and this map is stored under the company name. So to get the information you have to read the company name first. If you don't add a person with company name 'Name', 'Mail' and so on Final.get("Name") returns null and you get the NullPointerException. You can iterate over all persons with Final.entrySet().

for (Entry<String, Map<String, String>> entry : m.entrySet()) {
    Map<String, String> person = entry.getValue();
    // your if statements here
}

Additionally your if blocks will always print null because the keys of the map are String and you call get with an int.

Stefan Warminski
  • 1,845
  • 1
  • 9
  • 18