NullPointerException error

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 794 Views - Last Post: 23 March 2010 - 01:51 PM Rate Topic: -----

#1 Guest_Missing return statement*


Reputation:

NullPointerException error

Posted 23 March 2010 - 08:47 AM

/** 
     * Return how many members joined in the given month. 
     * @param month The month we are interested in. 
     * @return How many members joined in the given month. 
     */
    public int joinedInMonth(int month)
    {
        if(month <1 || month > 12){
            System.out.println("Error");
        }else{
            int count =0;
            for(Membership x:clubMembership){
            if(x.getMonth() == month)
            count++;
        }
        return count;
    }
    }



I keep getting a missing statement error although I have included am return statement

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0

Replies To: NullPointerException error

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: NullPointerException error

Posted 23 March 2010 - 08:57 AM

Since your return statement is in your else block, your method may not return anything. However, since you're method is declared with a non-void return type, you must make sure your method returns a value (in this case, an int). If you want to return a flag to signify an invalid input, you could return a negative value (maybe in your if statement).
Was This Post Helpful? 0
  • +
  • -

#3 Guest_variable count might not..*


Reputation:

Re: NullPointerException error

Posted 23 March 2010 - 10:16 AM

I played around with the code but now I get a new error "variable count might not been initialized"
  /** 
     * Return how many members joined in the given month. 
     * @param month The month we are interested in. 
     * @return How many members joined in the given month. 
     */
    public int joinedInMonth(int month)
    {
        int count;
        if(month <1 || month > 12){
            System.out.println("Error");
        }else{
        count = 0;
        for(Membership x : clubMembership){
        if(x.getMonth() == month)
            count ++;
  
        }
    }
        return count;
} 

Was This Post Helpful? 0

#4 Guest_Guest*


Reputation:

Re: NullPointerException error

Posted 23 March 2010 - 10:17 AM

Can anyone help me find a solution
Was This Post Helpful? 0

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: NullPointerException error

Posted 23 March 2010 - 10:19 AM

Because you initialize "count" inside the "else". If the "if" executes, then the message of "Error" is printed, and "count" is returned. "count" being not initialized, then a garbage value is returned (probably something like -231020301 ...). Just initialize "count" to 0 when you declare it:
int count = 0;


In this way, when month is lower than 1 or bigger than 12, 0 is returned.

This post has been edited by sarmanu: 23 March 2010 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: NullPointerException error

Posted 23 March 2010 - 10:48 AM

global variables are initialized automatically by the compiler.
int global variables are initialized to 0.
local variables do not.
attemoting such code:

public class Test {
   public static void main(String[] args){
       int localInt;
       System.out.println(localInt);
   }
}

will give you an error.

However this code:
public class Test {
   static int globalInt;
   public static void main(String[] args){
       System.out.println(globalInt);
   }
}

will print 0.

In your case, you assign count with a value only inside the else block.
what happens if the if statement is true? count will never get a value!
and you will not be able to use it later in that method.
as sarmanu said, better initiate count when declaring it.
int count = -1;

you can initiate it to -1, so if the method returns -1, you know that there was an error with the month parameter.

This post has been edited by japanir: 23 March 2010 - 10:49 AM

Was This Post Helpful? 1
  • +
  • -

#7 Guest_thanks*


Reputation:

Re: NullPointerException error

Posted 23 March 2010 - 10:51 AM

thanks guys
Was This Post Helpful? 0

#8 Guest_Guest*


Reputation:

Re: NullPointerException error

Posted 23 March 2010 - 10:53 AM

I am working on the last method

 /**
     * Remove from the club's collection all members who joined
     * in the given month, and return them stored in a separate
     * collection object.
     * @param month The month of the Membership.
     * @param year The year of the Membership.
     */
    public ArrayList purge(int month, int year)
    {
        for (Membership member : clubMembership){
            if ( member.getMonth() == month ){
                if (member.getYear() == year ){
                    removedMembers.add(member);
                }
            }
        }
        for (Membership member : removedMembers){
            clubMembership.remove(member);
        }
        return removedMembers;
    }
}


when I run the method the get the following error "NullPointerException" null

Please help
Was This Post Helpful? 0

#9 Guest_Full adder*


Reputation:

Re: NullPointerException error

Posted 23 March 2010 - 11:29 AM

I am working on the last method

/**
     * Remove from the club's collection all members who joined
     * in the given month, and return them stored in a separate
     * collection object.
     * @param month The month of the Membership.
     * @param year The year of the Membership.
     */
    public ArrayList purge(int month, int year)
    {
        for (Membership member : clubMembership){
            if ( member.getMonth() == month ){
                if (member.getYear() == year ){
                    removedMembers.add(member);
                }
            }
        }
        for (Membership member : removedMembers){
            clubMembership.remove(member);
        }
        return removedMembers;
    }
}


when I run the method the get the following error "NullPointerException" null

Please help
Was This Post Helpful? 0

#10 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: NullPointerException error

Posted 23 March 2010 - 11:35 AM

apparently one of the Objects you refer to in this method is null.
but It is really hard to say without looking at the rest of your code..
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Guest*


Reputation:

Re: NullPointerException error

Posted 23 March 2010 - 11:44 AM

public class Club
{
    private ArrayList<Membership>clubMembership;
    private ArrayList<Membership>removedMembers;
    
    /**
     * Constructor for objects of class Club
     */
    public Club()
    {
        clubMembership = new ArrayList<Membership>();
        
    }

    /**
     * Add a new member to the club's list of members.
     * @param member The member object to be added.
     */
    public void join(Membership member)
    {
        clubMembership.add(member);
    }

    /**
     * @return The number of members (Membership objects) in
     *         the club.
     */
    public int numberOfMembers()
    {
        return clubMembership.size();
    }
    
    /** 
     * Return how many members joined in the given month. 
     * @param month The month we are interested in. 
     * @return How many members joined in the given month. 
     */
    public int joinedInMonth(int month)
    {
        int count = 0;
        if(month <1 || month > 12){
            System.out.println("Error");
        }else{
        for(Membership x : clubMembership){
            if(x.getMonth() == month)
            count ++;
        }
    }
        return count;
}

/**
     * Remove from the club's collection all members who joined
     * in the given month, and return them stored in a separate
     * collection object.
     * @param month The month of the Membership.
     * @param year The year of the Membership.
     */
    public ArrayList purge(int month, int year)
    {
        for (Membership member : clubMembership){
            if ( member.getMonth() == month ){
                if (member.getYear() == year ){
                    removedMembers.add(member);
                }
            }
        }
        for (Membership member : removedMembers){
            clubMembership.remove(member);
        }
        return removedMembers;
    }
} 


The full code
Was This Post Helpful? 0

#12 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: NullPointerException error

Posted 23 March 2010 - 11:48 AM

you didn't initialized removedMembers.
now it is null.
you cannot call methods from a null object.
you should add this:
ArrayList<Membership>removedMembers = new ArrayList<Membership>();

Was This Post Helpful? 0
  • +
  • -

#13 Guest_Guest*


Reputation:

Re: NullPointerException error

Posted 23 March 2010 - 12:03 PM

in the method joinedInMonth(int month) if I enter a invalid number for the months there should be a error, but the output is not a error?

please help
Was This Post Helpful? 0

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: NullPointerException error

Posted 23 March 2010 - 12:39 PM

Can you tell us what line number it is on and provide the error message? I'm guessing that you have a null reference in your ArrayList some how, or that one of your ArrayLists wasn't properly initialized.
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,962
  • Joined: 15-July 08

Re: NullPointerException error

Posted 23 March 2010 - 01:43 PM

Well, you can throw an exception...Personally, I like IllegalArgumentException as it describes your needs best. Also, instead of System.out.println(), use System.err.println(), as it is the error stream:

    public int joinedInMonth(int month) throw IllegalArgumentException
    {
        int count = 0;
        if(month <1 || month > 12){
            System.err.println("Error");
            throw new IllegalArgumentException("Month must be between 1 and 12 inclusive.");
        }else{
            for(Membership x : clubMembership){
                if(x.getMonth() == month)
                    count ++;
            }
        }
        return count;
    }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2