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 8275533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:06:18+00:00 2026-06-08T08:06:18+00:00

I have got this exercise working perfectly: import acm.program.*; import acm.graphics.*; import java.awt.event.*; public

  • 0

I have got this exercise working perfectly:

import acm.program.*;
import acm.graphics.*;
import java.awt.event.*;
public class ProgramHierarchy extends GraphicsProgram {
public void run() {
    Paddle = new GRect(0,getHeight() - 30,100,20);
    add(Paddle);
    addMouseListeners();
    }

public void mouseMoved(MouseEvent e) {
    if(e.getX() - Paddle.getWidth()>0) {
    Paddle.move(e.getX()-Paddle.getX()-Paddle.getWidth(), 0);
    }
}

private GRect Paddle;
}

But when I use the exact same code in a larger project, my eclipse throws null pointer exceptions and even hard-crashes eclipse altogether and I hit my desk and say bad things I would not like my family to hear. Is my copy of eclipse corrupt? What could be going on?

Here’s my larger hard-crashing project.

import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Breakout extends GraphicsProgram {

public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
private static final int PADDLE_Y_OFFSET = 30;
private static final int NBRICKS_PER_ROW = 10;
private static final int NBRICK_ROWS = 10;
private static final int BRICK_SEP = 3;
private static final int BRICK_WIDTH =
  (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
private static final int BRICK_HEIGHT = 8;
private static final int BALL_RADIUS = 10;
private static final int BRICK_Y_OFFSET = 70;
private static final int NTURNS = 3;
private static final int DELAY = 30;



public void run() {
    setup();
    GRect Paddle = new GRect(0,HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
    add(Paddle);
    addMouseListeners();
    }







private void setup() {

    int xStart = (WIDTH - ((NBRICKS_PER_ROW * BRICK_WIDTH) + ((NBRICKS_PER_ROW -1) * BRICK_SEP)))/2;

    int y = BRICK_Y_OFFSET;

    for (int i = 0; i< NBRICK_ROWS; i++) {
        int x = xStart;

        for (int j = 0; j < NBRICKS_PER_ROW; j++) {
            GRect block = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
            if (i<2) {
                block.setColor(Color.RED);
                block.setFilled(true);
                block.setFillColor(Color.red);
            }
            if (i==2||i==3) {
                block.setColor(Color.orange);
                block.setFilled(true);
                block.setFillColor(Color.orange);
            }
            if (i==4||i==5) {
                block.setColor(Color.yellow);
                block.setFilled(true);
                block.setFillColor(Color.yellow);
            }
            if (i==6||i==7) {
                block.setColor(Color.green);
                block.setFilled(true);
                block.setFillColor(Color.green);
            }
            if (i==8||i==9) {
                block.setColor(Color.cyan);
                block.setFilled(true);
                block.setFillColor(Color.cyan);
            }
            add (block);
            x=x+BRICK_WIDTH + BRICK_SEP;
        }
    y=y+BRICK_HEIGHT+BRICK_SEP;
    }
}


public void mouseMoved(MouseEvent PadX) {
    if(PadX.getX() - Paddle.getWidth()>0) {
    Paddle.move(PadX.getX()-Paddle.getX()-Paddle.getWidth(), 0);
    }
}


private GRect Paddle;
}
  • 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-08T08:06:20+00:00Added an answer on June 8, 2026 at 8:06 am

    You’re shadowing the Paddle variable: declaring it in the class, but re-declaring it in the run method. The Paddle variable held in the class is never initialized and so it remains null. The solution is to not re-declare this variable in the run method.

    For example, in the code of yours that works, you have:

    public void run() {
      Paddle = new GRect(0,getHeight() - 30,100,20); // **** notice the difference?
      add(Paddle);
      addMouseListeners();
    }
    

    and in the code that doesn’t work:

    public void run() {
      setup();
    
      // notice that you re-declare Paddle here!
      GRect Paddle = new GRect(0,HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
      add(Paddle);
      addMouseListeners();
    }
    

    The Paddle variable that is initialized above is only visible within the run method and no-where else in your class. The class variable with the same name remains null.

    Solution: don’t re-declare Paddle:

    public void run() {
      setup();
    
      // Now we don't re-declare Paddle 
      Paddle = new GRect(0,HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
      add(Paddle);
      addMouseListeners();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a learning exercise in expression trees. I have this working code: class
I have got this html: <div id=myDiv> <p class=readMore></p> <p class=readMore></p> <p class=readMore></p> </div>
I got this as home exercise but I have no clue how to solve
I have got this html: <a href=http://www.google.com data-ref=su1c2cess class=Wifewriting id=target_site_to_visit> <span data-app-id=63 class=btn id=visit_site
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
I have got this text file with latitude and longitude values of different points
I have got this code: $subject=<<<EOD <object height=400 width=500><param name=allowfullscreen value=false> <param name=AllowScriptAccess value=always>
I have got this regex pattern that works with my current apache log format:
I have got this result with the answer given by bluefeet Equipt BSL AQ
I have got this code for detection of mobile device. <?xml version=1.0 encoding=UTF-8?> <configuration>

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.