Why is my Bitmap returning null?

Why is this returning null pointer exception?

  • I'm just learning java gui and can't figure out for the life of me why this continues to return null pointer exception. I know what the error is but really am lost as to why its popping up right now. Here's the code below. I'm a beginner so its just something to display a few things on a JPanel, to learn how it works. public class dots extends JComponent { private int pwidth = 400; private int pheight = 500; Graphics g; public dots() { draw(); } public void draw() { g.setColor(Color.WHITE); g.fillRect(0,0, pwidth, pheight); g.fillOval(50,50, 25, 25); } } and then I use this to display it: public class dotshower extends JPanel { public static void main (String args[]) { dots dotter = new dots(); JPanel p = new JPanel(); p.add(dotter); } } the exception is first thrown when I call g.setColor(...); and is thrown again when draw() is called and when dotter is initialized. any help is greatly appreciated. Thanks

  • Answer:

    You didn't initialize "g". Your constructor should look something like this public dots() { g = new Graphics(); //or get it from somewhere, I don't know draw(); }

sunexced... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Hi Dear, In fact you did not initialize the object with the graphic class. I hope this is helpful for you. Thanks. Saq

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.