Suppose below is my code :

  public void paintComponent( Graphics g )
  {
      graphics2D = (Graphics2D)g;
      graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      graphics2D.setPaint(Color.white);
      graphics2D.fillRect(0, 0, getSize().width, getSize().height);
      graphics2D.setColor(Color.black);
  }

and graphics2D is global variable declared like :

private Graphics2D graphics2D;

Now, how can I access g to draw lines from other methods?
When I'm accessing graphics2D, it's showing that NullPointerException.
and please also brief me about the Graphics g thing.

access g to draw lines from other methods

Call the other methods from the paint method and pass them the Graphics object: g that was passed to paint.
otherMethod(g);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.