Results 1 to 9 of 9
Thread: StringIndexOutOfBounds exception
- 05-26-2010, 10:21 PM #1
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
StringIndexOutOfBounds exception
Hello everyone, I'm making a small simple interpreter for school. A script looks as follows:
My code for everything works, except that when I try to add a new command (besides messageBox) I get the exception. I don't know how to explain it any more, so here's the code and compile it for yourself:Java Code:file.Delete('C:\text.txt'); messageBox('Deleted', 'Gone!');
I don't know what I'm doing wrong. The exception is thrown when clicking the "Run script" button. It's weird, you can do 100 messageBox() commands one after the other and they'll all work, but any other commands will mess it up. I've tried adding a comment command, some more file commands, etc, but nothing works at all because of the damn exception.Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Scripter { public static void main(String[] args) { ScriptFrame frmMain = new ScriptFrame(); } } class ScriptFrame extends JFrame implements ActionListener{ JTextArea txtScript; JTextArea txtConsole; JScrollPane scrScroll; JButton cmdRun; JButton cmdLoad; JButton cmdSave; public ScriptFrame(){ super("DanScript"); setVisible(true); setResizable(true); setSize(510, 550); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); txtScript = new JTextArea(22, 40); add(new JScrollPane(txtScript)); //txtScript.setLineWrap(true); txtScript.setWrapStyleWord(true); txtConsole = new JTextArea(5, 40); add(new JScrollPane(txtConsole)); txtConsole.setWrapStyleWord(true); txtConsole.setEditable(false); txtConsole.setText("[---DANSCRIPT CONSOLE (Errors, etc, show up here)---]"); cmdRun = new JButton("Run script"); cmdRun.addActionListener(this); add(cmdRun); cmdLoad = new JButton("Open script"); add(cmdLoad); cmdSave = new JButton("Save script"); add(cmdSave); } public void runScript(String script){ String[] arrCommands = null; arrCommands = script.split(";" + "\n"); for(int i = 0; i < arrCommands.length; i++){ //messageBox(); variables String strContent; String strTitle = getBetween(arrCommands[i], "', '", "')"); //file.Delete(); variables String fPath; File f; if(arrCommands[i].startsWith("messageBox")){ strContent = getBetween(arrCommands[i], "('", "', "); strContent = strContent.replace("('", ""); strTitle = strTitle.replace("', '", ""); MessageBox(strContent, strTitle); }else if(arrCommands[i].startsWith("file.Delete")){ fPath = getBetween(arrCommands[i], "('", "');"); fPath = fPath.replace("('", ""); f = new File(fPath); if(!f.exists()){ txtConsole.setText(txtConsole.getText() + "\n" + "[file.Delete()] file does not exist"); return; } if(!f.canWrite()){ txtConsole.setText(txtConsole.getText() + "\n" + "[file.Delete()] does not have permission"); } }else{ return; } } } public void doNothing(){ } public void actionPerformed(ActionEvent e){ if(e.getSource() == cmdRun){ runScript(txtScript.getText()); } } public void MessageBox(String Message, String Title){ JOptionPane.showMessageDialog(this, Message, Title, JOptionPane.INFORMATION_MESSAGE); } public void MessageBox(String Message){ JOptionPane.showMessageDialog(this, Message, "DanScript", JOptionPane.INFORMATION_MESSAGE); } public static String getBetween(String search, String start, String stop) { int index = search.indexOf(start); return search.substring(index, search.indexOf(stop, index)); } }
Help is greatly appreciate :)
- 05-26-2010, 10:31 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Can you post the stack trace of the Exception?
Also what do you expect the following to do?
If I had to guess, I'd say it has something to do with the getBetween() as you are not checking for whether or not the start String is actually in the search String which would result in a -1 index which you are trying to use in the substring call.Java Code:arrCommands = script.split(";" + "\n");
- 05-26-2010, 10:42 PM #3
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
EDIT: I think you're right, so how would I go about looking for the start string in the search string?Java Code:--------------------Configuration: Scripter - JDK version 1.6.0_20 <Default> - <Default>-------------------- Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1931) at ScriptFrame.getBetween(Scripter.java:135) at ScriptFrame.runScript(Scripter.java:66) at ScriptFrame.actionPerformed(Scripter.java:115) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6263) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6028) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) Process completed.Last edited by AwesomeStorm; 05-26-2010 at 10:45 PM.
- 05-26-2010, 11:45 PM #4
What code is at line 135 in your program?at ScriptFrame.getBetween(Scripter.java:135)
How does the index get to be -1 there?
- 05-27-2010, 12:50 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
what happens if "search" doesn't have the "start" string? "index" will become -1, and when you try to sub-string off that index, you get the exception.Java Code:public static String getBetween(String search, String start, String stop) { int index = search.indexOf(start); return search.substring(index, search.indexOf(stop, index)); }
- 05-27-2010, 12:57 AM #6
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
But how is there no start string, I'm using the same code for the messageBox as I am the file.Delete and that works fine :S
- 05-27-2010, 01:29 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
you're messing up on this line:
String strTitle = getBetween(arrCommands[i], "', '", "')");
start = ', '
command=file.Delete('C:\text.txt');
command doesn't contain start.
- 05-27-2010, 03:37 AM #8
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
- 05-27-2010, 04:02 AM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Similar Threads
-
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
Re:SQL Exception
By jyotigupta6 in forum New To JavaReplies: 1Last Post: 10-28-2008, 09:37 AM -
Help with Exception
By bozovilla in forum New To JavaReplies: 2Last Post: 10-19-2008, 05:19 AM -
Where does the exception go?
By aytidaalkuhs in forum New To JavaReplies: 3Last Post: 04-07-2008, 02:24 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks