0

I draw simple splines and I got code here. I editted code and receive NullPointerException in dataset.size(). I guess that my programm don't be at time filling all dataset points from files (Scanner works slowly) and it throws this error. I also guess that I need to add certain timer to wait while dataset == null. But how to make it?

public class SimpleGrapher2 extends JPanel {
    ...
    private static List<Double> scores;
    private static File[] sFiles;
    ...
    private static List<List<Point2D.Double>> dataset;
    private static int snumber = 0;

    public SimpleGrapher2(List<Double> scores) {
        SimpleGrapher2.scores = scores;
        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent me) {
                double x = me.getX();
                double y = me.getY();
                List<Point2D.Double> series = findNearPoint(dataset, x, y);
                editSerie(x, y, series);
                revalidate();
                repaint();
            }

            @Override
            public void mouseMoved(MouseEvent me) {}
        });
    }

    static JPanel paintingComponent = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            //painting X,Y axises
            drawSplines(g2); // painting splines
        }
    };

    public static void drawSplines(Graphics2D g2) {
        int precision = 10;
        for (int k = 0; k < dataset.size(); k++) { // NULLPOINTEREXCEPTION HERE
            List<Point2D.Double> series = dataset.get(k);
            int np = series.size();
            //algorithm and painting of splines
        }
    }

    public List<Point2D.Double> findNearPoint(List<List<Point2D.Double>> dataset, double x, double y) {
        //hidden part of code: to move points
    }

    public void editSerie(double x, double y, List<Point2D.Double> serie) {
        //hidden part of code: edit series in case of MouseDragged
    }

    public void readFromFiles() {
        dataset = new ArrayList<>();
        for (File polFile : sFiles) {
            List<Point2D.Double> series = new ArrayList<>();
            Scanner s = null;
            try {
                s = new Scanner(new File(polFile.getAbsolutePath()));
            }catch (FileNotFoundException ex) {
                System.out.println("Scanner error!");
            }
            s.useLocale(Locale.US);
            while (s.hasNext()) {
                double x = s.nextDouble();
                double y = s.nextDouble();
                series.add(new Point2D.Double(x, y));
            }
            dataset.add(series);
        }
    }

    //hidden part of code: some helpfull functions

    private static void createAndShowGui() {
        ...
        SimpleGrapher2 mainPanel = new SimpleGrapher2(scores);
        mainPanel.setPreferredSize(new Dimension(800, 600));
        JFrame frame = new JFrame("DrawGraph");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        //frame.setContentPane(new GLG2DCanvas(paintingComponent));
        frame.setContentPane(paintingComponent);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Community
  • 1
  • 1
TheOnly
  • 3
  • 7
  • Post your exception with the entire stack trace. – PM 77-1 Apr 08 '15 at 01:04
  • Is `dataset` a global variable? If it is, then that means `dataset` is not initialized like @reseuman said. – mrQWERTY Apr 08 '15 at 01:06
  • @Renren29 yes, it's global variable and I read files and add points to dataset in the constructor. I editted the post to show it – TheOnly Apr 08 '15 at 01:27
  • @resueman I call `readFromFiles()` in the constructor. I editted post to show it – TheOnly Apr 08 '15 at 01:29
  • It seems that `series` in `readFromFiles()` was just instantiated. But thats it, you didn't do anything with it and just added it to `dataset`. – mrQWERTY Apr 08 '15 at 01:39
  • @Renren29 I didn't want to post all code here so I hided some parts of code, for example, reading from files in `series`. This code works. There only 1 problem - reading from files in dataset takes too much time and when `drawSplines()` called, `dataset == null` yet. I need to set some timer to give time for adding points in dataset but I don't know how to make it. I guess that I need to put it before loop in `drawSplines()` but I don't know what I need to add. – TheOnly Apr 08 '15 at 02:05
  • Without further evidence, `dataset` is `null`, why, is impossible to say – MadProgrammer Apr 08 '15 at 02:11
  • @MadProgrammer `dataset` is null because `readFromFiles()` takes too much time to get points from files in dataset (`Scanner` works slowly). When programm calls `drawSplines()`, `readFromFiles()` is still working and `dataset` is null. I need to add some timer in `drawSpline()` to get time for `readFromFiles()` to complete reading but I don't know how to make it – TheOnly Apr 08 '15 at 02:17
  • Well, now you have a thread race condition. Maybe, you should put a check in your `paintComponent` to check for a `null` `dataset`... – MadProgrammer Apr 08 '15 at 02:19
  • @MadProgrammer I editted my post again and make my code more usefull for your. How to make this check? And I need to add timer or not? – TheOnly Apr 08 '15 at 02:24
  • 1- `static` is not your friend and will cause you no end of grieve; 2- In you `drawSplines` method, you should put an `if` statement in which checks to see if `dataset` is `==` `null`; 3- You should not fill `dataset` directly within you `readFromFiles` method, instead, use a temporary `List` while reading the content and set it to `dataset` once the processing is complete, this will reduce the risk of dirty paints – MadProgrammer Apr 08 '15 at 02:27
  • @MadProgrammer thanks, I understand you. But what I need to write in `if (dataset == null)` statement in `drawSplines()`? – TheOnly Apr 08 '15 at 02:30
  • Since the error occurs there, that would be a good starting point – MadProgrammer Apr 08 '15 at 02:33

0 Answers0