将数组传递到构造函数以在 JList 上使用
我知道这个标题听起来很令人困惑,因为它确实如此。它有点长,所以也尽量留在我身边。
布局
这是我的代码设计变量的 构造函数 方法。
我也想把 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要在构造函数中定义数组,您可以按照以下方式执行操作,
查看 JList from the Java Doc's 你肯定可以将一个数组传递给
JList
你会得到
NullPointerException
因为数组,contactListNames
未初始化,您需要对其进行初始化。To define an array in a constructor you can do something along these lines,
Looking at JList from the Java Doc's you can most certainly pass in an array to
JList
You are getting
NullPointerException
because the array,contactListNames
is not initialized, you would need to initialize it.您可以在构造函数中定义数组,就像定义任何其他变量一样。因此,它看起来像:
您收到异常的原因是因为您实际上并未初始化数组。您声明了它,但从未将其设置为值(或为其指定大小)。您应该发布错误的堆栈跟踪,但我怀疑它是 NullPointerException。
You define an array in a constructor just like you would any other variable. So, it would look something like:
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.
您在这里所做的是创建新的局部变量,这些变量会隐藏类中定义的变量。
将其更改为:
What you're doing here is creating new local variables that are shadowing the ones defined in your class.
Change it to: