Results 1 to 4 of 4
  1. #1
    amanrv is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default NullPointerException is heck of a problem!

    Java Code:
            BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
            String username="";
            File file=new File("C:/aman/usernames.txt");
            for(int i=1;i>0;i++){
            System.out.println("Enter new Username");
            username=in.readLine();
            BufferedReader in1=new BufferedReader(new FileReader(file));
            String str="123";
            for( ; ; )
            {
                if(str==null)
                {
                    break;
                }
                str=in1.readLine();
                if(str.equals(username))
                {
                    System.out.println("This username is already taken!");
                }
            }
            }
            BufferedWriter out=new BufferedWriter(new FileWriter(file, true));
            out.newLine();
            out.write(username);
            out.close();
    well,this is my code, and it always shows nullpointerexception at somewhere inside the for( ; ; ). please help me correct it!

    Last edited by JosAH; 07-17-2011 at 01:23 PM. Reason: added [code] ... [/code] tags

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    At somewhere inside the loop? You understand the stack trace tells you exactly what line the exception was from. A null pointer exception is caused generally by calling a method on something that is null.

    Also, if you are reading a file in the for(;;) loop you should use a while loop like this:

    Java Code:
    String s;
    while((s=in.readLine())!= null){
      //do stuff
    }
    Rather than testing for null and breaking out of the loop and getting input in the loop.
    Last edited by sunde887; 07-17-2011 at 01:17 PM.

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

    Default

    You should look at the order that you are doing things.
    You test the variable before you change its value instead of after it is changed.

  4. #4
    amanrv is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

Similar Threads

  1. What the heck is wrong with this code.
    By eLancaster in forum New To Java
    Replies: 12
    Last Post: 06-08-2011, 07:09 AM
  2. What the heck??? Need urgent help!
    By Julian in forum New To Java
    Replies: 4
    Last Post: 05-07-2011, 02:26 PM
  3. problem with nullPointerException
    By Bat0u89 in forum New To Java
    Replies: 3
    Last Post: 03-22-2010, 04:25 PM
  4. NullPointerException() problem
    By mollybaba4 in forum New To Java
    Replies: 1
    Last Post: 02-02-2010, 03:25 PM
  5. nullPointerException problem
    By conandor in forum Networking
    Replies: 1
    Last Post: 08-14-2007, 01:22 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
  •