Passing array to constructor for use in JList

I know the name sounds confusing, and that's because it is. its a little longer so try to stay with me too.

this is the layout where my code is generated

variables constructor Methods.

im trying to fill the Jlist too much by name. I also want to get these names using the method. so here goes.

in my variables i have my JList. his named contact names; I also have an array that stores 5 strings that are the names of the contacts; heres the code for this anyway

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

String[] contactListNames;

JList contactList;

      

simple enough. then in my constructor I have defined a Jlist to fill it with the contents of the array

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

      

this fillContactList () method is coming soon.

now here's where things get balls. ive created three different methods from which hausent worked. basically i'm trying to populate an array with all of them. this is the easiest one. it doesn't install the Jlist, it doesn't compile anything. all it tries to 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 doesn't work. I get a printout to fill with index 0; i inject something and i get a nice big stack trace starting at

    contactListNames[i] = contact;

      

so my question is shorter

why can't I fill the array from this method.

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

stack trace as requested please fill in 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)

      

+2


a source to share


3 answers


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 the JList from Java Doc you can certainly pass the array toJList



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

      

You get NullPointerException

because the array is contactListNames

not initialized, you need to initialize it.

+2


a source


You define an array in the constructor just like any other variable. So it will look something like this:

// define an array of size 3
String[] contactListNames = new String[3]; 

      



The reason you are getting exceptions is because you are not actually initializing the array. You declare it, but you never set it to a value (or size it). You should post the error stack trace, but I suspect it is a NullPointerException.

+1


a source


Then in my constructor I have a Jlist defined to fill the contents of the array

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

      

What you are doing here is creating new local variables that will shadow those defined in your class. Change it to:

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

      

0


a source







All Articles