2

我知道标题听起来令人困惑,那是因为它是。它有点长,所以也试着和我在一起。

这是我设计的代码的布局

变量构造方法。

我也在尝试填写完整的 Jlist 名称。我也想使用一种方法来获取这些名称。就这样吧。

在我的变量中,我有我的 JList。它称为contactNames;我还有一个存储 5 个字符串的数组,这些字符串是联系人姓名;无论如何,继承人的代码

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

String[] contactListNames;

JList contactList;

很简单。然后在我的构造函数中,我定义了 Jlist 以用数组的内容填充自己

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

该方法 fillContactList() 即将推出。

现在这里的东西得到了球。我创建了三种不同的方法,但都没有奏效。基本上我试图用所有这些填充数组。这是最简单的。它没有设置 Jlist,它没有做任何复杂的事情。它也尝试做的就是一次填充一个数组

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();   
               }
           }
       }

不幸的是,这不起作用。我得到打印出来以在索引 0 处填充它;我输入了一些东西,我得到了一个很好的大堆栈跟踪,从

    contactListNames[i] = contact;

所以简而言之,我的问题是

为什么我不能用那个方法填充数组。

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

按请求进行堆栈跟踪,请填写索引 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)
4

3 回答 3

2

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.

于 2010-04-23T13:09:32.350 回答
1

您在构造函数中定义一个数组,就像您定义任何其他变量一样。所以,它看起来像:

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

The reason you are getting exceptions is because you don't actually initialize the array. You declare it but you never set it to a value (or give it a size). You should post the stack trace of the error but I suspect it's a NullPointerException.

于 2010-04-23T13:08:17.047 回答
0

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();

What you're doing here is creating new local variables that are shadowing the ones defined in your class. Change it to:

 contactListNames = new String[5];
 contactList = new JList(contactListNames);
 fillContactList();
于 2010-04-23T13:36:02.187 回答