Results 1 to 9 of 9
  1. #1
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default NullPointerException

    Why does it happen? Code:
    Java Code:
    package org.Bart.Tests.AI;
    
    import java.io.*;
    
    public class Main {
    	public static void print(String a) {
    		System.out.println(a);
    	}
    	public static void main(String[] args) {
    		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    		String input;
    		String[] brain;
    		brain = new String[30000];
    		brain[0] = "Hello|Hi";
    		print("Hello, welcome to my failish AI");
    		try {
    			input = reader.readLine();
    			for (String thing : brain) {
    				thing.substring(0,thing.indexOf("|") );
    				thing.toLowerCase();
    				if (thing.contains(input)) {
    					print("Found it!");
    				}
    			}
    		} catch (IOException e) {
    			print("You suck at typing");
    		}
    	}
    }
    I have no Idea why it errors.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: NullPointerException

    I have no Idea why it errors.
    The stacktrace will tell you why the error occurs. What is the stacktrace? And which lines of your code does it refer to?

  3. #3
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: NullPointerException

    Line 19

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: NullPointerException

    Moved from Advanced Java. Inability to resolve a rather obvious NPE is in no way an advanced topic.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: NullPointerException

    Hint: What's the default value of an element of an array of any reference type?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: NullPointerException

    null?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: NullPointerException

    Next hint: What happens when you attempt to invoke a method call on a reference that has a null value?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: NullPointerException

    It thorws a null pointer exception.

  9. #9
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

Similar Threads

  1. NullPointerException
    By s0meb0dy in forum New To Java
    Replies: 3
    Last Post: 10-09-2010, 08:12 PM
  2. NullPointerException
    By GPB in forum New To Java
    Replies: 8
    Last Post: 02-21-2010, 03:05 PM
  3. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 05:43 PM
  4. NullPointerException help?
    By fab5freddy in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 08:26 PM
  5. NullPointerException: I can't get rid of it.
    By mcashe in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2009, 09:16 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
  •