Explain an error in code below:

List<User> userList = [select id from User limit 1];

Set<User> usrs = new Set<User>();

Map<Id, User> usrMap = new Map<Id, User>(usrs);
Set<Id> usrIds = usrMap.keySet();

It just gives me

An internal error occurred during: "Execute-Anonymous". java.lang.NullPointerException

I worked with real code which has a lot of maps of sets (Map<Id, Map<Id, User>>), I'm passing them around my methods. I encountered this Set problem in Batch class. I thought it was old Batch file problem when you can't create it normally or edit. But it seems it's a Set problem. Hm... It seems it's a Map problem. I'm passing Set of users, but Map class doesn't have constructor with Set parameter. However, SFDC doesn't tell me anything about this. It just fires an internal error.

Why doesn't Salesforce tell me anything normal about Map constructor?

  • 1
    Set<Id> usrIds = usrMap.keySe(); it should be keySet() . Maybe a typo when you posted the question? Jan 5, 2015 at 22:45
  • Not sure if it has anything to do with your code but it can all be simplified to: Map<ID,User> usrMap = New Map<ID,User>([Select Id from User Limit 1]); Set<Id> usrIds = usrMap.keySet(); Try updating to that and see if it resolves the issue
    – Eric
    Jan 5, 2015 at 23:41
  • Code i posted is not real. That code is spread around a few methods, so it cant be simplified to a one kine of code. Jan 6, 2015 at 10:45

2 Answers 2


It appears to be a problem with the Apex compiler. It isn't rejecting the Set as an invalid parameter to the Map constructor (as per Lance's answer).

Note that you can't explicitly cast between Set, List and Map or perform an instanceOf check between them. Doing so gives a compiler error like:

Incompatible types since an instance of SET is never an instance of LIST

You can simplify reproducing the problem down to:

Set<User> usrs = new Set<User>();
Map<Id, User> usrMap = new Map<Id, User>(usrs);

In my case using anonymous Apex against API v31.0 I get the message:

UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 1535046690-155909 (-120809640)

The same code against the developer console causes it to hang on Executing > Sending Request > Unpacking results for me.

Depending on your support level you can either raise this directly as a bug from your Org or via the developer forums.

In the short term, convert your Sets to Lists before using them in the Maps constructor. You will need to use the List.addAll method for this.

  • Yes, everything is ok, except that I changed a lot of code and I need to search this problem 'manualy'. Jan 6, 2015 at 8:36

According to the document, the constructor of a map should take list as parameter, not set. Probably you can try with it. Link: map constructors

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .