Help with java (null pointer exception)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help with java (null pointer exception)

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    3

    Help with java (null pointer exception)

    Hi all, I've been doing some java programing, I created a tester class for a project and when i try to output something to the terminal window i get a 'null pointer exception', any help would be appreciated, many thanks.


    All the code needed has been uploaded.

    *Extra info
    - I get the null pointer exception when i call the method within the tester class.
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2011
    Posts
    3

    Re: Help with java (null pointer exception)

    --- UPDATE ---

    The null pointer exception points to the 'getAllRallies()' Method in the Club class

    public String getAllRallies ()
    {
    String r = "";
    for (int index = 0; index < allRallies.size(); index++)
    {
    Rally m = allRallies.get(index);
    r += m.getAsString();
    }
    return r;
    }

  3. #3
    Join Date
    Mar 2011
    Posts
    7

    Re: Help with java (null pointer exception)

    I can't find your main also use [code] tags ['/'code] (clearly - the 's )

  4. #4
    Join Date
    Mar 2011
    Posts
    3

    Re: Help with java (null pointer exception)

    The Main is not there because I'm using blue J which creates the Main for you.

    I'm confused i don't understand what tags your talking about, is that to do with the way the forum shows the code or are you saying i need to do something with the code :S

  5. #5
    Join Date
    Mar 2011
    Location
    Dunwoody GA
    Posts
    8

    Re: Help with java (null pointer exception)

    Look at the JavaDocs for HashMap. Your code is trying to iterate the HashMap using an index and that's not the way you loop through the HashMap members.
    Code:
        public String getAllRallies ()
        {
            String r = "";
            for (int index = 0; index < allRallies.size(); index++)
            {
                Rally m = allRallies.get(index);
                r += m.getAsString();
            }   
            return r;
        }
    Instead, you would do something like this:
    Code:
        public String getAllRallies ()
        {
          String r = "";
          Set set = allRallies.entrySet();
          Iterator i = set.iterator();
    
          while(i.hasNext()){
            Map.Entry me = (Map.Entry)i.next();
            Rally m = (Rally) me.getValue();
            r += m.getAsString();
          }
          return r;
        }
    Ray

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: Help with java (null pointer exception)

    Quote Originally Posted by AlterEgo1234 View Post
    The Main is not there because I'm using blue J which creates the Main for you.
    And this is why nobody should use BlueJ.

    Somebody got the bright idea of obfuscating what is really happening for students to try and get them to focus on the code / methods they write. When these same students have to write real code and run it elsewhere, or set up an environment, they fall flat on their face.

    The code tags that he is talking about are called BBCode (google them for the markup standard). They start with a "[" and end with a "]" and have the word "code" inbetween. They work like XML markup and need a closing tag as well with the "/" before the word.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center