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();
}
});
}
}