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 8 of 8

Thread: NullPointerException error

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException error

    Hi, my program reads out of a textfile containing about 20 hockey players. At the beginning of the program, the user is given the option to sort by name, rating, club or position. No matter what I press, I always get a nullpointerexception error. This never happened before and the program works fine on my friends computer, it also used to work fine on mine and then I randomly got this error. What do I do to fix this?


    This is what is says in the bluej thingy



    java.lang.NullPointerException
    at java.lang.String$CaseInsensitiveComparator.compare (String.java:1217)
    at java.lang.String$CaseInsensitiveComparator.compare (String.java:1211)
    at java.lang.String.compareToIgnoreCase(String.java:1 258)
    at FINAL.sort(FINAL.java:206)
    at FINAL.options(FINAL.java:48)
    at FINAL.main(FINAL.java:41)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: NullPointerException error

    What do I do to fix this?
    Your first need to post code, preferably an abbreviated version, which clearly demonstrates the problem. The stack trace you posted provides a lot of information but is meaningless without the context of the code that throws the exception.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException error

    I get the error at this part of my program. It's weird though, it was working on my computer a few days ago and I suddenly got this error.


      if(options.equalsIgnoreCase("A"))
            {
                for (int x=0; x<players.length; ++x)
                {
                    for (int y=0;y<players.length-1; ++y)
                    {
                        if (players [y][0].compareToIgnoreCase(players [y+1][0]) > 0)  
                        {  
                            for(int b=0;b<=4;b++)
                            {
                                String temp = players [y][b];          
                                players [y][b] = players [y + 1][b];      
                                players [y + 1][b] = temp;
                            } 
                        }
                    }
                }
            }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: NullPointerException error

    Better, but the code still doesn't provide much context. An SSCCE is much more demonstrative. As is, I could guess that not all of your players array has been instantiated.

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException error

    Thanks, it works now. i have another question, when I add a player to the database, it works fine. But when I try to remove a player from the database, it doesn't work. Another thing is, after I use the remove function, the other functions won't work anymore (for example, after using remove, i tried to sort the players by name, it didn't work because I got an array out of bounds error) Here is the code I use for adding players



    public static void add(String players [][],String args[]) throws IOException
        {   
            BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
     
            PrintWriter output = new PrintWriter ( new FileWriter ("nhlstats.txt",true));//append file
     
            System.out.println ("Enter the name of the player (first then last)");
            String hplayer = c.readLine ();
     
            System.out.println ("Enter the position(C,RW,LW,D,G) of " + hplayer);
            String posadd = c.readLine ();
     
            System.out.println ("Enter the rating of " +hplayer);
            String ratingadd=c.readLine ();
     
            System.out.println ("Enter the country of origin of " + hplayer);
            String countadd = c.readLine ();
     
            System.out.println ("Enter the current team " + hplayer + " plays for");
            String clubadd = c.readLine ();
     
            System.out.println ("");
            System.out.println ("");
            System.out.println("The player has been added toe.");
            System.out.println("Please see text file for the added players.");
     
            int x = 0;    
            for (int i = 0 ; i <=players[x].length-1 ; ++i)
            {
                if (players [x][0] == null)
                {
                    i = x;
                    break;
                }
            }
            players [x][0] = hplayer;
            players [x][1] = posadd;
            players [x][2] = ratingadd;
            players [x][3] = countadd;
            players [x][4] = clubadd;
            output.println ();
            output.println (hplayer);
            output.println (posadd);
            output.println (ratingadd);
            output.println (countadd);
            output.println (clubadd);
            output.close();
            for (int j=0; j<players.length; ++j)//sort the name after it is added
                {
                    for (int y=0;y<players.length-1; ++y)
                    {
                        if (players [y][0].compareToIgnoreCase(players [y+1][0]) > 0)  
                        {  
                            for(int b=0;b<=4;b++)
                            {
                                String temp = players [y][b];          
                                players [y][b] = players [y + 1][b];      
                                players [y + 1][b] = temp;
                            } 
                        }
                    }
                }
        }


    The code I "tried" to use for removing players from the database is:

    public static void takeout(String players[][],String args[]) throws IOException
        {
            BufferedReader c = new BufferedReader (new InputStreamReader (System.in));
            PrintWriter out = new PrintWriter ( new FileWriter ("nhlstats.txt"));
            System.out.println ("What do you want to remove?.");
            String read = c.readLine ();
            for (int x =0; x<=players.length-1; ++x)
            {   
     
                if (players[x][0] != null)     
                {
     
                    if (players[x][0].equalsIgnoreCase(read))     
                    {
                        for (int y = 0; y <= 4; ++y)
                        {
                            players [x][y] = null;
                        }
                    }
     
     
                }
            }
     
            for (int x =0; x<=players.length-1; ++x)
            {   
                for (int y = 0; y <= 4; ++y)
                {
                    if (players[x][y] != null)
                    {
                        out.println(players[x][y]);
                    }
                }
                out.close();
            }
        }


    I think what happens is that instead of that specific player getting deleted from the text file, all the players get deleted. How do I delete only one player?
    Last edited by blazerix; January 23rd, 2011 at 05:13 PM.

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException error

    I really don't mean to be rude, but can someone please help me?

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException error

    please help!

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: NullPointerException error

    Please don't bump threads. I recommend you read the Welcome Announcements, in particular clause F. Moreover, you've provided very little information for us to help. "It doesn't work" could mean a million things. Describe the behavior you expect, and the behavior you receive (suggested reading: How To Ask Questions)

Similar Threads

  1. Problem with NullPointerException?
    By scooty199 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: November 6th, 2010, 05:13 PM
  2. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  3. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  4. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM