I'm making a program to draw graphs. I have an object 'Line' who containt an array with numbers where a real line must be drawn of( bad english ) and a color. These lines are all in one arraylist. There is one other object who considers these lines. Now, I've made a canvas where the graph must be drawn on. There is another method 'drawgraph' who has the arraylist of lines as parameter. I've also a data who represents the arraylist of lines. the drawgraph methode sets it's data member equals to the parameter. Then it calls the repaint( ). When the paint method is invoked, the graph must be drawn. The lines to be drawn are in the arrayList. This works, but now: if i initialize the canvas the repaint method is invoked and the paint also, thus the graph is drawn, but there are no lines te draw. The arraylist with the lines in it is not initilized either so a nullpointer exception occurs. Can anyone help me with that?
NullPointerExceptions are usually pretty straightforward to resolve. See which object is null and then walk back in your code and see why it wasn't properly initialized. Otherwise I don't see how we'll be able to guess what is not working based on just descriptions and not code.
Well, it is a lot of code, over 1000 lines. The nullpointerexception occurs because my arraylist in the canvas isn't initialized. But if i do, I've an arraylist in my canvas and one in my lineholder. These two become thesame only if the user wants to draw. Thus in the beginnin i have two arraylists and later they become 1. But i'm looking for a better solution that you don't need 2 in the beginning. If anyone can help me with that
You either have lines to draw or you haven't; if you can decide that you haven't if an arraylist happpens to be null, fine, don't draw anything then in your paintComponent(Graphics g) method.
Originally Posted by hannes