while循环产生的错误,这段代码有啥问题?

Posted

技术标签:

【中文标题】while循环产生的错误,这段代码有啥问题?【英文标题】:Error produced from while loop, whats wrong with this code?while循环产生的错误,这段代码有什么问题? 【发布时间】:2018-09-28 21:50:50 【问题描述】:

我仍在学习 Java,并且正在使用场景构建器。每次我输入一个不在网站上的值时,它都会从中提取数据,我会收到此错误。我已经玩了好几个小时试图解决它,这让我很头疼。当我输入站点中存在的值时,我没有收到任何错误,但是当我在文本字段中输入不在站点中的内容时,我会收到该错误。

    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at com.sun.javafx.scene.control.behavior.TextFieldBehavior.fire(TextFieldBehavior.java:179)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callAction(TextInputControlBehavior.java:178)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:218)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callActionForEvent(TextInputControlBehavior.java:127)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda$new$74(BehaviorBase.java:135)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
    at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$352(GlassViewEventHandler.java:248)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
    at com.sun.glass.ui.View.notifyKey(View.java:966)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 46 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -2
    at java.lang.String.substring(String.java:1967)
    at sample.testController.findID(testController.java:85)
    at sample.testController.search(testController.java:39)
    ... 56 more

这是我的控制器文件

    package sample;
import javafx.scene.control.Label;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javafx.scene.control.TextField;

public class testController 


    @FXML
    private Button Hiscores;

    @FXML
    private TextField geSearch;

    @FXML
    private Label Price;

    Items item = new Items();

    public void search() throws MalformedURLException, IOException 
        BufferedReader br = null;

        String itemName = geSearch.getText();
        item.setName(itemName);
        item.test();
        findID();

        System.out.println("MAde it");
        System.out.println(item.getId());
        if(item.getId().equalsIgnoreCase(""))
            Price.setText("Item not found");
        else 
            String u = "http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + item.getId();
            URL url = new URL(u);
            br = new BufferedReader(new InputStreamReader(url.openStream()));

            String line = "";

            line = br.readLine();
            String lineSplit;

            String result = line.substring(line.indexOf(",\"price\":") + 9, line.indexOf(",\"today\""));
            if (result.contains("\"")) 
                result = result.substring(1, result.length() - 1);
            
            Price.setText(result);
        
    


    public void findID() throws IOException

        BufferedReader br = null;
        try 
            String u = "https://pastebin.com/raw/LhxJ7GRG";
            URL url = new URL(u);

            br = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = "";

            int counter = 0;
            String next;
            while ((line = br.readLine()) != null) 

                if(counter >= 2) 

                    next = br.readLine();


                    System.out.println("Current Line: " + line);
                    System.out.println("Next Line: " + next);
                    String tester = next.substring(next.indexOf(": \"") + 3, next.length() - 1);
                    if (geSearch.getText().equalsIgnoreCase(tester))
                        item.setName(geSearch.getText());
                        item.setId(line.substring(line.indexOf(": ") + 2, line.length() - 1));

                        break;
                    

                

                item.setId("");
                Price.setText("");
                counter++;

                    

            

        catch (MalformedURLException e) 
            System.out.println("Error");

        


    
    public void changeScene() throws Exception
        Stage primaryStage = (Stage) Hiscores.getScene().getWindow();
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setScene(new Scene(root, 500, 600));
        primaryStage.show();

    





正如 Andreas 指出的那样,也许我的问题出在测试仪上,但当我尝试此解决方案时,这并没有奏效。

if( counter < 12045)
   tester = next.substring(next.indexOf(": \"") + 3, next.length() - 1); 
else 
       tester = "";

【问题讨论】:

第 85 行是什么? : "next 中的文本中不存在时,您认为next.indexOf(": \"") 会返回什么? I downvoted because there was no effort to debug the code,或者至少你没有分享调试时发现的信息。 @Andreas 哦,这可能是问题所在,只是当项目存在时,在找到它之前,它在整个列表中都没有问题。 你还没有告诉我们哪一行是第85行,哪里出错了。 --- 您也没有共享您拥有的调试信息,即错误发生时next 和/或line 的值,并且您确实打印了这些行,所以您应该知道它们的值是什么。 【参考方案1】:

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-2

您正在尝试引用不包含在字符串中的索引。 我认为问题可能出在next.indexOf(": \"") + 3

【讨论】:

以上是关于while循环产生的错误,这段代码有啥问题?的主要内容,如果未能解决你的问题,请参考以下文章

java操作数据库时在while循环里面new一个对象和在while循环外面new一个对象有啥区别?

这段代码有啥问题?啥是运行时错误?

这段代码有啥错误

这段代码有啥错误,在二维向量中如何添加额外的元素?

在 while 循环中使用 -- 运算符会产生意想不到的结果

JavaScript While 循环