如何阅读调试错误?

时间:2017-10-13 16:50:58

标签: java

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

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

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

1 个答案:

答案 0 :(得分:0)

查看错误日志,我们可以看到它是NumberFormatExceptionString正在转换为不代表数字的数字

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: [...]

有问题的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的{​​{1}}表示转换为数字

在日志中我们看到您的代码在第107行调用了某个内容,它试图调用String

Integer.parseInt()

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

at java.lang.Integer.parseInt(Unknown Source)
at assignment11.JFrameExt$1.actionPerformed(JFrameExt.java:107)

vCount = Integer.parseInt(count.toString()); count,为了获取数据,您应该使用JTextField.getText()