0

Part of a java assignment I have requires me to use a set method to input details into an array. So far I have the following method to set the details

public void setCanopy(String uniqueRef, String modelName, int width, int height, int depth, int crewToBuild, double timeToBuild, double trailerLength, String available)
{
    this.uniqueRef = uniqueRef;
    this.modelName = modelName;
    this.width = width;
    this.height = height;
    this.depth = depth;
    this.timeToBuild = timeToBuild;
    this.available = available;
    this.crewToBuild = crewToBuild;
    this.trailerLength = trailerLength;     
}

This method works fine as long as it's only used to input details to a constructor, but when I try to use it with an array I get a NullPointerException.

I also have to display these details later on using get methods. I'm using the following method to display these but again, it only works if I'm using constructors.

public static void displayCanopyDetails(Canopy c)
{
    System.out.println("Canopy reference number: " + c.getUniqueRef() + "\nCanopy model name: " + c.getModelName() + 
    "\nCanopy Dimensions (cm) - Width: " + c.getWidth() + " Height: " + c.getHeight() + " Depth: " + c.getDepth() +
    "\nCrew to build: " + c.getCrewToBuild() + "\nTime to build canopy (minutes): " + c.getTimeToBuild() + 
    "\nTrailer Length: " + c.getTrailerLength() + "\nAvailability: " + c.getAvailable());
}

Any help getting these working with arrays would be greatly appreciated. Thanks.

In my main method I have

tentDetails(c[0]);

which calls the method

public static void tentDetails(Canopy c1,)
{
    c1.setCanopy("CAN123", "Model1", 500, 200, 500, 5, 15, 10, "Available");
}

The NullPointerException error happens when it tries to run this method.

4
  • 1
    Where's the code that doesn't work? Commented Apr 29, 2015 at 11:27
  • What do you mean by:"but when I try to use it with an array I get a NullPointerException" ? Commented Apr 29, 2015 at 11:30
  • 1
    You need to use constructor because otherwise there is no object created. If you use an array for example Canopy[] canopy=new Canopy[5]; And then Canopy[1].object=5 you get NullPointer because the array gives you object storage but initializing array doesn't create the objects inside. they are nulls. If you check Canopy[1] it is null.. Commented Apr 29, 2015 at 11:30
  • I've updated my post to show the method which tries to input details to the array. This is where the NullPointerException occurs. Commented Apr 29, 2015 at 11:34

2 Answers 2

2

When you declare the array it creates an empty "bag" for objects but it doesn't create the objects themselves. When you use a method on an object in this array you get NullPointerException because the object is null. You cannot execute methods on an object before creating it first. For example:

Canopy[] canopy=new Canopy[5];  //Creates a 'storage' for 5 Canopy objects
System.out.println(Canopy[0]); //Prints null and throws NPE if you execute method

Canopy[0]=new Canopy();  //Create new Canopy object and insert it in the array

System.out.println(Canopy[0]); //Not null anymore - you can execute methods
Canopy[0].setCanopy("CAN123", "Model1", 500, 200, 500, 5, 15, 10, "Available"); // works fine
Sign up to request clarification or add additional context in comments.

Comments

1

In Java the rule is that when you create an array its elements receive the default value. An Object's default value is null, therefore initially each element in your array is null. You have to explicitly instantiate the Canopy objects like this:

for (int i = 0; i < c.length; i++) {
    c[i] = new Canopy();
}

After this, you can safely call the tentDetails() method on each element of your array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.