Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Index error

  1. #1
    Member
    Join Date
    Jun 2017
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Index error

     
    	public static void main(String[] args) throws IOException {
     
    		ArrayList<String> listCliente = new ArrayList<>();
    		ArrayList<String> listCliente1 = new ArrayList<>();
     
    		BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\java\\Desktop\\prova.txt"));
    		BufferedReader reader1 = new BufferedReader(new FileReader("C:\\Users\\java\\Desktop\\prova1.txt"));
     
    		String line = reader.readLine();
            while(line!=null) {
                System.out.println(line);
                line = reader.readLine();
                listCliente.add(line);
            }
     
     
     
        	String line1 = reader1.readLine();
            while(line1!=null) {
                System.out.println(line1);
                line1 = reader1.readLine();
                listCliente1.add(line1);
            }
     
            System.out.println(listCliente);
            System.out.println(listCliente1);
     
     
     
    		for (int i = 0; i < listCliente.size(); i++) {
    			for (int j = 0; j < listCliente1.size(); j++) {
    				if (listCliente.get(i).equals(listCliente1.get(j))) {
    					System.out.println(listCliente.get(i));
    				}
    			}
    		}
     
    	}
     
    }


    Why do you give me an error?

    output:
    1
    2
    3
    1
    2
    3
    [2, 3, null]
    [2, 3, null]
    2
    3
    Exception in thread "main" java.lang.NullPointerException
    at it.nexid.Test.main(Test.java:41)

    both files have only three strings
    1
    2
    3

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Index error

    There is a possible logic problem here:
           String line1 = reader1.readLine();    // read first line
            while(line1!=null) {                       //  execute loop if first line not null
                System.out.println(line1);          // print first line
                line1 = reader1.readLine();        // read second line (could be null)
                listCliente1.add(line1);              //  save second line (could be null)
            }
    The code skips saving the first line and saves the null as the last value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2017
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Index error

    Grazie

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Index error

    Prego
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Error Accessing Array Index
    By 13whispers in forum Collections and Generics
    Replies: 3
    Last Post: March 25th, 2018, 07:03 AM
  2. error string index out of range
    By moaz amin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 2nd, 2014, 01:21 PM
  3. index help
    By zerocool18 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 4th, 2014, 12:00 PM
  4. Replies: 3
    Last Post: August 26th, 2013, 04:41 PM
  5. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM