reset password
Author Message
afetiso
Posts: 84
Posted 16:21 Jan 20, 2014 |

try (BufferedReader in = new BufferedReader(new FileReader(getServletContext().getRealPath("/WEB-INF/Spanish.txt"))))
        {
            Map<String,List<String>> map = new HashMap<String,List<String>>();        // create map
            
            String eng;
            List<String> spn = null;
            while (in.readLine() != null){               //while we have lines in file
                
                String line = in.readLine();
                if (!line.startsWith("#")){                 //check is line start from #
                    String[] parts = line.split(" ");          //take all the words splitted by "space"
                    eng = parts[0];                             // first word english
                    for (int i = 1; i < parts.length; i++){            //all the rest words spanish
                        spn.add(parts[i]);                                //add spanish words in list
                    }
                    map.put(eng, spn);                              //add key word eng and all spn words in map
                }      
                
            }
            
            in.close();
        } catch (IOException e) {
            System.out.println("File not found!");
        }

 

It shows NullPointerException after TRY? Can someone help?

redge
Posts: 31
Posted 16:28 Jan 20, 2014 |
You're splitting on space characters, but in that text file, values are separated by tab characters. The impact this has on the resulting array is likely the cause of your NPE; try stepping through it with the Eclipse debugger.