-1

In order to index my collection of tweets I have written this code but an error appears I do not know is what I should initialize a few things, thank you.

public static void main (String args []) throws IOException{
        String line;
        Gson gson = new GsonBuilder().create();
        File f = new File(Constante.DATA);
        BufferedReader reader;
        Indexer indexer = new Indexer(Constante.INDEX);
        int nbrtweet =0, i=0;
                for(File file : f.listFiles())
        {
                System.out.println(file.getName());
        reader = new BufferedReader( new FileReader(file));
        while((line = reader.readLine())!=null){
        Tweet tweet;
                try{
           tweet = gson.fromJson(line, Tweet.class);
           }catch( JsonSyntaxException e){
            i++;
            continue;
                }
        nbrtweet++;
        indexer.indexTweet(tweet);
                }

The error shows up on this line:

 for(File file : f.listFiles())

and prints

Exception in thread "main" java.lang.NullPointerException
    at ApprocheTwitter.src.Indexer.IndexationThematiqueTwits.main(IndexationThematiqueTwits.java:26)
Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
celia
  • 29
  • 1
  • 6
  • Sorry but i did not understand the previous solutions – celia Jun 21 '17 at 18:52
  • @JonSkeet I disagree about this being a duplicate of a "simple" NPE error; if you know about the shortcomings of the Java `File` API, you know what I mean – fge Jun 21 '17 at 18:52
  • I do not know that's why I asked the question. thank you – celia Jun 21 '17 at 18:54
  • see my profile, my mail is in it; contact me there (in French, since I am French) and we will work that out; your problem comes from using the `File` API – fge Jun 21 '17 at 18:55
  • D'accord merci beaucoup. – celia Jun 21 '17 at 18:57
  • @fge: But that question provides the first part of diagnosing the error, which is to work out what is null. If the OP had identified what was unexpectedly null, I wouldn't have closed this as a dupe. – Jon Skeet Jun 21 '17 at 19:03

1 Answers1

0

You traced back the error to a line which reads:

for(File file : f.listFiles())

The problem is that since you are using the antiquated File API, the cause for errors here can be one of the following:

  • f is not a directory;
  • f is a directory, but you cannot read entries from it.

What is more, you create one reader per file but do not close it afterwards, which is a mistake; moreover, you don't even specify the encoding.

Now, the solution to this problem depends on whether you are using Java 7 or Java 8. In Java 8, it is really simple: use Files.list() and consume each file content ; with Java 7, use Files.newDirectoryStream().

In any event, as far back as 2011, File is dead; use JSR 203 instead.

fge
  • 110,072
  • 26
  • 223
  • 312
  • I work with java 8, And I want to specify that my collection contains files in json format – celia Jun 21 '17 at 19:09
  • Can you give me an example of how to used it? thank you – celia Jun 21 '17 at 19:27
  • Before I do so, can you tell whether the JSON stored in files is really on a single line, since this is what your existing code suggestst-? – fge Jun 21 '17 at 19:43
  • Yes of corse here is an example of this json: {"text":"Chef salad is calling my name, I\u0027m so hungry!","id_str":"28965131362770944","id":28965131362770944,"created_at":"dim. janv. 23 24:00:00 +0000 2011"}. – celia Jun 21 '17 at 19:47
  • Do you also have several tweets per file? Your existing code seems to say that one file may contain several tweets – fge Jun 23 '17 at 10:46
  • I can cook up a solution; however please note that it will be dependent on _you_ to adapt it to your reporting needs -- your current code does not do much – fge Jun 26 '17 at 16:39