java.lang.NullPointerException at com.jogamp.opengl.util.texture.spi.awt.IIOTextureWriter.write(IIOTextureWriter.java:59) - Stac

admin2025-04-16  4

I'm visualizing the file of 3d flux in xy plane projection from the file like this

VARIABLES="X","Y","Z","U","V","W"
ZONE F=FEPOINT ET=BRICK N=1331 E=1000
0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    
1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    
1.000000    1.000000    0.000000    1.000000    0.000000    0.000000    

with the below code:

package com.example.laminarflow;

import org.jzy3d.chart.AWTChart;
import org.jzy3d.chart.Chart;
import org.jzy3d.chart.factories.AWTChartComponentFactory;
import org.jzy3d.chart.factories.AWTChartFactory;
import org.jzy3d.colors.Color;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.rendering.canvas.Quality;
import org.jzy3d.plot3d.primitives.Scatter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class NavierStokes2DVisualization {
    public static void main(String[] args) {
        try {
            
            List<Coord3d> velocityData = readVelocityData("NavierStokesBox3D/grids/stokes_cavity3d2_uvw01_00.dat");

            Chart chart = new AWTChartFactory().newChart(Quality.Fastest());
            Scatter scatter = new Scatter(velocityData.toArray(new Coord3d[0]));
            scatter.setWidth(3.0f);
            scatter.setColor(Color.BLUE);
            chart.getScene().add(scatter);

            saveChartAsImage(chart, "D:\\Java\\windmodel\\wind_projection.jpg");

            System.out.println("Visualization saved to wind_projection.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static List<Coord3d> readVelocityData(String filePath) throws IOException {
        List<Coord3d> points = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] values = line.trim().split("\\s+");
            if (values.length >= 6) {
                double x = Double.parseDouble(values[0]);
                double y = Double.parseDouble(values[1]);
                double z = Double.parseDouble(values[2]);
                double u = Double.parseDouble(values[3]); 
                double v = Double.parseDouble(values[4]); 

                if (Math.abs(z - 0.1) < 0.0001) {
                    points.add(new Coord3d(x + u * 0.01, y + v * 0.01, 0)); 
                }
            }
        }
        reader.close();
        return points;
    }

    private static void saveChartAsImage(Chart chart, String fileName) throws IOException {
        if (chart instanceof AWTChart) {
            chart.render();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


            AWTChart awtChart = (AWTChart) chart;
            File file = new File(fileName);

            File parentDir = file.getParentFile();

            
            if (parentDir != null && !parentDir.exists()) {
                parentDir.mkdirs();
            }

            awtChart.screenshot(file);
            System.out.println("Chart saved successfully to " + fileName);
        } else {
            System.err.println("Error: Chart is not an instance of AWTChart.");
        }
    }
}

and getting the error

java.lang.NullPointerException
    at com.jogamp.opengl.util.texture.spi.awt.IIOTextureWriter.write(IIOTextureWriter.java:59)
    at com.jogamp.opengl.util.texture.TextureIO.write(TextureIO.java:686)
    at org.jzy3d.plot3d.rendering.canvas.CanvasAWT.screenshot(CanvasAWT.java:246)
    at org.jzy3d.chart.Chart.screenshot(Chart.java:282)
    at com.example.laminarflow.NavierStokes2DVisualization.saveChartAsImage(NavierStokes2DVisualization.java:82)
    at com.example.laminarflow.NavierStokes2DVisualization.main(NavierStokes2DVisualization.java:32)

I tired just to sleep between the render and saving the file, but this doesn't help much

转载请注明原文地址:http://www.anycun.com/QandA/1744790476a87664.html