0

I'm working on a piece of code which randomly puts circles across the screens for my game(simply put). I have no error in the code but I've got into a problem where it wants to write more paint code but I want to type more, here is the code. I'm Using JFrame for this

    public class Game extends JPanel{
        public static final long serialVersionUID = 2L; 
        public static Random random;
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            this.setBackground(Color.BLACK);
            Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int width = (int) ScreenSize.getWidth();
            int height = (int) ScreenSize.getHeight();
            for(int i=1; i<12; i++){
            int rw = random.nextInt(width);
            int rh = random.nextInt(height);
            int radius = random.nextInt(50);
            g.setColor(Color.gray);
            g.fillOval(rw, rh, radius+50, radius+50);
       }

` Error Code comes out like this

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Game.paintComponent(Game.java:18)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)

Simply put, what have I done wrong and How may i fix it?

MrFAYAZ666
  • 13
  • 4
  • 1
    You will likely need to post more code for us to understand your problem, preferably a [mcve], but one thing I suggest is not to create the random values within the painting method, but rather within your class, and then use them within your painting method. – Hovercraft Full Of Eels Nov 22 '15 at 14:28
  • What you want is not so clear. What do you mean by "How can i get it to cancel "painting" so I can draw out the rest of the circles?" or "I've got into a problem where it wants to write more paint code but I want to type more" – Jean-Baptiste Yunès Nov 22 '15 at 14:31
  • @HovercraftFullOfEels The error seems to be because of the g.setColor() and then me trying to do something which is not painting. But if i was to take your suggestion, then how would i create a new random number with each itteration and not run into the same bug? – MrFAYAZ666 Nov 22 '15 at 14:34
  • Mr. Fayaz, without more pertinent code, again preferably your [mcve] (please read the link), I have no idea. I just know that as a general rule, you don't want such logic within a painting method. No, this will not solve your main problem, but we're waiting for a better question to allow us to help you with this. – Hovercraft Full Of Eels Nov 22 '15 at 14:35
  • @Jean-BaptisteYunès I apologise, i'm not very good at explaining this bug but ill attempt again, with the g.setColor() it will always come out with errors if i attempt to do anything other than draw a new shape or something along those lines. How am i able to cancel that action so i can generate new random number and paint a new circle – MrFAYAZ666 Nov 22 '15 at 14:36
  • Again: what you want is not clear. Also follow the advice from @Hovercraft, post some testable code not just some lines out of context (we are not magicians). – Jean-Baptiste Yunès Nov 22 '15 at 14:39
  • @HovercraftFullOfEels Here is the all the code i believe is relevant. With the error code incase that clarifies my problem – MrFAYAZ666 Nov 22 '15 at 14:41
  • @Jean-BaptisteYunès I applogise for not making it clear enough, i put the error code and basically everything significant excluding the import – MrFAYAZ666 Nov 22 '15 at 14:44
  • Question answered as a community wiki, but it should be closed as a duplicate of this: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). – Hovercraft Full Of Eels Nov 22 '15 at 15:00

1 Answers1

1

You're using the random variable without initializing it:

Random random;

should be

Random random = new Random();

More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.

Also in the future, please strive to ask a more complete question, including any and all error messages, and including an indication of which line throws the exception. This will reduce the frustration level enormously.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373