0

我正在使用这个代码部分(在 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,程序会崩溃并显示错误消息“找不到源”。现在在调试窗口中是:

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..

如果我正常运行程序(无调试),则出现以下错误消息:

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:嗯,我想我有问题。如果我使用 50 倍的字符“a”作为变量lne,它可以工作。但是不允许 char 怎么办?!?

这是字符串中的值(在开始和结束之间没有“:”)lne

开始:_VXC_POLICY_ai_b0_cbw_vs_01 2 1 2 2 - - - - 1 1 1 1 1 1 1 1 :结束

在此先感谢,greez 舞者 :)

4

2 回答 2

1

如果在java.lang.StringIndexOutOfBoundsException您发布的代码中被抛出,它会发生在这里:

String spl_name = lne.substring(0, idx_firstnumber);

由于超出了lne指定的长度的字符串长度idx_firstnumber

调试您的代码以自行查看它并找出idx_firstnumber明显错误值的原因。

于 2013-01-04T07:31:36.360 回答
0

问题解决了。我现在正在使用“for each”循环,而不再使用“for index”..

调试器中的值相同,但它有效..

格力兹舞者

于 2013-03-08T10:57:06.623 回答