import needed?

James Brooks

Gunslinger
Ranch Hand

Posts: 165

posted 10 years ago

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:

Hello. My name is Inigo Montoya. You killed my father. Prepare to die.

Eric Gero

Ranch Hand

Posts: 73

posted 10 years ago

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.

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.

-P Lavti<br />SCJP 5.0 (88%)

James Brooks

Gunslinger
Ranch Hand

Posts: 165

posted 10 years ago

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.

Hello. My name is Inigo Montoya. You killed my father. Prepare to die.

James Brooks

Gunslinger
Ranch Hand

Posts: 165

posted 10 years ago

Thanks P Latvi!

Hello. My name is Inigo Montoya. You killed my father. Prepare to die.

Eric Gero

Ranch Hand

Posts: 73

posted 10 years ago

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.