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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:49:41+00:00 2026-06-14T09:49:41+00:00

I am trying to assign text to buttons inside a loop but I am

  • 0

I am trying to assign text to buttons inside a loop but I am getting an NullPointerException.
I have initialized the button array inside constructor of the class and after initilizatoin, I call the following method. Here is the code where I am getting error.

public class Alfred
{
   private String names[]={"nfs","gta","maxpayne","hitman"};
   private JButton but[];

   public Alfred()
   {
    ...
     but= new JButton[4];
    AssignLettersToButtons();
   }
    private void AssignLettersToButtons()
    {       
        for(int i=0;i<names.length;i++)
        {
            but[i].setText(names[i]); // error
            pane1.add(but[i]);
        }
    }
}
}

The length of button array and names array is same. What is the reason for this exception and how can I solve it?

Regards

  • 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-14T09:49:42+00:00Added an answer on June 14, 2026 at 9:49 am
     but= new JButton[4]; 
    

    This only allocates space for four array elements; each element is initialized to null, the default value for a reference type like your JButton objects.

    but[0], but[1], but[2], but[3] are all null.

    You should initialize them like this:

    but[0] = new JButton(); //or whatever.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to assign button controls text dynamically by using a loop like
I am trying to assign a regular expression result to an array inside of
I am trying to add a button inside a jqGrid column and to assign
Trying to loop though my XML data and assign to text fields. Not get
I'm trying to assign a variable from a html text input value and then
Possible Duplicate: Adding css class through aspx code behind Im trying to assign a
I have variable of a type DateTime objApplicationSummaryInfo.AdmissionDate . I am trying to assign
i am trying to get id names from a array and trying to assign
I'm trying to have different html buttons that send user's to different sections of
I'm simply trying to assign a UITextField.text to an NSString in my object. 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.