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

  • Home
  • SEARCH
  • 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 8395837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:23:56+00:00 2026-06-09T20:23:56+00:00

Here I have a simple class that extends a class in the ACM graphics

  • 0

Here I have a simple class that extends a class in the ACM graphics library called GRect.
Grect is a basic rectangle that can be drawn to a GCanvas (Also part of ACM).
What I want to achieve is a new object that is a rectangle, but has an attached label that moves with the rectangle.

In my code, I have created a class named labeledRect that extends GRect, and contains an instance variable “rectLabel” that is of type GLabel, that is initialized in labeledRects constructor. I want to override some of GRect’s methods so that when labledRect is moved, rectLabel moves with it.

My issue is that despite “rectLabel” being declared as an instance variable, and initialized in the constructor, it becomes NULL in the overridden method “setLocation”.
I have also attempted to initialize “rectLabel” at declaration, but the same problem occurs.

import acm.graphics.*;

public class labeledRect extends GRect {

    //GLabel declared as an instance variable
    public GLabel rectLabel;

    public labeledRect(double x, double y, double width, double height, String theLabel) {
        //Call GRect constructor
        super(x, y, width, height);

        //Label initialized. 
        //Location setting and adding to the canvas works fine here. 
        rectLabel = new GLabel(theLabel);
        rectLabel.setLocation(
                    x + (width / 2) - (rectLabel.getWidth() / 2),
                    y + (height / 2) + (rectLabel.getAscent() / 2.2));
    }

    public void setLocation(double x, double y)
    {
        //Setting GRect's location. Works correctly./
        super.setLocation(x, y);

        //Attempt to set the label's location 
        //and get a NullPointer exception for rectLabel
        rectLabel.setLocation(
                super.getX() - (rectLabel.getWidth() / 2),
                super.getY() - (rectLabel.getHeight() / 2));
    }
}
  • 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-09T20:23:57+00:00Added an answer on June 9, 2026 at 8:23 pm

    It’s hard to reason about exactly what might be setting the variable to null given that it’s a public field. That’s the first thing you should change. Fields should almost always be private.

    Next, you say setLocation is an overridden method – is it called in the GRect constructor by any chance? If so, that will be called before the value is set within the labeledRect constructor, which could well be the cause of your problem.

    In Java, the superclass constructor is executed before any of the code in the subclass – both before instance variable initializers and the constructor body. All variables will have their default values. This is why it’s a bad idea to call virtual methods from constructors.

    (As an aside, the name labeledRect doesn’t follow Java naming conventions.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm missing something fundamental here. i have a very simple custom class that draws
Here's something weird that I can't figure out. I have a Moose class that
I have a simple class, BoundedView that extends View . I'm doing this primarily
So i have a class name repository which is just a simple array.Here is
I have simple algorithm that clean the whitespace from half string until end. Here
I have a simple page here to demonstrate the issue that I am seeing.
What i have here is a simple webview activity that when loaded it auto
So, say I have a simple enum and a class that uses it: enum
Here's a simple example: class World<S extends Sprites, B extends Bodies> { // Both
Here is my model_users class (the model it extends from doesn't have anything yet

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.