I want to create a matrix with LinkedLists anf fill all the cells with 0.  private LinkedList&lt;LinkedList&lt;T&gt;&gt; matrix; private int rows; private int columns;   // constructor public Matrix(int rows, int columns){     this.rows = rows;     this.columns = columns;     for(int i=0; i&lt;rows; i++){         for(int j=0; j&lt;columns; j++){              matrix.get(i).add(0);  // here I get the NullPointerException          }     } }   What am I doing wrong?
129
What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?  What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
46378
Can someone explained, as detailed as possible, the differences between the following types?  List List&lt;Object&gt; List&lt;?&gt;   Let me make this more specific. When would I want to use  // 1  public void CanYouGiveMeAnAnswer(List l) { }  // 2 public void CanYouGiveMeAnAnswer(List&lt;Object&gt; l) { }  // 3 public void CanYouGiveMeAnAnswer(List&lt;?&gt; l) { }
57295
I have a Trigger that is referencing a class. It works in Dev, but now I am trying to create the test class in order to push it to production. I am trying to test that a new user is created. I keep getting the same error System.QueryException: List has no rows for assignment to SObject. The error seems to point to the class on line 9 where the Account is added. The goal of the code is when a new user is inserted, then a contact for that new user is created as well. Does someone have any ideas why this is happening? I've used the Query Editor to make sure that the record does exist, and it does, so I'm stumped. Thanks in advance for the help.   Trigger      trigger UserInsertContactInsert1 on User (after insert, after update) {      if(Trigger.isAfter &amp;&amp; Trigger.isInsert){         Set&lt;ID&gt; usrIds = new Set&lt;ID&gt;();          for(User u : Trigger.new){             usrIds.add(u.id);         }          UserContactSyncClass1.syncContact(usrIds);       }      if(Trigger.isAfter &amp;&amp; Trigger.isUpdate){          Set&lt;ID&gt; idSet = new Set&lt;ID&gt;();         Set&lt;ID&gt; idSet2 = new Set&lt;ID&gt;();          for(User u : trigger.new){             for(User r : trigger.old){                 if(u.ID == r.ID &amp;&amp; u.isActive==false &amp;&amp; r.isActive==true){                     idSet.add(u.ID);                 }                  if (u.ID == r.ID &amp;&amp; u.isActive==true &amp;&amp; r.isActive==false) {                     idSet2.add(u.ID);                 }              }         }          UserContactSyncClass1.InactiveContact(idSet);         UserContactSyncClass1.ActivateContact(idSet2);     } }   Class  public class UserContactSyncClass1 {      @future     public static void syncContact(Set&lt;Id&gt; userSet){         List&lt;Contact&gt; cList = new List&lt;Contact&gt;();         List&lt;User&gt; userList = [Select ID, FirstName, LastName, Email, Phone, True_Market_Unit__c, True_Market_Name__c FROM User WHERE ID IN :userSet];         List&lt;String&gt; mList = new List&lt;String&gt;();         List&lt;String&gt; mktList = new List&lt;String&gt;();         **Account acc = [SELECT Id, Name FROM Account WHERE Name = 'Company Users' Limit 1];**          // Loop through Users, to get List of True Market Number         for(User usr : userList){             mList.add(usr.True_Market_Unit__c);                 }          // Use the True Market Number and Match to the True Market Number in Markets         for(Market__c mkt : [SELECT Name, Unit_Number__c FROM Market__c WHERE Unit_Number__c IN :mList]){             mktList.add(mkt.Name);           }          for(User usr : userList){             for(String mk : mktList){                 usr.True_Market_Name__c = mk;             }             System.debug(usr.True_Market_Name__c);         }          for(User u : userList){             cList.add(new Contact(FirstName=u.FirstName, LastName=u.LastName, User_True_Market_Name__c=u.True_Market_Name__c, Email=u.Email, Phone=u.Phone, OwnerID=u.ID, Related_User__c=u.Id, AccountId = acc.Id));         }          upsert cList;     }   Test Class Method with the Error  public static testMethod void validateNewUser(){          Profile p = [SELECT id FROM Profile WHERE Name='System Administrator'];          User u = new User(             FirstName = 'test',              LastName= 'last_test',              Email='test323232@test.com',              Phone='111-111-1111',              True_Market_Unit__c='1111',              alias = 'test',              EmailEncodingKey='UTF-8',             ProfileId = p.Id,             LanguageLocaleKey='en_US',             LocaleSidKey='en_US',              TimeZoneSidKey='America/Los_Angeles',             UserName = 'test323232@test.com'          );          System.runAs(u){             System.assertEquals('111-111-1111', u.Phone);         }      }   Updated Solution  Turns out even though my User never referenced the account, I still needed to insert it into my test class, with the Name='Company Users' from my SOQL query. Thanks everyone for all the help.
34074
Why this code isn't working? In my mind the next() method should move the cursor for the next item of the list...  List&lt;string&gt; listX = new List&lt;String&gt; {'A','B','C'}; while(listX.iterator().hasNext())     system.debug(listX.iterator().next());   When I try to run it I receive a error message of exceed a governor limit of CPU time. It's happend because the next() method doesn't move the cursor for the next list item. Every time returns "A".
88315
The code below works,  but when I add 10 more columns, it does not. So, how can I add ten more columns to the matrix below?  \documentclass{article} \usepackage{amsmath} \begin{document}  \begin{equation} \begin{bmatrix}  1 &amp; 0 &amp; 0 &amp; 0  &amp; 0 &amp; 1 &amp; 0 &amp; 0 &amp; 1 &amp; 0  \end{bmatrix} \end{equation}  \end{document}
43660
I have a list of structs and I want to change one element. For example :  MyList.Add(new MyStruct("john"); MyList.Add(new MyStruct("peter");   Now I want to change one element:  MyList[1].Name = "bob"   However, whenever I try and do this I get the following error:     Cannot modify the return value of   System.Collections.Generic.List.this[int]‘ because it is not   a variable   If I use a list of classes, the problem doesn't occur.  I guess the answer has to do with structs being a value type.  So, if I have a list of structs should I treat them as read-only? If I need to change elements in a list then I should use classes and not structs?
90115
I am trying to create an array of pointers. These pointers will point to a Student object that I created. How do I do it? What I have now is:   Student * db = new Student[5];   But each element in that array is the student object, not a pointer to the student object. Thanks.
79010
Take the following class as an example:  class Sometype {     int someValue;      public Sometype(int someValue)     {         this.someValue = someValue;     } }   I then want to create an instance of this type using reflection:  Type t = typeof(Sometype); object o = Activator.CreateInstance(t);   Normally this will work, however because SomeType has not defined a parameterless constructor, the call to Activator.CreateInstance will throw an exception of type MissingMethodException with the message "No parameterless constructor defined for this object." Is there an alternative way to still create an instance of this type? It'd be kinda sucky to add parameterless constructors to all my classes.
80312
In C# 3.0 you can create anonymous class with the following syntax  var o = new { Id = 1, Name = "Foo" };   Is there a way to add these anonymous class to a generic list?  Example:  var o = new { Id = 1, Name = "Foo" }; var o1 = new { Id = 2, Name = "Bar" };  List&lt;var&gt; list = new List&lt;var&gt;(); list.Add(o); list.Add(o1);   Another Example:  List&lt;var&gt; list = new List&lt;var&gt;();  while (....) {     ....     list.Add(new {Id = x, Name = y});     .... }
33737
If you take a look into this issue     I have provide an answer of using set&lt;Sobject&gt; instead using list&lt;Sobject&gt;  i.e.  if( !contacts.values().isEmpty() ){   set&lt;Contact&gt; setContacts = new set&lt;Contact&gt;();   for( Contact objCon : contacts.values() ){     if(!setContacts.contains(objCon)){         setContacts.add( objCon );     }   }  update new list&lt;Contact&gt;(setContacts); }   even if(!setContacts.contains(objCon)) is not needed  if( !contacts.values().isEmpty() ){   set&lt;Contact&gt; setContacts = new set&lt;Contact&gt;();   for( Contact objCon : contacts.values() ){         setContacts.add( objCon );   }  update new list&lt;Contact&gt;(setContacts); }   So is there any condition/use case where this answer might fail?
46764
I may be teaching a "Java crash-course" soon.  While it is probably safe to assume that the audience members will know Big-O notation, it is probably not safe to assume that they will know what the order of the various operations on various collection implementations is.  I could take time to generate a summary matrix myself, but if it's already out there in the public domain somewhere, I'd sure like to reuse it (with proper credit, of course.)  Anyone have any pointers?
9629
I use a lot of lists and arrays but I have yet to come across a scenario in which the array list couldn't be used just as easily as, if not easier than, the linked list.  I was hoping someone could give me some examples of when the linked list is notably better.
98518
Using extension syntax I'm trying to create a left-join using LINQ on two lists that I have. The following is from the Microsoft help but I've modified it to show that the pets list has no elements. What I'm ending up with is a list of 0 elements. I assume that this is because an inner-join is taking place. What I want to end up with is a list of 3 elements (the 3 Person objects) with null data filled in for the missing elements. i.e. a Left-Join. Is this possible?  Person magnus = new Person { Name = "Hedlund, Magnus" }; Person terry = new Person { Name = "Adams, Terry" }; Person charlotte = new Person { Name = "Weiss, Charlotte" };  //Pet barley = new Pet { Name = "Barley", Owner = terry }; //Pet boots = new Pet { Name = "Boots", Owner = terry }; //Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte }; //Pet daisy = new Pet { Name = "Daisy", Owner = magnus };  List&lt;Person&gt; people = new List&lt;Person&gt; { magnus, terry, charlotte }; //List&lt;Pet&gt; pets = new List&lt;Pet&gt; { barley, boots, whiskers, daisy }; List&lt;Pet&gt; pets = new List&lt;Pet&gt;();  // Create a list of Person-Pet pairs where  // each element is an anonymous type that contains a // Pet's name and the name of the Person that owns the Pet. var query =     people.Join(pets,                 person =&gt; person,                 pet =&gt; pet.Owner,                 (person, pet) =&gt;                     new { OwnerName = person.Name, Pet = pet.Name }).ToList();
44787
If you are creating a 1d array, you can implement it as a List, or else use the 'array' module in the standard library.  I have always used Lists for 1d arrays.  What is the reason or circumstance where I would want to use the array module instead?  Is it for performance and memory optimization, or am I missing something obvious?
84534
So I want to find the number of ways to fill an $m\times n$ matrix with only 0s and 1s such that each row and column has an even number of 1s.  I'm pretty stumped here.  I've set up m+n equations summing the respective rows and columns yielding 0 mod2, but other than that I'm not too sure of a clean way to approach this.  The subsequent part asks for the same thing except that there's an odd number of 1s.  Any hints and/or tips very much welcome.
65540
I've made a matrix in a beamer presentation and I'm trying to style the nodes.  \newcommand\foo{row 1/.style={inuse}} \begin{frame}{Chunks}   \tikzset{     inuse/.style={text=red},     free/.style={text=green},     addr/.style={minimum width=3em},     data/.style={nodes=draw,minimum width=6em},   }   \scalebox{0.6}{   \begin{tikzpicture}[node distance=2mm]     \matrix(magic) [matrix of nodes,ampersand replacement=\&amp;,row sep=-\pgflinewidth,                     column 1/.style={addr},                     column 2/.style={data, column sep=1em},                     %row 1 column 2/.style={inuse},                     %\foo ,                     %\foreach \r in { 1,2,3 } {row \r column 2/.style={inuse}, }                     column 3/.style={addr},                     column 4/.style={free, data, column sep=1em},                   ]     {       100: \&amp; 00000010 \&amp; 110: \&amp; 12345678 \&amp; 120: \&amp; 12345678 \&amp; 130: \&amp; 12345678    \\       101: \&amp; 00000001 \&amp; 111: \&amp; 12345678 \&amp; 121: \&amp; 12345678 \&amp; 131: \&amp; 12345678    \\       102: \&amp; 00000002 \&amp; 112: \&amp; 12345678 \&amp; 122: \&amp; 12345678 \&amp; 132: \&amp; 12345678    \\       103: \&amp; 00000003 \&amp; 113: \&amp; 12345678 \&amp; 123: \&amp; 12345678 \&amp; 133: \&amp; 12345678    \\     };   \end{tikzpicture} } \end{frame}   Of the three commented out lines, the first works and the other two fail. For the \foo macro I get:  Package pgfkeys Error: I do not know the key '/tikz/row 1/.style={inuse}' and I am going to ignore it. Perhaps you misspelled it.   But if I stick the macro body where I tried to use it, no problem. Clearly I'm missing something fundamental about macros, but what?  Invoking \foo in the tikzset gave the same result.  The foreach gives me something totally cryptic:  ! Undefined control sequence. \foreach ...reach \let \pgffor@assign@before@code                                                    =\pgfutil@empty \let \pgff...   Any help much appreciated.
38779
I'm new to Apex and development in general... So, I'm playing around coding and doing some badges in Trailhead. I created a small piece of code, as below, and have no actual use: public class GenderIdentifier {     public void DetermineGender(String Gender){                 if (Gender == 'Man'){             Gender = 'Male';             } else {             Gender = 'Female';         }            } }  If I make the method 'non-static' it works, and I can see it working fine executing as a anonymous block. If I make it static, I can't execute the anonymous block, as I get an Error like:  Non static method cannot be referenced from a static context: void GenderIdentifier.DetermineGender(String)  What am I doing wrong? It would make sense to have a method like this as Static. I think I misunderstood the concept of static methods.