- Trying to compare and print only members with Inactive status. The problem is that it will not search the array and check the status of each element. If the first element has an “Active” status, it prints the error message and doesnt continue to check the other elements.
-
If the status of the first element is “Inactive” it will print but throws an exception before the output which looks like this:
Exception in thread “main” java.lang.NullPointerException at
GymAss.MemberTest.AllInactive(MemberTest.java:293) at
GymAss.MemberTest.main(MemberTest.java:58) -
Account Number: 1 Name: 1 Date: 1 Status: INACTIVE Type: 1 Any
help would be greatly appreciated! Code below:
….
public static void AllInactive()
{
int check=0;
do
{
if ( accounts[check] instanceof Student && accounts[check].getStatus().equals("INACTIVE") )
{
System.out.printf("\nAccount Number: %d \nName: %s \nDate: %d \nStatus: %s \nType: %s \n" , accounts[check].getIdNumber(),accounts[check].getName(),accounts[check].getDateJoined(),accounts[check].getStatus(),accounts[check].getMemberType());
check++;
}
else if ( accounts[check] instanceof Adult && accounts[check].getStatus().equals("INACTIVE"))
{
System.out.printf("\nAccount Number: %d \nName: %s \nDate: %s \nStatus: %s \nType: %s \n" , accounts[check].getIdNumber(),accounts[check].getName(),accounts[check].getDateJoined(),accounts[check].getStatus(),accounts[check].getMemberType());
System.out.print("\n");
check++;
}
else
{
System.out.println("**Account Does Not Exist**");
}
} while ( accounts[check].getStatus().equals("INACTIVE"));
}
Just to make sure we are talking about the same thing: As far as I understood you have an Array containing accounts of persons. What you want to do is get all the accounts which currently are inactive and nothing else.
Why do you stop your loop if you find an active account? If you have an inactive account followed by an active followed by an inactive again, your loop will stop at the active one, because the condition of
while(accounts[check].getStatus().equals("INACTIVE"))is false. You want to check the whole array so you have to loop through the whole array. This can be done withfor(int check=0; check<accounts.length; check++)This checks every account and stops if you reach the end of the array. There is even an easier way. I recommend looking up “java foreach”.
The cause of your
NullPointerExceptionpropably is not checking if you reached the end of your array. Let’s assume you have only one inactiveStudentaccount in your array and nothing else, your code will do this:check = 0;accounts[0]aStudentand is it inactive? – yescheck = 1else ifelseaccounts[1]active? wait, there’s noaccounts[1]->NullPointerExceptionI don’t want to give you the complete code because trial and error is a great way of learning so try to fix it with those hints. If you encounter any problems you still can’t solve after a while of thinking, come back.