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
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:
- Why is onActivityResult() Intent data equal null?Best solution by Stack Overflow
- What is John Lewis' policy when returning an LCD with a dead pixel?Best solution by Yahoo! Answers
- Which laser pointer is better and worth it?Best solution by Yahoo! Answers
- Why does 'null' appear in Yahoo Contacts?Best solution by answers.yahoo.com
- Why does my mouse pointer disappear?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.