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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:43:02+00:00 2026-05-28T02:43:02+00:00

I am a java beginner and for my first project I started building a

  • 0

I am a java beginner and for my first project I started building a Monopoly game.

I’m building the GUI in SWING using the Graphics method.

Two problems appeared which I can’t find an answer to.

The first one is that it seems that I can’t set the Background color to my JPanel which I had previously done the same way in another JPanel in the same project.

The second one is that I get a NullPointerException while trying to add an image.I managed to correct this error with a try/catch but it seems that the Graphics won’t paint.Again I’ve used the same method to load and add images in a previous JPanel and it worked.

I should mention that my JFrame at the moment contains 3 elements everyone in there separate classes and are added via BorderLayout().

This is the code for the class that is creating problems:

    public class MonopolyBoard extends JPanel{


    Image atlantic;
    MonopolyBoard() {
        this.setBorder(new EtchedBorder());

        this.setBackground(new Color( (80), (180), (210) )); //this code dosent work

        //this throws exception without try catch
        try{
        ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/Atlantic Ave.jpg"));
         atlantic = a.getImage();
       }
       catch(NullPointerException e){}
       }

    public void paint(Graphics g){

          }
          Graphics2D g2 = (Graphics2D) g; 
         //this code should draw the image but it dosent
          g2.drawImage(atlantic, 100, 100, null);
          g.drawImage(atlantic, 100, 100, this);

    };
}
  • 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-05-28T02:43:02+00:00Added an answer on May 28, 2026 at 2:43 am

    You won’t know unless you print the stacktrace inside the catch block. If the constructor, new ImageIcon(), isn’t throwing the exception and instead is returning a null object, the next line, a.getImage(), will definitely cause a NPE because you can’t call a method on a null object.

    Instead of this

     //this throws exception without try catch         
     try
     {           
         ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
         atlantic = a.getImage();        
     }        
     catch(NullPointerException e){}   
    

    Try this

    // the next line may be wrapped incorrectly due to MarkDown
    ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
    if (a == null)
    {
        System.out.println("Can not find AtlanticAve.jpg");
        return;
    }
         atlantic = a.getImage();        
    

    The line

     // the next line may be wrapped incorrectly due to MarkDown
     ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
    

    Basically, you need to start by seeing what might cause the constructor if ImageIcon to return a null object. That will get you on the right track. It might be something due to a failed getResource() call. A simple way of finding out would be to separate the above line into it’s parts and give them their own result variables. It’s messy and ineficient but that’s how troubleshooting goes sometimes.

    // using _var_ because I'm too lazy to look up the return types of the methods
    var x1 = this.getClass().getResource("../Card/AtlanticAve.jpg");
    if (x1 == null)
    {
       System.out.println("Can't find my resource");
    }
    

    You get the picture

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

Sidebar

Related Questions

I'm a Java beginner and this is my first post. I couldn't find anything
I am a beginner in Java . The first thing I learned was the
First of all, Beginner here. I'm using this code. class MDArrays { public static
Possible Duplicate: Drag and Drop Java GUI Hi about 3 months ago I started
First things first, I am a Python beginner, with a typical C++/Java background for
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying
I'll be cranking out my first serious GUI in Java here over the next
I'm a complete beginner trying to learn Java as my first language. When I'm
I'm a Java beginner. I already created a simple GUI application that display will
I'm a java beginner, & I'm creating a program that draws lines using the

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.