-1

So i currently have this:

  public static JavaFact register(JavaFact jf){
    Scanner input = new Scanner(System.in);
    Map<String, Activity> activities = new HashMap<String, Activity>();
    Map<String, Activity> companyActivities = new HashMap<String, Activity>();

    activities = jf.getAllActivities //this is just to get all the existent 
   // activities into the Map activities

    System.out.println("How many activities is the user envolved in?");
    int count = input.nextInt();
    for(int i = 0; i<count; i++){
         System.out.println("Insert activity code");
         String code = input.nextLine();
         Activity a = activities.get(code);
         companyAcitivities.put(code, a.clone()); //the error refers to this line
    }
}

With this i'm getting a NullPointerException after running it on main. How can i solve this? What i want is for the user to insert the code of the Activity, and then add that Activity to the companyActivities wich will be the Map holding all the activities corresponding to the Company in question.

azro
  • 35,213
  • 7
  • 25
  • 55
  • What line of code does the error refer to? – Scott Hunter May 27 '18 at 17:50
  • Edited with that info – John Mulaney May 27 '18 at 17:52
  • may be the last one ? – André May 27 '18 at 17:53
  • `a` is null, simply. You are getting value from a map, where there may not be a value for `code` that user inputted. – Shadov May 27 '18 at 17:55
  • You surely ask an activity that does not exists with the String you typed, so a is null and give NPE – azro May 27 '18 at 17:55
  • What i don't understand is that if i get a System.out.println(activities.toString()) it clearly shows all the activities with respective codes... – John Mulaney May 27 '18 at 18:08
  • As @azro also said ,the activity you get from code must be null. To check this you can put a if block checking activities contains the provided code and also print the activity you got from map for that code. – Sachin May 27 '18 at 18:15
  • The problem is i really need those activity values to be stored on the companyActivities... Like the activities are all in a Map wich already has all the type of activities. After i do a activities.toString(), it shows me all those activities. Why isn't it getting the Activity? I can clearly see on my screen that the Activity has the code i'm entering... – John Mulaney May 27 '18 at 18:24

1 Answers1

1

To avoid your NullPointerException try it this way, as others already recommended in their comments above:

for(int i = 0; i<count; i++){
     System.out.println("Insert activity code");
     String code = input.nextLine();
     Activity a = activities.get(code);

     if(a != null) {
         companyAcitivities.put(code, a.clone()); //the error refers to this line
     }
}
Lynx 242
  • 7,658
  • 5
  • 22
  • 40
  • The problem is i really need those activity values to be stored on the companyActivities... Like the activities are all in a Map wich already has all the type of activities. After i do a activities.toString(), it shows me all those activities. Why isn't it getting the Activity? I can clearly see on my screen that the Activity has the code i'm entering... – John Mulaney May 27 '18 at 18:24
  • Could you provide the string representation of your Activities-Map? – Lynx 242 May 27 '18 at 18:46
  • https://gyazo.com/c77ce007fe559581ced143833ad5ce99 It's in another languade but codigo = code , designacao = name, then it's just stating that Activity allows deduction, and then the tax deduction value. – John Mulaney May 27 '18 at 19:02
  • I found a solution for you. Simply put this line `input.nextLine();` between `int count = input.nextInt();` and the for-loop. – Lynx 242 May 27 '18 at 20:18