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

The Archive Base Latest Questions

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

I have another beginner’s question that hopefully someone can help with. I’m trying to

  • 0

I have another beginner’s question that hopefully someone can help with.

I’m trying to pass an array from one class to another in order to use it to produce a bar graph. I know I can pass an array as a parameter but I’m getting some errors and am basically a bit lost.
The basic code for the classes for the chart are below. Supposing I want to pass “anArray” from “AnotherClass”, can anyone tell me exactly how I should pass it?

I’ve tried passing it as a parameter of JBChart & chartComponent but I think I need it to be in paintComponent? As it already has “Graphics g” as parameter, I am rather confused. Anyway, either of those two and I get nullPointer errors (although I know I could be doing something else wrong too).

Here’s the basic code for the chart classes. Any ideas very much appreciated:

public class JBChart extends JFrame {

    public JBChart()    {}

    public void buildChart()
    {   
        ChartComponent component = new ChartComponent();
        chartFrame.add(component);                           
        chartFrame.setVisible(true);
    }
}
public class ChartComponent extends JComponent {
    public ChartComponent() {}

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
    }

—

-EDIT-

This is the first few lines of one of the stack traces (I hope that’s enough?): —

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at ChartComponent.<init>(ChartComponent.java:43) at JBChart.<init>(JBChart.java:32) 
at JavaBallGUI.displayBarChart(JavaBallGUI.java:273) 
at JavaBallGUI.actionPerformed(JavaBallGUI.java:310) 

—
The line it points to is:

for (int i = 0; i < teamObjects.length; i++) 
{ 
    if (teamObjects[i] != null) 
    { 
    teamName = teamObjects[i].getTeamName(); 
    System.out.println(teamName); 
    } 
} 
  • 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:03:51+00:00Added an answer on May 28, 2026 at 2:03 am

    You should probably pass it to the constructor:

    public class ChartComponent extends JComponent {
        private final int[] values; // For example
    
        public ChartComponent(int[] values) {
            this.values = values;
        }
    
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
        }
    }
    

    Note that this will still allow either class to change the values within the array afterwards, as they’ve both got a reference to the same mutable object. There are various ways round this, depending on exactly what you’re trying to do. You’d then use:

    ChartComponent component = new ChartComponent(array);
    

    Alternatively, you could always create setter methods on the ChartComponent and call those at the appropriate time. You won’t be able to change the signature of paintComponent and still get the behaviour you want, so you’ll need to provide the data before the method is called.

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

Sidebar

Related Questions

I have came from another question to this one: How can one start with
I have another programmer who I'm trying to explain why it is that a
Ok I have another question and I'm a beginner at this. I have this
I apologize for yet, another beginner question. I have a .Net background and this
I have another csv file where I am trying to do a simple word
i have another (probably unanswered) question about map views. I have a map view
I have a DataTable populated with values from a database. I have another datatable
I have a form (Form1) that has a NotifyIcon on it. I have another
I have a table (user) that contains user information. I have another table (userview)
hey guys, beginner here. I have written a program that outputs files to .txt's

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.