Results 1 to 2 of 2
Like Tree2Likes
  • 1 Post By jlcarrillo

Thread: Getting NullPointerException Error

  1. #1
    jlcarrillo is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Getting NullPointerException Error

    Here is the error I get and it only happens when I input an integer between (1-4) when I ask how many rectangles you wish to draw.

    Exception in thread "main" java.lang.NullPointerException
    at DrawingRectangles.printRect(DrawingRectangles.java :117)
    at DrawingRectangles.GetUserInput(DrawingRectangles.j ava:90)
    at DrawingRectangles.main(DrawingRectangles.java:6)

    Here is my code:
    Java Code:
    import java.util.Scanner;
    import java.awt.Rectangle;
    public class DrawingRectangles {
    
    	public static void main(String[] args) {
    		GetUserInput();
    	}
    	public static void GetUserInput(){
    		Scanner input = new Scanner(System.in);
    		Rectangle[] array = new Rectangle[5];
    		System.out.print("How many rectangles would you like to draw (1-5)?: ");
    		int draw = input.nextInt();
    		System.out.println();
    		
    		if (draw >= 1){
    			System.out.print("Please enter x location of rectangle 1: ");
    			int rectX = input.nextInt();
    			System.out.print("Please enter y location of rectangle 1: ");
    			int rectY = input.nextInt();
    			System.out.print("Please enter width of rectangle 1: ");
    			int rectWidth = input.nextInt();
    			System.out.print("Please enter height of rectangle 1: ");
    			int rectHeight = input.nextInt();
    			System.out.println();
    			
    			Rectangle box1 = new Rectangle();
    			box1.setRect(rectX, rectY, rectWidth, rectHeight);
    			array [0] = box1;
    			}
    		if (draw >= 2){
    			System.out.print("Please enter x location of rectangle 2: ");
    			int rectX = input.nextInt();
    			System.out.print("Please enter y location of rectangle 2: ");
    			int rectY = input.nextInt();
    			System.out.print("Please enter width of rectangle 2: ");
    			int rectWidth = input.nextInt();
    			System.out.print("Please enter height of rectangle 2: ");
    			int rectHeight = input.nextInt();
    			System.out.println();
    			
    			Rectangle box2 = new Rectangle();
    			box2.setRect(rectX, rectY, rectWidth, rectHeight);
    			array [1] = box2;
    			}	
    		if (draw >= 3){
    			System.out.print("Please enter x location of rectangle 3: ");
    			int rectX = input.nextInt();
    			System.out.print("Please enter y location of rectangle 3: ");
    			int rectY = input.nextInt();
    			System.out.print("Please enter width of rectangle 3: ");
    			int rectWidth = input.nextInt();
    			System.out.print("Please enter height of rectangle 3: ");
    			int rectHeight = input.nextInt();
    			System.out.println();
    			
    			Rectangle box3 = new Rectangle();
    			box3.setRect(rectX, rectY, rectWidth, rectHeight);
    			array [2] = box3;
    			}
    		if (draw >= 4){
    			System.out.print("Please enter x location of rectangle 4: ");
    			int rectX = input.nextInt();
    			System.out.print("Please enter y location of rectangle 4: ");
    			int rectY = input.nextInt();
    			System.out.print("Please enter width of rectangle 4: ");
    			int rectWidth = input.nextInt();
    			System.out.print("Please enter height of rectangle 4: ");
    			int rectHeight = input.nextInt();
    			System.out.println();
    			
    			Rectangle box4 = new Rectangle();
    			box4.setRect(rectX, rectY, rectWidth, rectHeight);
    			array [3] = box4;
    			}		
    		if (draw == 5){
    			System.out.print("Please enter x location of rectangle 5: ");
    			int rectX = input.nextInt();
    			System.out.print("Please enter y location of rectangle 5: ");
    			int rectY = input.nextInt();
    			System.out.print("Please enter width of rectangle 5: ");
    			int rectWidth = input.nextInt();
    			System.out.print("Please enter height of rectangle 5: ");
    			int rectHeight = input.nextInt();
    			System.out.println();
    			
    			Rectangle box5 = new Rectangle();
    			box5.setRect(rectX, rectY, rectWidth, rectHeight);
    			array [4] = box5;
    			}
    		printRect(array);
    	}
    	public static void printRect(Rectangle[] array){
    		for (int i=0; i < array[0].getHeight(); i++){
    			for (int j=0; j < array[0].getWidth(); j++){
    				System.out.print("* ");
    			}
    			System.out.println();
    		}
    		for (int i=0; i < array[1].getHeight(); i++){
    			for (int j=0; j < array[1].getWidth(); j++){
    				System.out.print("+ ");
    			}
    			System.out.println();
    		}
    		for (int i=0; i < array[2].getHeight(); i++){
    			for (int j=0; j < array[2].getWidth(); j++){
    				System.out.print(", ");
    			}
    			System.out.println();
    		}
    		for (int i=0; i < array[3].getHeight(); i++){
    			for (int j=0; j < array[3].getWidth(); j++){
    				System.out.print("! ");
    			}
    			System.out.println();
    		}
    		for (int i=0; i < array[4].getHeight(); i++){
    			for (int j=0; j < array[4].getWidth(); j++){
    				System.out.print("@ ");
    			}
    			System.out.println();
    		}
    	}
    }
    Ross P likes this.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Getting NullPointerException Error

    Suppose you enter, say, 2; array[0] and array[1] will contain a valid Rectangle (array[2] and further contain a null); your method tries to print all Rectangles in the array, even if they're null.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 06:26 PM
  2. NullPointerException Error
    By PorgrammingNoob117 in forum Java Applets
    Replies: 32
    Last Post: 04-26-2011, 12:05 AM
  3. NullPointerException error
    By Thecoffeefiend in forum New To Java
    Replies: 8
    Last Post: 04-14-2011, 07:03 PM
  4. Replies: 1
    Last Post: 12-05-2009, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •