Null pointer exception

When I try to run my program, it gives me a null pointer exception

Page 1 of 1

5 Replies - 1169 Views - Last Post: 17 October 2010 - 07:24 PM Rate Topic: -----

#1 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Null pointer exception

Posted 17 October 2010 - 06:04 PM

Hello, everyone

I'm almost done with a program I've been writing for days at school, but now, I don't know what is wrong with it because every time I try to run it, I get a null pointer exception when it reaches the method below.
  
    public void addHours(int hours,int day)
    {
        card.addHours(hours, day);
    }



This method is calling another method from another class, but this is the one giving my program trouble. What could be going wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Null pointer exception

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,179
  • Joined: 18-April 07

Re: Null pointer exception

Posted 17 October 2010 - 06:22 PM

Where have you defined the instance "card" at? The error is telling you that when it runs across "card" there, it hasn't been defined. If you defined card in another method, you either have to pass it to the function along with hours and day, or you have to make card a class scope variable (Defined within the class, not defined in a method). Remember, anything you define within a method leaves scope at the end of that function. In other words, has function scope.

We can help you if you show us the entire class where the method you are showing us now is defined.

:)

This post has been edited by Martyr2: 17 October 2010 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Re: Null pointer exception

Posted 17 October 2010 - 06:30 PM

card is defned in the same class as the method. in fact, it seems that this classes methods are giving me all the trouble because when I run the program without using them, it runs beautifully. I'll put the code of TimeCard, of which card is an instance of, and of the ClockedEmployee class, which is the class that is using "card".

The abstract class that uses "card"

public abstract class ClockedEmployee extends Employee
{
    protected TimeCard card;
    protected final double OVERTIME_RATE=1.5;
    protected final int DAYS_PER_WEEK=7;    

    public void addHours(int hours,int day)
    {
        card.addHours(hours, day);
    }

    public abstract int getOvertimeHours();

    //The program wouldn't compile 
    /*@Override
    public String toString()
    {
        return idNumber+"\n"+payRate+card.toString();
    }*/
}



the TimeCard class.

public class TimeCard
{
    private int[] timeCard;
    private final int NUMBER_OF_DAYS=7;
    
    public TimeCard()
    {
        for(int i:timeCard)
        {
            i=0;
        }
        timeCard=new int[NUMBER_OF_DAYS];
    }

    public void addHours(int hours, int day)
    {
        timeCard[day]=hours;
    }

    public int getHours(int day)
    {
        return timeCard[day];
    }

    @Override
    public String toString()
    {
        String returner="";
        for(int i:timeCard)
        {
            returner += i +"  ";
        }
        return returner;
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Null pointer exception

Posted 17 October 2010 - 07:07 PM

Ok, so you defined it correctly, but you need to instantiate it. All objects must be instantiated before you can call methods on them. Otherwise, they equal null...

protected TimeCard card;



That is simply a null TimeCard object. Notice it's...null...Though you have defined it, it has no value and you cannot call it...yet.

protected TimeCard card = new TimeCard();



NOW, it has a value. You can do this here OR in a constructor. The latter is generally preferred, but for short apps like this, the former is ok too.
Was This Post Helpful? 0
  • +
  • -

#5 SilverMage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 11-October 10

Re: Null pointer exception

Posted 17 October 2010 - 07:18 PM

my god, how did that slip through? I was not only that, but something of the like in the actual timecard class! thanks Ninja Duck, you really saved the hell out of me there.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Null pointer exception

Posted 17 October 2010 - 07:24 PM

Oh yah, just noticed that one too! :D

Glad I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1