Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:42:56+00:00 2026-06-07T06:42:56+00:00

I’m doing some exercise to understand Java and Swing API. Why do I have

  • 0

I’m doing some exercise to understand Java and Swing API. Why do I have a nullPointerException in the Disegno constructor? I want to print the coordinates of the two rectangles, but they seem not to be initialitied.

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Disegno extends JFrame{

    Disegno(){
        this.setSize(500, 500);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        MyPanel aba = new MyPanel();
        this.setContentPane(aba);
        this.setVisible(true);

        System.out.println(aba.rect.blue.x + "-" + aba.rect.blue.y);
        System.out.println(aba.rect.yellow.x + "-" + aba.rect.yellow.y);
    }

    public static void main(String[] args){
        new Disegno();
    }
}

class MyPanel extends JPanel{

    JPanel up, down;
    RectArea rect;

    MyPanel(){
        this.setLayout(new BorderLayout());

        up = new JPanel();
        this.add(up, BorderLayout.NORTH);
        up.setBackground(Color.red);
        up.setVisible(true);

        down = new JPanel();
        down.setBackground(Color.green);
        this.add(down, BorderLayout.SOUTH);
        down.setVisible(true);

        rect = new RectArea();
        this.add(rect, BorderLayout.CENTER);

        this.setVisible(true);
    }
}

class RectArea extends JPanel{

    Rectangle blue, yellow;
    boolean check = false;

    RectArea(){
        super();
        this.setVisible(true);
    }

    public void initRect(){
        blue = new Rectangle(0, 0, 100, 100);
        yellow = new Rectangle(this.getWidth(), this.getHeight(), 100, 100);
        System.out.println("ok");
    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        if(check == false){
            this.initRect();
            check = true;
        }

        System.out.println(this.getWidth() + "-" + this.getHeight());
        g.setColor(Color.blue);
        g.fillRect(blue.x, blue.y, blue.width, blue.height);
        g.setColor(Color.yellow);
        g.fillRect(yellow.x - yellow.width, yellow.y - yellow.height, yellow.width, yellow.height);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:42:57+00:00Added an answer on June 7, 2026 at 6:42 am

    Others have helpfully suggested ways to detect and avoid the NullPointerException. Unfortunately, you can’t rely on when your implementation of paintComponent() will be called. Instead,

    • Determine the required geometry as a function of the current widow’s size; resize the window in the example below to see how yellow seems to stick to the bottom right corner.

    • Because MyPanel contains no components of its own, you should override getPreferredSize(), as @nIcE cOw shows here.

    • Use pack() to size the enclosing Window.

    • Build on the event dispatch thread.

    Addendum: I can’t understand why you override the method getPreferredSize().

    Subclasses of JComponent override getPreferredSize() so that pack() can size the Window “to fit the preferred size and layouts of its subcomponents.” That way you don’t have to worry if the user has a different font, for example. MyPanel just draws geometric shapes, so you’re the boss on preferred size. As discussed here, a demo may use setPreferredSize() for convenience, but you should understand the limitations of doing so.

    enter image description here

    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
    * @see https://stackoverflow.com/q/11376272/230513
    */
    public class Disegno extends JFrame {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Disegno();
                }
            });
        }
    
        Disegno() {
            this.setSize(500, 500);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            MyPanel aba = new MyPanel();
            this.add(aba);
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
    
        class MyPanel extends JPanel {
    
            private JPanel up, down;
            private RectArea rect;
    
            MyPanel() {
                super(new BorderLayout());
    
                up = new JPanel();
                up.setBackground(Color.red);
                this.add(up, BorderLayout.NORTH);
    
                rect = new RectArea();
                this.add(rect, BorderLayout.CENTER);
    
                down = new JPanel();
                down.setBackground(Color.green);
                this.add(down, BorderLayout.SOUTH);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        }
    
        class RectArea extends JPanel {
    
            private Rectangle blue = new Rectangle(0, 0, 100, 100);
            private Rectangle yellow = new Rectangle(0, 0, 100, 100);
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                System.out.println(this.getWidth() + " x " + this.getHeight());
                g.setColor(Color.blue);
                g.fillRect(blue.x, blue.y, blue.width, blue.height);
    
                g.setColor(Color.yellow);
                int dx = getWidth() - yellow.width;
                int dy = getHeight() - yellow.height;
                g.fillRect(dx, dy, yellow.width, yellow.height);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.