Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp

Jil Van Wetter

Greenhorn
+ Follow
since Sep 09, 2013
Belgium
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
7
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jil Van Wetter

Matthew Brown wrote:I've only had a quick look, but one issue seems to be this. When you call find() (and that looks a strange name - I wouldn't expect a method called "find" to make any changes), the adding of the new value is inside the search loop. Let's say you've already got 1000 elements in the list - the next call will add 1000. Which means your collections will grow exponentially quickly, and I don't think that's what you intended.

On top of that, though, this does look like it could be greatly simplified. For instance, you might want to look at the "contains()" method.



Okay thank you for the fast and useful reply, you are right, in the else-loop he keeps adding items to the ArrayList, which was not my intention (stupid mistake). I will look into the contains method
5 years ago
Hi I changed my VM argument to:
-Xms1024m
-Xmx1024m
but I am still getting the java heap space error.

I am trying to fill three ArrayLists:
* the first one has all the different train numbers
* the second one has the total amount of a specific train number
* the third one has the distinct total amount of a specific train number (based on the values entered in an ArrayList called 'temporary')

Here is my code:



This is the error I am getting:


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at MainProgramPart3_1.find(MainProgramPart3_1.java:225)
at MainProgramPart3_1.main(MainProgramPart3_1.java:103)



Line 103 is where I call my method find.
Line 225 is shown in the code by "//this is line 225".

Thank you!
5 years ago

Ulf Dittmer wrote:Well, as I have said a number of times by now, you need to find out which Java object is null. Then you can work back from that and try to figure out *why* it is null (and then take further steps based on that). So far it seems -possibly a mistaken impression- that you're trying to guess what might be going wrong, instead of taking concrete steps to find out for sure what is going wrong.

And no, you don't need to determine for each file how big it is, you just need to write code that performs proper checks on the data it reads.


I have followed your advice and since there was no null in the ArrayList, I investigated the Excel file I edited and that is how I found out that the null occured after the last row with data.
I have changed this and the problem is fixed.

Stuart A. Burkett wrote:

Jil Van Wetter wrote:There must be a way to fix this (maybe an update button in Excel or something like that), since I have a lot of files to work with and I cannot enter for each file seperately the last row number with data in it.


I don't know if there is a way to fix the files in Excel, but you can fix the problem in your code by wrapping lines 14 - 23 in the code you posted above in an if statement that checks if cell is null



Edit: This of course assumes that it is the cell variable that is null. As Ulf said, the first thing you should do is find out which variable is causing the NPE.


This fixed the problem, thank you!
6 years ago

Ulf Dittmer wrote:I seem to recall that getLastRowNum does not return the last row number that has data, but the overall last row number (or something like that). So you need to check all the data you get from the file whether or not they actually contain valid data (or are null, since you keep getting NPEs).


I understand that, but in another program he gets the lastrownum with data in it but here the file hasn't been modified.
If I cut a part at the end of the file and save and get the lastrownum, I get the NPE.
There must be a way to fix this (maybe an update button in Excel or something like that), since I have a lot of files to work with and I cannot enter for each file seperately the last row number with data in it.
6 years ago

Ulf Dittmer wrote:You're confusing something in the spreadsheet having some value with a Java object being null. There may be a connection, but there needn't be.

But you're getting a NullPointerException, which means that some Java object is definitely null. The stack trace even tells you in which line that causes a problem. Finding out which object that is is the obvious next step in debugging this. That's why I keep asking about it.



Okay, I think I have found the problem.
I am working with CSV-files which I split up in several xlsx files.
I get the amount of rows from

int rows = Sname.getLastRowNum();


When I work with this value in a for loop, I get the NullPointerException.
But when I choose a fixed value for int rows (smaller than the amount of rows) I don't get the error.
I think Sname.getLastRowNum() takes a row where there is nothing filled in. Is this possible and how can I change this? Because it's really not practical to check each file for the last filled in row.
6 years ago

Ulf Dittmer wrote:Have you ascertained by now whether either TRAIN_NUM or cell is null?


I think neither are null, the cells in the column are never empty.
In the beginning ArrayList TRAIN_NUM is empty but it should be filled with the values from the Excel column TRAIN_NUM.
6 years ago

James Boswell wrote:

The ArrayList TRAIN_NUM is empty in the beginning, but that shouldn't be a problem.


That depends. How have you initialised the list?


Like this:


ArrayList <String> TRAIN_NUM = new ArrayList <String>();

6 years ago

Ulf Dittmer wrote:So either TRAIN_NUM or cell could be null - which one is it?


The ArrayList TRAIN_NUM is empty in the beginning, but that shouldn't be a problem. My column in Excel has no null values (see attachment).

I don't understand where this error is coming from.
6 years ago

James Boswell wrote:It would be more useful if you posted a snippet of your code where the problem is occurring rather than simply one line. That way, you are more likely to get help faster.


I hope this helps:

6 years ago
Hi

I have an Excel-file with a column called 'TRAIN_NUM'. Some examples of the trainnumbers are: 10, 1149, 1150/1, 1173/2, ...
We don't work with an Integer all the time so I decided to store these values as String values.


TRAIN_NUM.add(cell.toString());



But when I run the program I get the following error:
Exception in thread "main" java.lang.NullPointerException
at ReadTMSdata.main(ReadTMSdata.java:122)

But there are no gaps in the TRAIN_NUM column, so there are no null values. Any ideas?
Thanks and happy Halloween!
6 years ago
I get this error when I am trying to read from an Excel file:
Exception in thread "main" java.lang.IllegalStateException: Cannot get a numeric value from a text cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:845)
at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:208)
at ReadTMSData.main(ReadTMSdata.java:121)

But the value is numeric and not a text cell in Excel and the strange thing is when I use another Excel file with the same lay-out but with other values, he doesn't give an error.
Any ideas?
6 years ago

Ulf Dittmer wrote:How big is the file? POI is known for needing lots of memory - try upping the default memory allocation of the JVM using something like "java -Xms128m -Xmx512m ...".


The file is 19 MB.
These are the changes I made
Run>Run Configurations...>Java Application I am working in>Arguments tab>VM arguments:
-Xms768m
-Xmx1024m

After running it, it still shows an error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.apache.xmlbeans.impl.store.Cur.createElementXobj(Cur.java:257)
at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.startElement(Cur.java:2992)
at org.apache.xmlbeans.impl.store.Locale$SaxHandler.startElement(Locale.java:3198)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportStartTag(Piccolo.java:1082)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseOpenTagNS(PiccoloLexer.java:1473)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseTagNS(PiccoloLexer.java:1362)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseXMLNS(PiccoloLexer.java:1293)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseXML(PiccoloLexer.java:1261)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.yylex(PiccoloLexer.java:4808)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yylex(Piccolo.java:1290)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yyparse(Piccolo.java:1400)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:714)
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3439)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1270)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1257)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:138)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:130)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:286)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:207)
at ReadTMSdata.main(ReadTMSdata.java:86)
6 years ago

James Boswell wrote:You are missing the dom4j-x.x.x.jar this time. This is also available under <poi_root_directory>/ooxml-lib.

I would recommend adding all jars in this folder to your classpath.


Ok, thanks. I added all the jar files. But now I receive the following error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.io.ByteArrayOutputStream.toByteArray(Unknown Source)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:131)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:55)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:83)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
at ReadTMSdata.main(ReadTMSdata.java:86)

Any ideas?
6 years ago

James Boswell wrote:You are missing some necessary jar files from your classpath.

More specifically, you need to add the jar xmlbeans-x.x.x.jar (x.x.x is the version number) to your classpath. You should find it under <poi_root_directory>/ooxml-lib.


Thanks! I have added the jar file, but now I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:154)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:82)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
at ReadTMSdata.main(ReadTMSdata.java:86)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 8 more

Any ideas?

6 years ago

I get this error at the last line (line 86)

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at ReadTMSdata.main(ReadTMSdata.java:86)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

How can I fix this?
Thanks!
6 years ago