Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javafx NullPointerException when rendering big image

Tags:

java

image

javafx

I'm trying to render a .png Image using the GraphicsContext.drawImage(...) method under JavaFX 8. My code works perfectly fine for an image of size ~1000px x 2000px. But unfortunately I need to render an image of size 7000px x 14000px. Loading this image works fine as well, but when calling the drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight()) method I get the following error output:

java.lang.NullPointerException
at com.sun.prism.impl.BaseGraphics.drawTexture(BaseGraphics.java:389)
at com.sun.prism.impl.ps.BaseShaderGraphics.drawTexture(BaseShaderGraphics.java:139)
at com.sun.javafx.sg.prism.NGCanvas.handleRenderOp(NGCanvas.java:1228)
at com.sun.javafx.sg.prism.NGCanvas.renderStream(NGCanvas.java:997)
at com.sun.javafx.sg.prism.NGCanvas.renderContent(NGCanvas.java:578)
... more rendering stuff here
at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:469)
at com.sun.javafx.tk.quantum.ViewPainter.paintImpl(ViewPainter.java:317)
at com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:89)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:129)
at java.lang.Thread.run(Thread.java:745)

It doesn't make a difference if I try to resize the image while drawing to the canvas or if I try to render the whole image.

My guess is that the image size is simply too big to render, but I could'nt find any source to validate this and neither could I find anything to solve my problem.

I also made an analysis of the Java heap (with Eclipse Memory Analyzer) which showed an image size of approximately 376 MB.

So basically my question are: 1. Why is my program crashing? Is it because the image is too big? 2. If my image is too big, how can I increase the available space for Java? My machine has 8GB RAM and the graphics card has 1GB RAM, so an image of <400MB should not really be a problem.

like image 345
schauk11erd Avatar asked May 05 '14 08:05

schauk11erd


People also ask

How do I fix Java Lang NullPointerException exception?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

How do I avoid NullPointerException?

How to avoid the NullPointerException? To avoid the NullPointerException, we must ensure that all the objects are initialized properly, before you use them. When we declare a reference variable, we must verify that object is not null, before we request a method or a field from the objects.

What causes Java Lang NullPointerException?

Some of the common reasons for NullPointerException in java programs are: Invoking a method on an object instance but at runtime the object is null. Accessing variables of an object instance that is null at runtime. Throwing null in the program.

Can NullPointerException be extended?

NullPointerException is an unchecked exception and extends RuntimeException class.


1 Answers

As somebody pointed in comments - only up tp 4k/8k textures will work for you. This is because JavaFX probably uses GPU to render images. Texture size limit is probably the limit of yout GPU or GPU drivers that can't handle bigger textures. It can't create so big texture so it returns null (and that explains the NullPointerException).

The only way to fix it is to avoid using so big textures or get better hardware to support bigger textures. Using software rendering (which may be really slow) may fix it by running java with the following parameters:

-Dprism.order=j2d

or

-Dprism.order=sw

But I'm not sure it's what you want to achieve.

like image 59
barteks2x Avatar answered Sep 29 '22 14:09

barteks2x