Gson.toJson throws a NullPointerException when file size> 1 GB

I tried to write the Json format in Java, but ran into a NullPointerException when the file size is> 1 GB. Can someone help me solve this problem?

Generation of Json files is saved in the code, and the file size increases. After a file size> 1 GB, the code throws an exception, as shown below. I used different data for testing, so I do not think this is a data problem. I assume there is a size limit for Gson.toJson in Java.

My code is:

private HashMap<String,HashSet<Token>> tokenCounter = new HashMap<String,HashSet<Token>>(); .... private void writeToFile(){ try { PrintWriter out = new PrintWriter(outputFileName); out.println(gson.toJson(tokenCounter)); out.close(); } catch (IOException e) { e.printStackTrace(); } } 

The exception he chose:

 java.lang.NullPointerException at java.lang.String.<init>(String.java:301) at java.lang.StringBuffer.toString(StringBuffer.java:790) at java.io.StringWriter.toString(StringWriter.java:204) at com.google.gson.Gson.toJson(Gson.java:481) at com.google.gson.Gson.toJson(Gson.java:460) at com.ebay.classification.discovery.DailyDiscovery.writeToFile(DailyDiscovery.java:181) at com.ebay.classification.discovery.DailyDiscovery.run(DailyDiscovery.java:169) at com.ebay.classification.discovery.TestDailyDiscoveryContinue.run(TestDailyDiscoveryContinue.java:142) at com.ebay.classification.discovery.TestDailyDiscoveryContinue.main(TestDailyDiscoveryContinue.java:245) 
+6
source share
1 answer

Posted as an answer to get around formatting problems in the comments.

An array of 2 ^ 30 char will be 2 ^ 31 bytes. Like one line, it's huge! The obvious question to ask is why you have the code:

 PrintWriter out = new PrintWriter(outputFileName); out.println(gson.toJson(tokenCounter)); out.close(); 

This is easy to write as:

 FileWriter out = new FileWriter(outputFileName); gson.toJson(tokenCounter, out); out.flush(); out.close(); 

This will not have a significant effect on memory and will be much faster.

This does not answer the question of why you get NPE in a large StringWriter, but to be honest, what you do is absurd ....

+3
source

Source: https://habr.com/ru/post/944295/


All Articles