Results 1 to 9 of 9
  1. #1
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default StringIndexOutOfBounds exception

    Hello everyone, I'm making a small simple interpreter for school. A script looks as follows:

    Java Code:
    file.Delete('C:\text.txt');
    messageBox('Deleted', 'Gone!');
    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:
    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));
    	}
     
    	
    }
    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.

    Help is greatly appreciate :)

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Can you post the stack trace of the Exception?

    Also what do you expect the following to do?
    Java Code:
    			arrCommands = script.split(";" + "\n");
    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.

  3. #3
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    Can you post the stack trace of the Exception?

    Also what do you expect the following to do?
    Java Code:
    			arrCommands = script.split(";" + "\n");
    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:
    --------------------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.
    EDIT: I think you're right, so how would I go about looking for the start string in the search string?
    Last edited by AwesomeStorm; 05-26-2010 at 10:45 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    at ScriptFrame.getBetween(Scripter.java:135)
    What code is at line 135 in your program?

    How does the index get to be -1 there?

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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));
    	}
    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.

  6. #6
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    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

  7. #7
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    you're messing up on this line:

    String strTitle = getBetween(arrCommands[i], "', '", "')");

    start = ', '
    command=file.Delete('C:\text.txt');

    command doesn't contain start.

  8. #8
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    you're messing up on this line:

    String strTitle = getBetween(arrCommands[i], "', '", "')");

    start = ', '
    command=file.Delete('C:\text.txt');

    command doesn't contain start.
    Aw :(

    So how do I fix it lol

  9. #9
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    you should apply Feynman Problem Solving Algorithm:

    Java Code:
    1. Write down the problem.
    2. Think real hard.
    3. Write down the answer.

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. Re:SQL Exception
    By jyotigupta6 in forum New To Java
    Replies: 1
    Last Post: 10-28-2008, 09:37 AM
  3. Help with Exception
    By bozovilla in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:19 AM
  4. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 02:24 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •