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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:44:40+00:00 2026-06-14T00:44:40+00:00

I have been making a Tetris-like game in Java recently, I have a interface

  • 0

I have been making a Tetris-like game in Java recently, I have a interface IBlocks which goes over ConsBlock and EmptyBlock (ConsBlock is a list with a block at the beginnning and a IBlock list at the end, for some reason that’s the way my teacher wanted it).

I have the getfirst() function in the interface:

public interface IBlocks{
//returns the first block in a list of blocks
public Resting getfirst();

(A Resting is one block in the Game)

Then, in the ConsBlock class, I have:

public class ConsBlock implements IBlocks{
//returns the first block in a list of blocks
public Resting getfirst(){
    return this.first;

But in the EmptyBlock class, I want it to return something that would be similar to saying there isn’t one. I tried returning null, but that gave me a null pointer exception due to that fact that the function in the interface tells it to return a Resting. What would be the best way to represent an empty one without returning a Resting with random numbers?

  • 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-14T00:44:41+00:00Added an answer on June 14, 2026 at 12:44 am
    public interface IResting;
    public class Resting implements IResting;
    

    Then

    public class EmptyResting implements IResting {
        @Override
        public Object sampleMethod() {
            // return what an empty block ought to return
        }
    }
    

    Further reading: http://en.wikipedia.org/wiki/Null_Object_pattern

    Response to your comment: you still need to do:

    public class EmptyBlock implements IBlocks {
        @Override
        public IResting getFirst() {
            return new EmptyResting();
        }
    }
    

    Note that your IBlocks and ConsBlock classes need to return an IResting not a Resting

    Make sure to read that wiki link!

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

Sidebar

Related Questions

I'm relatively new to java (specifically swing) and have recently been making some fairly
I am making a game that needs a crosshair. I have been playing with
I have been making a small game for fun. In the game you are
In last few months I have been making a transition from Java to Groovy
Possible Duplicate: Java, GUI builder or hand coding? I have been making GUI hand-coded
I was thinking of making a Tetris game for Java and could not decide
I have been making use of Java's synchronized blocks to make parts of my
I have been making a Phonegap / Cordova 2.0 app with backbone.js which has
I have been making a button which (hopefully) looks fairly realistic. One part of
I have been making application in which i click on image and new window

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.