please have a look and see if you can help.
question:
You are required to create a class SolarSystem that will define an object containing a variable number of planets. You will put the definition for the class Planet inside the definition of the class SolarSystem.
When a SolarSystem object is being created, the user specifies the name of the solar system and the number of planets in the solar system. The Planet objects should be stored in an array.
When creating a Planet object the user specifies the name of the planet and its distance
from its Sun in millions of miles. For example in our solar system, Earth is 93 million miles from the Sun. This would be stored as 93. The array of Planet objects is filled by allowing the user to enter the names of the planets and their distance from the Sun, at the keyboard.
Include a printDetails() method to display the details of the Solar System and its Planets.
Create a test class that creates an instance of SolarSystem called Our Solar System. Our Solar System has nine planets. (For the CA you can assume that Pluto is still a planet).
Call the printDetails() method.
test classCode:package Q1;
import java.util.*;
public class SolarSystem
{
static Scanner sc = new Scanner(System.in);
static Planet[] planets;
int numPlanets;
static String SSName;
public SolarSystem()
{
System.out.println("Name of solar system?");
SSName = sc.next();
sc.nextLine();
System.out.println("How many planets in your solar system?");
numPlanets = sc.nextInt();
sc.nextLine();
planets = new Planet[numPlanets];
}
static class Planet
{
String planetName;
int disFromSun; //millions of miles
public Planet()
{
for(int i = 0; i < planets.length; i++ )
{
System.out.println("What is the name of planet number " + (i+1));
planetName = sc.next();
System.out.println("How far is it from the sun?");
disFromSun = sc.nextInt();
}
}
public static void printDetails()
{
System.out.println("The name of the solar system is " + SolarSystem.SSName);
for(int i = 0; i < planets.length; i++)
{
System.out.println("Planet name: " + planets[i].planetName + "\nDistance from Sun " + planets[i].disFromSun);
}
}
}
}
compiler error:Code:package Q1;
import Q1.SolarSystem.Planet; //did this as a prompt from eclipse, though i think it isn't needed??
public class TestSolarSytem
{
public static void main(String[] args)
{
SolarSystem ourSolarSystem = new SolarSystem();
Planet ourPlanets = new Planet();
//SolarSystem.Planet planets = ourSolarSystem.new Planet(); -----> get an error when i try that:
//Illegal enclosing instance specification for type SolarSystem.Planet
//I've also tried ourPlanets.printDetails();
//and Planet.printDetails();
}
}
Name of solar system?
The milky way
How many planets in your solar system?
1
What is the name of planet number 1
Earth
How far is it from the sun?
78
The name of the solar system is The
Exception in thread "main" java.lang.NullPointerException
at Q1.SolarSystem$Planet.printDetails(SolarSystem.jav a:45)
at Q1.TestSolarSytem.main(TestSolarSytem.java:11)
so its working up until it has to print the first system.out.println in the printDetails method, then the error occurs.
Any help appreciated.

