ArrayIndexOutOfBoundsException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    ArrayIndexOutOfBoundsException

    Hi!
    i just started learning java ;]
    i wanna write bank accounts program
    i get an error i know its in login method but i dont know where :\
    cd u please help me with this one

    the code below reads user data from a file then sets the vars declared in a class

    Exception in thread "main" java.lang.Array IndexOutOfBound sException: 1
    at konta_bankowe.U zytkownik.login (Uzytkownik.jav a)
    at konta_bankowe.K onto.main(Konto .java)

    my code for user
    Code:
    package konta_bankowe;
    
    
    import java.io.*;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Set;
    import java.io.IOException;
    import java.util.*;
    import javax.swing.*;
    
    public class Uzytkownik {
    	private String  IDuzytkownika;
    	private String haslo;
    	private String numerKonta;
    	private String imie;
    	private String nazwisko;
    	
    	
    	 public Uzytkownik(){};
    	
    	public void setUserID(String id){
    		IDuzytkownika=id;
    	}
    	
    	public void setHaslo(String h){
    		haslo=h;
    	}
    	
    	private void setNumerKonta(String n_k){
    	numerKonta	=n_k;
    	}
    	
    	private void setImie(String i){
    		imie=i;
    		}
    	private void setNazwisko(String n){
    		nazwisko=n;
    		}
    	
    	public void displayUserData(){
    System.out.println("Numer konta"+numerKonta+" "+imie+" "+nazwisko);
    		}
    	
    public boolean login(){
         Map<String,String> map = new HashMap<String,String>();
    
    		try{
    		    // Open the file that is the first 
    		    // command line parameter
    		    FileInputStream fstream = new FileInputStream(IDuzytkownika+".txt");
    		    // Get the object of DataInputStream
    		    DataInputStream in = new DataInputStream(fstream);
    		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
    		    String strLine;
    
    		    
    		   
    		  while((strLine = br.readLine()) != null)   {   
    		  String[] dane=strLine.split(":"); 
    		  map.put(dane[0],dane[1]);
    		  }
    		    in.close();
    		  }catch (FileNotFoundException e) {
    			    System.err.println("Nie ma takiego uzytkownika(File not found!)");
    		  }catch (IOException e) {
    		          System.err.println(e);}
    		  
    		   
    		   
    if((map.get("id")==IDuzytkownika) && (map.get("haslo")==haslo)){
    			   
    
    			   
    			   
    			   
    			   
    			   String numer_konta=map.get("numer konta");
    			   String i=map.get("imie");
    			   String n=map.get("nazwisko");
    			   
    			   
    			   setNumerKonta(numer_konta);
    			   setImie(i);
    			   setNazwisko(n);
    			  return  true;
    			}else{return false;}
    }
    }
    Last edited by horace1; Mar 11 '07, 06:49 AM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you don't appear to have included your main()

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by oll3i
      Hi!
      i just started learning java ;]
      i wanna write bank accounts program
      i get an error i know its in login method but i dont know where :\
      cd u please help me with this one

      the code below reads user data from a file then sets the vars declared in a class

      Exception in thread "main" java.lang.Array IndexOutOfBound sException: 1
      at konta_bankowe.U zytkownik.login (Uzytkownik.jav a)
      at konta_bankowe.K onto.main(Konto .java)

      my code for user
      Code:
      package konta_bankowe;
       
       
      import java.io.*;
      import java.util.HashMap;
      import java.util.HashSet;
      import java.util.Set;
      import java.io.IOException;
      import java.util.*;
      import javax.swing.*;
       
      public class Uzytkownik {
      	private String IDuzytkownika;
      	private String haslo;
      	private String numerKonta;
      	private String imie;
      	private String nazwisko;
       
       
      	 public Uzytkownik(){};
       
      	public void setUserID(String id){
      		IDuzytkownika=id;
      	}
       
      	public void setHaslo(String h){
      		haslo=h;
      	}
       
      	private void setNumerKonta(String n_k){
      	numerKonta	=n_k;
      	}
       
      	private void setImie(String i){
      		imie=i;
      		}
      	private void setNazwisko(String n){
      		nazwisko=n;
      		}
       
      	public void displayUserData(){
      System.out.println("Numer konta"+numerKonta+" "+imie+" "+nazwisko);
      		}
       
      public boolean login(){
      Map<String,String> map = new HashMap<String,String>();
       
      		try{
      		 // Open the file that is the first 
      		 // command line parameter
      		 FileInputStream fstream = new FileInputStream(IDuzytkownika+".txt");
      		 // Get the object of DataInputStream
      		 DataInputStream in = new DataInputStream(fstream);
      		 BufferedReader br = new BufferedReader(new InputStreamReader(in));
      		 String strLine;
       
       
       
      		 while((strLine = br.readLine()) != null) { 
      		 String[] dane=strLine.split(":"); 
      		 map.put(dane[0],dane[1]);
      		 }
      		 in.close();
      		 }catch (FileNotFoundException e) {
      			 System.err.println("Nie ma takiego uzytkownika(File not found!)");
      		 }catch (IOException e) {
      		 System.err.println(e);}
       
       
       
      if((map.get("id")==IDuzytkownika) && (map.get("haslo")==haslo)){
       
       
       
       
       
       
      			 String numer_konta=map.get("numer konta");
      			 String i=map.get("imie");
      			 String n=map.get("nazwisko");
       
       
      			 setNumerKonta(numer_konta);
      			 setImie(i);
      			 setNazwisko(n);
      			 return true;
      			}else{return false;}
      }
      }
      Here is the culprit code

      Code:
      String[] dane=strLine.split(":");
      Creates an array of strings whose size depends on the number of ":" in the String strLine. In this case there are no ":" at all in that String (you can verify that by printing the strLine to console and see its value). So the array created has only one element at postion 0 and its length is therefore 1. When you then do

      Code:
       map.put(dane[0],dane[1]);
      You try to access positon 1 of the array which is not in the array and thus you get the exception.

      Comment

      Working...