Java 如何读取调试错误?

Java 如何读取调试错误?,java,Java,我有一个调试错误,我真的不知道如何阅读。我认为这与我的文本字段输入没有解析成整数这一事实有关 对于某些上下文,我正在创建一个GUI程序,它使用两个线程编写一个文件。每个线程向用户选择的指定文件写入指定的消息(特定次数)。例如,线程1可以向abc.txt写入10次“Hi”,线程2可以向同一文件写入10次“Bye”。我将有我的图形用户界面代码下的调试作为参考。提前谢谢你: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatExc

我有一个调试错误,我真的不知道如何阅读。我认为这与我的文本字段输入没有解析成整数这一事实有关

对于某些上下文,我正在创建一个GUI程序,它使用两个线程编写一个文件。每个线程向用户选择的指定文件写入指定的消息(特定次数)。例如,线程1可以向abc.txt写入10次“Hi”,线程2可以向同一文件写入10次“Bye”。我将有我的图形用户界面代码下的调试作为参考。提前谢谢你:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "javax.swing.JTextField[,506,5,92x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@7ec75020,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=8,columnWidth=11,command=,horizontalAlignment=LEADING]"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at assignment11.JFrameExt$1.actionPerformed(JFrameExt.java:107)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
GUI代码:

// GUI components
private JFrame main;
private JPanel topPane;
private JPanel midPane;
private JPanel botPane;
private JLabel tmsg1;
private JLabel tmsg2;
private JLabel tcount;
private JLabel tfname;
private JTextField msg1;
private JTextField msg2;
private JTextField count;
private JTextField fName;
private JButton write;
private JButton sWrite;
private JButton cWrite;
private JButton display;
private JButton clear;
private JTextArea stuff;
private JScrollPane scroll;

// local variables
private String message1;
private String message2;
private String fileName;
private int vCount;

public JFrameExt() {

    main = new JFrame();
    topPane = new JPanel();
    midPane = new JPanel();
    botPane = new JPanel();

    // creating components
    tmsg1 = new JLabel("Msg 1:");
    tmsg2 = new JLabel("Msg 2:");
    tcount = new JLabel("Count:");
    tfname = new JLabel("File Name:");
    msg1 = new JTextField(8);
    msg2 = new JTextField(8);
    count = new JTextField(8);
    fName = new JTextField(10);
    write = new JButton("Write");
    sWrite = new JButton("Sync Write");
    cWrite = new JButton("Coop Write");
    display = new JButton("Display");
    clear = new JButton("Clear");
    stuff = new JTextArea(45, 70);
    scroll = new JScrollPane(stuff);

    stuff.setEditable(false);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    // adding components
    main.add(topPane, BorderLayout.PAGE_START);
    main.add(midPane, BorderLayout.CENTER);
    main.add(botPane, BorderLayout.PAGE_END);

    topPane.add(tmsg1);
    topPane.add(msg1);
    topPane.add(tmsg2);
    topPane.add(msg2);
    topPane.add(tcount);
    topPane.add(count);
    topPane.add(tfname);
    topPane.add(fName);
    midPane.add(scroll);
    botPane.add(write);
    botPane.add(sWrite);
    botPane.add(cWrite);
    botPane.add(display);
    botPane.add(clear);

    // changing colors
    topPane.setBackground(Color.RED);
    midPane.setBackground(Color.CYAN);
    botPane.setBackground(Color.GREEN);

    // setting sizes
    main.setSize(1000, 900);

    // setting main jFrame parameters
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setLocationRelativeTo(null);
    ;
    main.setVisible(true);

    // write ActionListener
    write.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            message1 = msg1.getText().toString();
            message2 = msg2.getText().toString();
            fileName = fName.getText().toString();
            vCount = Integer.parseInt(count.getText().toString().trim());

            NoSyncRunnable noSync1 = new NoSyncRunnable(message1, fileName, vCount);
            NoSyncRunnable noSync2 = new NoSyncRunnable(message2, fileName, vCount);
            Thread t1 = new Thread(noSync1);
            Thread t2 = new Thread(noSync2);
            t1.start();
            t2.start();
        }
    });

    // sWrite ActionListener
    sWrite.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            message1 = msg1.toString();
            message2 = msg2.toString();
            fileName = fName.toString();
            vCount = Integer.parseInt(count.toString());
            Object obj = new Object();

            CompSyncRunnable compSync1 = new CompSyncRunnable(message1, fileName, vCount, obj);
            CompSyncRunnable compSync2 = new CompSyncRunnable(message2, fileName, vCount, obj);
            Thread t1 = new Thread(compSync1);
            Thread t2 = new Thread(compSync2);
            t1.start();
            t2.start();
        }
    });

    // cWrite ActionListener
    cWrite.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            message1 = msg1.toString();
            message2 = msg2.toString();
            fileName = fName.toString();
            vCount = Integer.parseInt(count.toString());
            Object obj = new Object();

            CoopSyncRunnable compSync1 = new CoopSyncRunnable(message1, fileName, vCount, obj);
            CoopSyncRunnable compSync2 = new CoopSyncRunnable(message2, fileName, vCount, obj);
            Thread t1 = new Thread(compSync1);
            Thread t2 = new Thread(compSync2);
            t1.start();
            t2.start();
        }
    });

    display.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                FileReader reader = new FileReader(fileName);
                BufferedReader br = new BufferedReader(reader);
                stuff.read(br, null);
                br.close();
                stuff.requestFocus();
            }

            catch (Exception e2) {
                System.out.println(e2);
            }
        }
    });

    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            msg1.setText("");
            msg2.setText("");
            count.setText("");
            fName.setText("");
            stuff.setText("");
        }
    });
}

查看错误日志,我们可以看到这是一个NumberFormatException,一个字符串在不表示数字时被转换为数字

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: [...]
问题字符串显示在第一行的错误日志中

[...] NumberFormatException: For input string: "javax.swing.JTextField[,506,5,92x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@7ec75020,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=8,columnWidth=11,command=,horizontalAlignment=LEADING]"
前几位javax.swing.JTextField[,506,5,92x20,layout=…使您看起来好像在尝试将JTextField的字符串表示形式转换为数字

进一步查看日志,我们看到您的代码在第107行调用了某个东西,它试图调用Integer.parseInt

查看您的代码,我们可以找到问题行

vCount = Integer.parseInt(count.toString());

count是一个JTextField,要获取它的数据,您应该使用它。

第一行告诉您它无法解析整数,它告诉您它试图解析java.lang.NumberFormatException:对于输入字符串:javax.swing.JTextField…该字符串看起来像JTextField上的toString,因此它告诉您ad是一个JTextField,您直接对它进行字符串化,而不是获取它的文本并将其传递给parseInt。它似乎来自count.toString,您在其中忘记了一个getText。通常,在代码中使用toString方法的大量调用是一个坏主意。JTextField.getText已经返回了一个字符串-在这种情况下,它完全没有必要ng JTextField.toString不好,因为如果没有toString,编译器会准确地告诉您代码的错误位置。谢谢,你们两个!我只是想知道,为什么使用toString而不是getText不好?谢谢!我想知道为什么使用toString不好?@Brandon它肯定会返回一个字符串,但不会返回字符串yo您正在查找。请尝试添加System.out.printlncount.toString;和System.out.printlncount.getText;。在这种情况下,JTextField.toString方法对于调试比获取文本字段的内容更有用
vCount = Integer.parseInt(count.toString());