我正在使用此代码部分(在StatsController类中):
String spl_name = lne.substring(0, idx_firstnumber);
SpecialList spl = new SpecialList(spl_name);
声明(需要):
String lne = "";
int idx_firstnumber = 0;
变量“lne”有127个字符。如果我继续调试,只需一步,就是变量“spl_name”中的正确字符。在下一步中,我创建了一个特殊列表(具有特殊功能的普通列表)。
编辑:变量“idx_firstnumber”为31。
构造函数的代码如下:
//Klasse
public class SpecialList {
//Variablen
private ArrayList<SpecialListItem> _list = new ArrayList<SpecialListItem>();
private String _name = "";
//Konstruktor
SpecialList(String name){
_name = name;
}
.. non relevant code
调试器和调试时的值的图像: http://dancger.pf-control.de/images
因此,如果我继续使用F5,程序将崩溃并显示错误消息“Source not found”。在调试窗口中现在是:
owns: Object ?id=2235?
ClassNotFoundException?Throwable?.<init>?String, Throwable? line: 286
ClassNotFoundException?Exception?.<init>?String, Throwable? line: not available
ClassNotFoundException?ReflectiveOperationException?.<init>?String, Throwable? line: not available
ClassNotFoundException.<init>?String? line: not available
URLClassLoader$1.run?? line: not available
URLClassLoader$1.run?? line: not available
AccessController.doPrivileged?PrivilegedExceptionAction<T>, AccessControlContext? line: not available [native method]
Launcher$ExtClassLoader?URLClassLoader?.findClass?String? line: not available
Launcher$ExtClassLoader?ClassLoader?.loadClass?String, boolean? line: not available
Launcher$AppClassLoader?ClassLoader?.loadClass?String, boolean? line: not available
Launcher$AppClassLoader.loadClass?String, boolean? line: not available
Launcher$AppClassLoader?ClassLoader?.loadClass?String? line: not available
StatsController.readdatafromfile?String? line: 108
Frm_Main.btn_show_clicked?? line: 209
Frm_Main.access$1?Frm_Main? line: 186
Frm_Main$3.actionPerformed?ActionEvent? line: 139
JButton?AbstractButton?.fireActionPerformed?ActionEvent? line: not available
AbstractButton$Handler.actionPerformed?ActionEvent? line: not available
DefaultButtonModel.fireActionPerformed?ActionEvent? line: not available
DefaultButtonModel.setPressed?boolean? line: not available
BasicButtonListener.mouseReleased?MouseEvent? line: not available
Some more..
如果我运行programm normal(无调试),则会出现errormessage:
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 31
at java.lang.String.substring(Unknown Source)
at business.StatsController.readdatafromfile(StatsController.java:107)
at gui.Frm_Main.btn_show_clicked(Frm_Main.java:209)
at gui.Frm_Main.access$1(Frm_Main.java:186)
at gui.Frm_Main$3.actionPerformed(Frm_Main.java:139)
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$000(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
我的问题是: 为什么程序崩溃或会崩溃?
这是一个普通的类,带有一个需要字符串的构造函数。字符串已填满!
有没有人有想法?
编辑2:我重建了整个项目。新电脑,新日食,新课程。同样的问题。
编辑3:问题是变量idx_firstnumber
。如果我取31(idx_firstnumber
的值),它就有效。但是如果程序必须使用这些变量,它就会崩溃。
由于编辑3,这个方法的完整代码。我认为问题在于:
public boolean readdatafromfile(String pth){
//Textinhalt holen
String[] input = fc.GetFileContentAsLines(pth);
//Inhalt prüfen
if (input.length == 0){
return false;
}
//Aktuelle Daten löschen
datalist.clear();
//Aktuellen Pfad ändern
currentpath = pth;
//Variablen
int idx_firstnumber = 0;
int idx_lastnumber = 0;
boolean bl_readpolicies = false;
//Auslesen
for (int idx = 0; idx < input.length;idx++){
//Zeile bestimmen
String lne = input[idx];
//Index oder Daten lesen
if (!bl_readpolicies){
//Index LastNumber lesen
if (lne.trim().contains("AVG")){
idx_lastnumber = lne.substring(0, lne.indexOf("AVG")).length();
}
//Index FirstNumber lesen
if (lne.trim().startsWith("01")){ //contains
idx_firstnumber = lne.substring(0,lne.indexOf("01")).length();
}
//Anfangspunkt suchen
if (lne.startsWith("----------")){
bl_readpolicies = true;
}
} else {
//Abbrechen wenn keine Daten mehr kommen
if (lne == ""){
break;
}
/*if (idx_firstnumber >= lne.length()) {
throw new IllegalStateException("Index " + idx_firstnumber + " out of range " + lne.length() + " [" + lne + "]");
}*/
//Neue SpezialListe erstellen
String spl_name = lne.substring(0, 31);//idx_firstnumber);
//String x = lne.
SpecialList spl = new SpecialList(spl_name);
//Alle Einträge auslesen
for(int idx2 = idx_firstnumber; idx2 <= idx_lastnumber; idx+=3){
//Auslesen der ANzahl Stunden
String anzstd_string;
try {
anzstd_string = lne.substring(idx2, 2);
} catch (Exception e) {
break;
}
//Prüfen ob numerisch
try {
//Parsen
int anzstd = Integer.parseInt(anzstd_string);
//Überschritten / Standard
if (anzstd > 48){
spl.add(new SpecialListItem(anzstd,enm_valuetyp.limitexceeded));
} else {
spl.add(new SpecialListItem(anzstd,enm_valuetyp.standard));
}
} catch (Exception e) {
switch (anzstd_string){
case "-":
spl.add(new SpecialListItem(0,enm_valuetyp.error));
break;
case "":
spl.add(new SpecialListItem(0, enm_valuetyp.blank));
break;
case "*":
spl.add(new SpecialListItem(0,enm_valuetyp.unfinished));
break;
default:
System.out.println("Fail AnzStd: " + anzstd_string);
break;
}
}
}
//Daten einer Reihe hinzufügen
datalist.add(spl);
}
}
//Erfolgreiche Rückgabe
return true;
}
编辑4:嗯,我想我遇到了问题。如果我使用50x字符“a”作为变量lne
它可以工作。但是不允许使用char??
这是字符串lne
中的值(在开头和结尾之间没有“:”):
开始:_VXC_POLICY_ai_b0_cbw_vs_01 2 1 2 2 - - - - 1 1 1 1 1 1 1 1:结束
提前致谢,Greez Dancger:)
答案 0 :(得分:1)
如果您发布的代码中出现java.lang.StringIndexOutOfBoundsException
,则会发生在此处:
String spl_name = lne.substring(0, idx_firstnumber);
由于超出lne
的字符串长度idx_firstnumber
指定的长度。
调试代码以便自己查看代码并找出idx_firstnumber
明显错误值的原因。
答案 1 :(得分:0)
问题解决了。我现在正在使用“for each”循环而不再使用“for index”..
调试器中的值相同,但它可以工作..
Greez Dancger