• Post Reply Bookmark Topic Watch Topic
  • New Topic

import needed?  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

This is a basic question, but can't figure it out right now. I have two different classes, as my book requires. One uses the other, creates an array of its objects and manipulates them. The problem is that my second class, UseTaxpayer, is not recognizing the objects of the first class. The suggestion NetBeans gives is import Taxpayer into the UseTaxpayer class, but that doesn't work. Here are the classes below:

Taxpayer class:


UseTaxpayer class:
 
Eric Gero
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick,

In the UseTaxpayer class, you create an array to hold Taxpayers, but you never put any Taxpayers in it, that is creating a NullPointerException when you try to call TaxpayerArray[i].getSocial();

Also, it is standard to use camelCase. Instead of TaxpayerArray, you would normally write taxPayerArray for the variable name.

public class UseTaxpayer
{
public static void main(String[] args)
{
Taxpayer TaxpayerArray[] = new Taxpayer[2];
for(int i = 0; i < TaxpayerArray.length; i++)
{
TaxpayerArray[i].getSocial();
TaxpayerArray[i].getGrossYearly();

}
}
}

Hope that helps,
Eric
 
P Lavti
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is okay. While complie just make sure they both are in same package - default package. As you dont have any package declaration in yout classes. Import of Taxpayer is not required.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I meant set instead of get, but that still doesn't solve the problem of not being able to reference a class in the same project.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks P Latvi!
 
Eric Gero
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Patrick Brooks:
Oops, I meant set instead of get, but that still doesn't solve the problem of not being able to reference a class in the same project.


The problem is not in an import, or being able to reference a class in the same project. The problem is that the array is created, but has nothing in it. When you try to access one of those objects, you will get a NullPointerException.

Eric
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!