title: Graphics NullPointerException when overriding Canvas paint method inline
body: <p>So I'm attempting to create a Canvas, and override the paint method inline, however I'm getting a NullPointerException on the Canvas' Graphics.</p>
  
  <pre><code>Canvas c = new Canvas(){
      @Override
      public void paint(Graphics g){
          g.setColor(Color.BLACK);
          g.drawString("Canvassed Window", 100, 100);
      }
  };
  </code></pre>
  
  <p>NullPointerException comes for </p>
  
  <pre><code>g.setColor(Color.BLACK);
  </code></pre>
  
  <p>I'm just going to create a new class that inherits from Canvas, but I was wondering if anyone knew the reason that it was doing this. I have a suspicion that it's because the Canvas hasn't been initialised yet.</p>
  
  <p>I also wondered if anyone knew a workaround for it, I tried to write something that looked like</p>
  
  <pre><code>private boolean isInit = false;
  init(){
      isInit = true;
  }
  </code></pre>
  
  <p>and then in the paint method put a line in there at the top that says</p>
  
  <pre><code>if(!isInit) return;
  </code></pre>
  
  <p>but when I tried to call init later on it didn't want to know, it just errored and said that init wasn't in the Canvas class, I'm guessing that I can only override methods in this manner.</p>
  
  <p>EDIT:</p>
  
  <p>I have a Window class</p>
  
  <pre><code>import java.awt.Canvas;
  import java.awt.Frame;
  
  @SuppressWarnings("serial")
  public class Window extends Frame{
  
      public Window(int width, int height, int locationX, int locationY) {
          setSize(width, height);
          setLocation(locationX, locationY);
          setResizable(false);
          setTitle("Window");
          setVisible(true);
      }
      public Window(int width, int height, int locationX, int locationY, String title){
          Window w = new Window(width, height, locationX, locationY);
          w.setTitle(title);
      }
      public Window(int width, int height, int locationX, int locationY, String title, Canvas c){
          Window w = new Window(width, height, locationX, locationY, title);
          w.add(c);
      }
  
  } 
  </code></pre>
  
  <p>And here's my main method, where I create a new Window instance:</p>
  
  <pre><code>public static void main(String[] args){
      Canvas c = new Canvas(){
          @Override
          public void paint(Graphics g){
              g.setColor(Color.BLACK);
              g.drawString("Canvassed Window", 100, 100);
          }
      };
      new Window(800, 600, 200, 100, "CanvassedWindow", c);
  }
  </code></pre>
  
  <p>I'm getting a Window or "Frame" come up, right size, dimensions and title, however it does not seem to be drawing the String, and there's an NPE. I'm not doing anything else, all I have is a wrapper class for my main method.</p>