簡體   English   中英

線程“AWT-EventQueue-1”中的異常java.lang.NumberFormatException

[英]Exception in thread “AWT-EventQueue-1” java.lang.NumberFormatException

我的applet有這個問題。 它只會在產生錯誤之前繪制一行。

這是我的代碼: http//www.so.pastebin.com/RkG5YHVQ

這是錯誤: http//www.so.pastebin.com/z1qWpFS6

看起來dan.txt在零之前包含一個額外的換行符。

只需將第38行更改為:

int line = Integer.parseInt(src.next().trim());

這將修剪導致代碼中的錯誤的數字字符串中的任何空格。

看起來您的掃描儀沒有使用空格作為分隔符,而Integer.parseInt(src.next()); 在它找到的第一個換行符上窒息。

您可以嘗試使用類似src.useDelimiter("[,\\\\s]+")來使用一個或多個空格和逗號字符作為分隔符的任何分組。

以下是您開始調試此問題的方法:

從底部向上讀取堆棧跟蹤,並注意Java源文件開始顯示的位置。 在你的情況下:

Inventory.paint(Inventory.java:51)

現在,您可以使用幾種不同的方法開始調試此行。 首選方法是將調試器附加到程序的運行實例,並查看此行發生的情況。 如果你不能這樣做,只需添加一個try catch塊來捕獲該行代碼周圍的異常,並打印出實際值與期望值。

Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For inpu
t string: "
0"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
        at java.lang.Integer.parseInt(Integer.java:449)
Tile0   at java.lang.Integer.parseInt(Integer.java:499)

        at Inventory.paint(Inventory.java:51)  <===== Your code shows up!
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
Row: 1 successfully painted.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM