Question: I am suppose to have my array before the main class but I am getting the error 7 errors found: File: C:UsersdiegoOneDriveDesktopschoolSpring 2020 classesHow to
I am suppose to have my array before the main class but I am getting the error
7 errors found: File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 60] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 61] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 67] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 73] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 79] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 85] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 104] Error: non-static variable objectArray cannot be referenced from a static context
I believe the error lies with how i declared the array
ResidencePolicy objectArray[] = new ResidencePolicy[6];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* * This is a test harness for the Policy class. * * This process will exercise all methods included in the Policy class: * > null constructor * > full constructor * -- all set methods * > toString * -- 5 of 6 get methods directly * -- txtType method * -- remaining get method * * This process will use an array type to hold Policy objects and an enhanced for loop. * * * */ public class Test_ResidencePolicy { /* * Global scoping for variables/data structures is STRONGLY DISCOURAGED, but here it makes * our lives (and code) simpler, so we'll allow it. * COMPLETE THE FOLLOWING ARRAY DECLARATION */ //<<< put your declaration/instantiation of the array here >>> ResidencePolicy objectArray[] = new ResidencePolicy[6]; /* * The main method should never do the bulk of the work; it should "direct traffic" for the methods * doing the bulk of the work. We'll start using this more modular (and easier to maintain) style * of structure here. * This main calls one method to build/load the array and a second method to process it. */ public static void main( String[] args ) { /*----------------------------------------------------------------------------------------------- * The first call in the main method calls executable code to check that minimal coding * standards have been followed in creating Policy.java */ PA01_Stds_Check.checkCode( ); //----------------------------------------------------------------------------------------------- buildArray( ); // "Load" the array with Policy objects printToStrings( ); // Output the return from the toString method of each Policy object } // end main /* * The buildArray method instantiates 6 ResidencePolicy objects and stores them in the globally * declared array. * The first object instantiated will use the null constructor; all remaining objects will * call the full constructor of Policy. * Thus, both constructors and all set methods will be tested. */ private static void buildArray( ) { objectArray[0] = new ResidencePolicy( ); // Call null constructor objectArray[1] = new ResidencePolicy( "Ted Arroway", // Call full constructor, which will "MUT", // call all set methods 658542, "2487 Morning Glory", 1, 2563.58 ); objectArray[2] = new ResidencePolicy( "Eleanor Arroway", "MUT", 987420, "247 Desert Sky D541", 2, 263.08 ); objectArray[3] = new ResidencePolicy( "Drumlin, LLC", "MUT", 983214, "13 Sulky Lane", 1, 5673.98 ); objectArray[4] = new ResidencePolicy( "Hadden Industries", "LLOYDS", 147569, "37 Betelguese Penthouse", 1, 12563.58 ); objectArray[5] = new ResidencePolicy( "Palmer Joss", "CIC", 831726, "984 Ave K Apt 8942", 1, 754.36 ); } // end buildArray /* * The printToStrings method will call the toString method of each object in the array and output * the formatted String object provided. * Thus, the toString method, the txtPolType method, and all get methods will be tested. */ private static void printToStrings( ) { System.out.printf( "%n%nList of ResidencePolicy Objects in Array objectArray%n%n" ); // Add the control statement for the ENHANCED FOR LOOP here for( ResidencePolicy oneObject : objectArray ) { System.out.printf( "%s%n", oneObject.toString( ) ); } // end for loop } // end printToStrings } // end Test_ResidencePolicy
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
