0

I want to try to put a rectangle on my screen by using JPanel and JFrame. But I get the java.lang.NullPointerException and I don't know why. My code:

Class to create a rectangle:

package com.game.main;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Rect extends JPanel {

Graphics a;

public Rect(){      
    a.drawRect(100, 100, 200, 250);
    a.setColor(Color.RED);
    a.fillRect(100, 100, 200, 250);     
    }
}

Class to create a JPanel:

package com.game.main;

import javax.swing.JPanel;

public class Inside extends JPanel {

    public Inside(){
        JPanel content = new JPanel();
        Rect rect = new Rect();
        content.add(rect);
    }
}

Class to create a JFrame:

package com.game.main;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window extends Main{

    public static final int Width = 1020, Height = 860;

    public Window(JPanel content){
        JFrame frame = new JFrame("The Game");
        frame.setContentPane(content);
        frame.setSize(Width, Height);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
    }

}

Class to put everything together:

package com.game.main;

public class Main {

    public static void main(String[] args) {
        Inside inside = new Inside();
        Window window = new Window(inside);
    }

}

I am also not very familiar with the Graphics class, so a little bit of explanation would be wonderful.

Thank you

  • the exact exeption message from the console: Exception in thread "main" java.lang.NullPointerException at com.game.main.Rect.(Rect.java:13) at com.game.main.Inside.(Inside.java:9) at com.game.main.Main.main(Main.java:6) – Jeffrey Saydam Dec 14 '16 at 13:33
  • `public class Rect extends JPanel { Graphics a; }` Where is the `a = new ...` ? – Zorglube Dec 14 '16 at 13:34
  • 1
    You have to provide an instance of Graphics to class Rect (in the constructor), so field "a" is Not Null. – pringi Dec 14 '16 at 13:35
  • Also have a look at how to draw in a `JPanel` : http://stackoverflow.com/questions/6118737/how-to-draw-in-jpanel-swing-graphics-java – Arnaud Dec 14 '16 at 13:36
  • Override `paintComponent` in your 1st panel class – Reimeus Dec 14 '16 at 13:36
  • @Zorglube : I get an error when I try Graphics a = new Graphics(); – Jeffrey Saydam Dec 14 '16 at 13:39
  • @Jeffrey, maybe you might create your Graphics instance in the Rect method. – Zorglube Dec 14 '16 at 13:41
  • @Zorglube : When I do that I still get the same error (Cannot instantiate the type Graphics) – Jeffrey Saydam Dec 14 '16 at 13:46
  • For what reason can't you instantiate Graphics ? – Zorglube Dec 14 '16 at 14:43
  • @Zorglube : it's an abstract class. – Jeffrey Saydam Dec 14 '16 at 15:15
  • @Reimeus : I've overridden the paint method, but it still doesn't work. I've tried to override paint method in rect only, and only in Inside and I tried to do in both, but I've got no results. The exception is no more, because I've deleted 'Graphics a" but rect still won't appear in the screen. What are the differences with paint and paintcomponent? thanks – Jeffrey Saydam Dec 14 '16 at 15:18
  • @JeffreySaydam hint: `paintComponent` has a Graphics argument that you can use... – Reimeus Dec 14 '16 at 15:20

0 Answers0