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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:49:08+00:00 2026-05-14T14:49:08+00:00

I know the title sound confusing and thats because it is. its a bit

  • 0

I know the title sound confusing and thats because it is. its a bit long so try too stay with me.

this is the layout i have my code designed

variables
constructor
methods.

im trying too fill a Jlist full on names. i want too get those names using a method. so here goes.

in my variables i have my JList. its called contactNames;
i also have an array which stores 5 strings which are the contacts names;
heres the code for that anyway

String contact1;
String contact2;
String contact3;
String contact4;
String contact5;

String[] contactListNames;

JList contactList;

simple enough. then in my constructor i have the Jlist defined to fill itself with the contents of the array

    String[] contactListNames = new String[5];
    JList contactList = new JList(contactListNames);
    fillContactList();

that method fillContactList() is coming up shortly.

now heres where stuff gets balls up.
ive created three different methods all of which havent worked. basically im trying to fill the array with all of them.
this is the simplest one. it doesnt set the Jlist, it doesnt do anything compilicated. all it trys too do is fill the array one bit at a time

public void fillContactList()
       {
           for(int i = 0;i<3;i++)
           {
               try
               {
                   String contact;
                   System.out.println(" please fill the list at index "+ i);
                   Scanner in = new Scanner(System.in);
                   contact = in.next();
                   contactListNames[i] = contact;
                   in.nextLine();
               }
               catch(Exception e)
               {
                e.printStackTrace();   
               }
           }
       }

unfortunately this doesnt qwork. i get the print out to fill it at index 0; i input something and i get a nice big stack trace starting at

    contactListNames[i] = contact;

so my question in short is

why cant i fill the array from that method.

***********************************************888 ***********************************************888

stack trace by request
please fill the list at index 0

overtone
please fill the list at index 1
 java.lang.NullPointerException
at project.AdminMessages.fillContactList(AdminMessages.java:410)
at project.AdminMessages.<init>(AdminMessages.java:91)
at project.AdminUser.createAdminMessages(AdminUser.java:32)
at project.AdminUser.<init>(AdminUser.java:18)
at project.AdminUser.main(AdminUser.java:47)
  • 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-14T14:49:09+00:00Added an answer on May 14, 2026 at 2:49 pm

    To define an array in a constructor you can do something along these lines,

    // if values are predefined, you can explicitly fill the array
    String[] contacts = {"Bill Gates", "Steve Jobs", "Jon Skeet"};
    
    // or this way, both will work.
    String[] contacts = new String[2];
    

    Looking at JList from the Java Doc’s you can most certainly pass in an array to JList

     String[] data = {"one", "two", "three", "four"};
     JList dataList = new JList(data);
    

    You are getting NullPointerException because the array, contactListNames is not initialized, you would need to initialize it.

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

Sidebar

Related Questions

I know at first glance (due to the title) this looks like one of
The title is kind of obscure. What I want to know is if this
Excuse my title if it's confusing, but I don't know how else to describe
Update: this question, including the title, was rephrased, see history for details I know
I know that the title might sound a little contradictory, but what I'm asking
I know the title does not sound very descriptive, but it is the best
I know the title sounds funny, but I found this snippet somewhere: my MyPackage
This sounds like a weird title and probably not stated too well. But here's
I know the title of the question is a bit odd. But I don't
As the title of this question tells I want to know the best way

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.