import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class NameListListenerTest extends JFrame implements ActionListener{
static ArrayList<String> nameList_2;
static JFrame bframe;
static JPanel p;
//JButton userB;
public NameListListenerTest() {
try {
JButton userB;
//ArrayList<String> ls = new ArrayList<String>();
String str = "hiwhatsupmate#!#jake sally joe#!#asdfasdf#!#kurt#!#";
int start = str.indexOf("#!#");
System.out.printf("%d\n", start);
int end = str.indexOf("#!#", start+1);
System.out.printf("%d\n",end);
String userNames = str.substring(start+3, end-3);
System.out.printf("%d\n",userNames);
int x = userNames.length();
while(true) {
if(x > 1) {
if(userNames.indexOf(" ") == 0)
nameList_2.add(userNames.substring(1,userNames.indexOf(" ",1)));
else
nameList_2.add(userNames.substring(0,userNames.indexOf(" ")));
}
else
break;
}
p = new JPanel(new GridLayout(nameList_2.size(),1));
for(int i=0; i<nameList_2.size(); i++){
userB = new JButton(nameList_2.get(i));
userB.addActionListener(this);
p.add(userB);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
//nameList_2.add("kurt");
bframe=new NameListListenerTest();
bframe.add(p);
bframe.setLocation(32,32);
bframe.setSize(50,500);
bframe.setVisible(true);
bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
System.out.printf("Connectin to user\n");
}
}
我的输出是
13
30
java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at NameListListenerTest.<init>(NameListListenerTest.java:28)
at NameListListenerTest.main(NameListListenerTest.java:53)
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at NameListListenerTest.main(NameListListenerTest.java:54)
Process java exited with code 1
所以我的第一个打印语句工作,但我尝试评论事情,它似乎完全相同。我没有整个子串和while循环部分之前有这个程序工作,但我手动放入一个ArrayList然后在构造函数中添加了四个名称
编辑:
现在我要
了java.lang.NullPointerException
at NameListListenerTest.<init>(NameListListenerTest.java:36)
at NameListListenerTest.main(NameListListenerTest.java:53)
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at NameListListenerTest.main(NameListListenerTest.java:54)
修复%s
答案 0 :(得分:3)
格式说明符不正确。取代
System.out.printf("%d\n",userNames);
带
System.out.printf("%s\n",userNames);
ArrayList
nameList_2
永远不会被初始化,导致编辑中显示的NPE
。
ArrayList<String> nameList_2 = new ArrayList<>()
答案 1 :(得分:1)
问题来自:
System.out.printf("%d\n",userNames);
System.out.printf遵循 C printf 语法。在这种语法中,%d取代了整数,但是你给它一个字符串。
答案 2 :(得分:1)
由于p,你得到空指针异常。我认为
nameList_2.add(userNames.substring(0,userNames.indexOf(" ")));
有问题因此nameList_2.size()
返回null