1

I am working with Google Guava to use a Multimap like this for example {S1.E11=[S2.E236], S1.E108=[S2.E371]} . But I have a problem. I want to read from file the mapping entities and put them into my Multimap, it's something like indexing with hashmaps. So here's my file mapping.csv:

S1.E10;S2.E235;0.86
S1.E108;S2.E371;0.99
S1.E109;S2.E372;0.99
S1.E11;S2.E236;0.86
S1.E113;S2.E51;0.73

And here's my Java code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package indexing;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

public class Indexing {

    public static String getKeyOfValue(String value, Multimap<String, String> multiM) {
        String keyOfValue = new String();
        Set<String> setKeys = multiM.keySet();
        for (String key : setKeys) {
            if (multiM.get(key).contains(value)) {
                keyOfValue = key;

            }
        }
        return keyOfValue;
    }

    public static boolean removeValueOfKey(String key, String value, Multimap<String, String> multiM) {
        Collection values = multiM.get(key);
        for (Iterator<String> iterator = values.iterator(); iterator.hasNext();) {
            String element = iterator.next();
            if (value.equals(element)) {
                values.remove(element);
                return true;
            }

        }
        return false;
    }
    /*e0 devient representant de e1*/

    public static void caseRptRpt(String e0, String e1, Multimap<String, String> multiM) {
        multiM.put(e0, e1);
        Collection values;
        //marquer e0 comme representant de e1
        values = multiM.get(e1);
        for (Iterator<String> iterator = values.iterator(); iterator.hasNext();) {
            String element = iterator.next();
            //marquer les representées par e1 comme represetées par e0
            multiM.put(e0, element);
        }
        //supprimer e1 des representants avec ses representées
        multiM.removeAll(e1);

    }

    public static void main(String[] args) throws IOException {

        // TODO code application logic here 
        Multimap<String, String> multiMap = ArrayListMultimap.create();
        BufferedReader br = new BufferedReader(new FileReader("mapping.csv"));
        String line = null;
        String[] tabLine;
        String[][] array = new String[5][5];
        try {
            while (((line = br.readLine()) != null)) {
                line = br.readLine();
                //line = line.trim();
                tabLine = line.split(";");
            //System.out.print(tabLine[0]);
                //System.out.print("  ");
                //System.out.println(tabLine[1]);

                if (!multiMap.isEmpty()) {
                    //si e0 est representant et e1 est  representant
                    if (multiMap.containsKey(tabLine[0]) && multiMap.containsKey(tabLine[1])
                            && !(multiMap.containsValue(tabLine[0]) || multiMap.containsValue(tabLine[1]))) {
                        //e1 devient representant de e0
                        caseRptRpt(tabLine[1], tabLine[0], multiMap);
                    } //si e0 est representant et e1 est representé par une entité e
                    else if (multiMap.containsKey(tabLine[0]) && multiMap.containsValue(tabLine[1])
                            && !(multiMap.containsValue(tabLine[0]) || multiMap.containsKey(tabLine[1]))
                            && !(multiMap.containsEntry(tabLine[0], tabLine[1]))) {
                        //marquer e1 comme representé par e0
                        multiMap.put(tabLine[0], tabLine[1]);
                        //supprimer e1 des representées par l'entité e
                        removeValueOfKey(getKeyOfValue(tabLine[1], multiMap), tabLine[1], multiMap);
                    } //sinon si e0 est representé par e et e1 est representant mais ne contient pas e0 comme representé
                    else if (multiMap.containsValue(tabLine[0]) && multiMap.containsKey(tabLine[1])
                            && !(multiMap.containsKey(tabLine[0]) || multiMap.containsValue(tabLine[1]))
                            && !(multiMap.containsEntry(tabLine[1], tabLine[0]))) {
                        //marquer e0 representé par e1
                        multiMap.put(tabLine[1], tabLine[0]);
                        removeValueOfKey(getKeyOfValue(tabLine[0], multiMap), tabLine[0], multiMap);
                    } // si e0 et e1 sont representées par des entitées diff 
                    else if (multiMap.containsValue(tabLine[0]) && multiMap.containsValue(tabLine[1])
                            && !(multiMap.containsKey(tabLine[0]) || multiMap.containsKey(tabLine[1]))
                            && !(getKeyOfValue(tabLine[0], multiMap).equals(getKeyOfValue(tabLine[1], multiMap)))) {
                        //marquer e0 comme representé par e1
                        multiMap.put(getKeyOfValue(tabLine[1], multiMap), tabLine[0]);
                        removeValueOfKey(getKeyOfValue(tabLine[0], multiMap), tabLine[0], multiMap);
                    } //else if (multiMap.isEmpty()) {
                    //multiMap.put(tabLine[0], tabLine[1]);
                    //}
                } else {
                    multiMap.put(tabLine[0], tabLine[1]);
                }

            }
        } catch (Exception e) {
            System.err.println("Error: " + e);
            e.printStackTrace();
        }
//to here
        System.out.println(multiMap);

    }

}

When I run it here's what I got :

run:
Error: java.lang.NullPointerException
java.lang.NullPointerException
    at indexing.Indexing.main(Indexing.java:74)
{S1.E108=[S2.E371]}
BUILD SUCCESSFUL (total time: 0 seconds)

So it seems like it does not read the lines properly or something is wrong in my long if-else statement, so that the 2nd line is null. Any help or suggestions please? Thanks in advance.

JPerk
  • 325
  • 1
  • 2
  • 12
  • Well, did you run in the debugger and see what object reference is null? – OldProgrammer May 15 '15 at 02:21
  • @OldProgrammer Well yeah but it does not help at all, it seems like the if-else statement is escaped so that only readLine() works in the while loop, but it's strange that only the first line of the file is worked properly but not the rest, for example for the second line it's like it's null... – JPerk May 15 '15 at 02:24

1 Answers1

3

Why are you calling readLine() inside the while loop?

        while (((line = br.readLine()) != null)) {
            line = br.readLine();

When you call readLine() and it is not null, the line would have been read into line variable. Then inside the while loop you are making a second call to readLine(). This second call may result in null (ie. end of stream or file).

anonymous
  • 1,317
  • 8
  • 7