2

I need to count the occurrences and the relationships of strings.

For example, I should read a file like above:

(city)  (name)  (surname)
London  Johnny  Stuart
Leeds   Johnny  Bravo
Wigan   Richard Stuart
Bristol Richard Bravo
Bolton  Johnny  Bravo
York    Alex    Crowley
Bolton    null    null

And I need to count the number of surnames, unique names and list names associated with surnames, like the output above (surname, surname count, unique name count, list of names):

Stuart  2 2 Johnny, Richard
Bravo   3 2 Johnny, Richard
Crowley 1 1 Alex

I'm trying to implement a modified version of the MutableInt (Most efficient way to increment a Map value in Java) but unsuccessful.

Code:

class MutableInt{

    //Surname Count
    int value = 1;
        public void increment () { ++value;      }
        public int  get ()       { return value; }
    //Name List and Count
    List<String> name;
    int nameCount = 1;
        public void incrementName () { ++nameCount;      }
        public int getNameCount() { return nameCount; }         

        public void addName(String n){
            if(n != null && !name.contains(n)){
                name.add(n);
                incrementName();
            }
        }
        public String getName() {
            return Arrays.toString(name.toArray());

        }
        public String toString() {
        return (this.get()+
            "\t"+ this.getNameCount() +
            "\t"+ this.getName());
        }
}

HashMap Filling:

    Map<String, MutableInt> nameCount = new HashMap<String, MutableInt>();
    String l;
    while ((l = inputStream.readLine()) != null) {

       if (curLine++ < 1) {
            continue;
        }

        values = l.split("\t");
        String name = values[1];
        String surname = values[2];

        // Count Names
        MutableInt count1 = nameCount.get(surname);
        if (count1 == null) {
            nameCount.put(surname, new MutableInt());
            nameCount.get(surname).addName(name);

        }
        else {
            count1.increment();
            nameCount.get(surname).addName(name);
        }
}

Printing:

Iterator<Entry<String, MutableInt>> itrE1 = nameCount.entrySet().iterator();

    while(itrE1.hasNext()){
        Map.Entry<String, MutableInt> entry1 = itrE1.next();

        efileWriter.write(entry1.getKey()"\t"+ entry1.getValue().toString()+"\n");
   }

And this is the error I'm having:

java.lang.NullPointerException at diverenrich.InterfaceAgent$writeSummary$MutableInt.addGene(InterfaceAgent.java:626)
        at diverenrich.InterfaceAgent$writeSummary.action(InterfaceAgent.java:744)

Line 626 -> if(na != null && !name.contains(name)){ ... }
Line 744 -> nameCount.get(surname).addName(name);

Any tips?

Community
  • 1
  • 1
Jwojwo
  • 49
  • 5

1 Answers1

2

In your MutableInt you are not initializing name, so it is null -- hence the NPE when calling .contains() on a null object. You would need to initialize it to something, e.g.:

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

Also, on a different note, you may want to clean up your design a little, or at least choose more appropriate names for your objects, as your MutableInt appears to be far more than just a mutable integer.

Jason C
  • 38,729
  • 14
  • 126
  • 182