gpt4 book ai didi

java - java gui程序上的llegalFormatConversionException错误

转载 作者:行者123 更新时间:2023-11-29 07:58:24 24 4
gpt4 key购买 nike

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 之后

最佳答案

格式说明符不正确。替换

System.out.printf("%d\n",userNames);

System.out.printf("%s\n",userNames);

此外,ArrayList nameList_2 从未初始化,导致编辑中显示的 NPE

ArrayList<String> nameList_2 = new ArrayList<>()

关于java - java gui程序上的llegalFormatConversionException错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com