0

My project is to create a program which reads a number the user inputs and return relevant information corresponding to the input. I've finished my code and it compiles, but when I enter a room number it returns this error:

Exception in thread "main" java.lang.NullPointerException
               at Room.setDirection(Room.java:93)
               at Room.roomNumber(Room.java:29)
               at Room.main(Room.java:18)

My complete program is below. I've looked at the lines corresponding to the errors but can't tell whats wrong. Any help?

import java.util.Scanner;

class artGallery
{
    String direction;
    String title;
    String artist;
    String year;
    String height;
    String width;
}

public class Room
{
    public static void main(String[] param)
    {       
        artGallery room;
        room = roomNumber();
        System.out.println("The painting " + getDirection(room) + " is called " + getTitle(room) + " and is painted by " + getArtist(room) + ". It was painted in " + getYear(room) + " and is approximately " + getHeight(room) + "cm tall and " + getWidth(room) + "cm wide.");
    }
    public static artGallery roomNumber()
    {
        int number;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Are you in room 1, 2 ,3 or 4?");
        number = scanner.nextInt();
        artGallery[] room = new artGallery[5];

        room[1] = setDirection(room[1], "in front of you");
        room[1] = setTitle(room[1], "The Starry Night");
        room[1] = setArtist(room[1], "Vincent Willem van Gogh");
        room[1] = setYear(room[1], "1889");
        room[1] = setHeight(room[1], "73.7");
        room[1] = setWidth(room[1], "92.1");

        room[2] = setDirection(room[2], "to the left of you");
        room[2] = setTitle(room[2], "The Persistence of Memory");
        room[2] = setArtist(room[2], "Salvador Domingo Felipe Jacinto Dalí i Domènech");
        room[2] = setYear(room[2], "1931");
        room[2] = setHeight(room[2], "24");
        room[2] = setWidth(room[2], "33");

        room[3] = setDirection(room[3], "to the right of you");
        room[3] = setTitle(room[3], "Liberty Leading the People");
        room[3] = setArtist(room[3], "Ferdinand Victor Eugène Delacroix");
        room[3] = setYear(room[3], "1830");
        room[3] = setHeight(room[3], "260");
        room[3] = setWidth(room[3], "325");

        room[4] = setDirection(room[4], "above you");
        room[4] = setTitle(room[4], "The Creation of Adam");
        room[4] = setArtist(room[4], "Michelangelo di Lodovico Buonarroti Simoni");
        room[4] = setYear(room[4], "1512");
        room[4] = setHeight(room[4], "280");
        room[4] = setWidth(room[4], "570");

        return room[number];
    }


    public static String getDirection(artGallery v)
    {
        return v.direction;
    }

    public static String getTitle(artGallery v)
    {
        return v.title;
    }

    public static String getArtist(artGallery v)
    {
        return v.artist;
    }

    public static String getYear(artGallery v)
    {
        return v.year;
    }

    public static String getHeight(artGallery v)
    {
        return v.height;
    }

    public static String getWidth(artGallery v)
    {
        return v.width;
    }

    public static artGallery setDirection(artGallery v, String direction)
    {
        v.direction = direction;
        return v;
    }

    public static artGallery setTitle(artGallery v, String title)
    {
        v.title = title;
        return v;
    }

    public static artGallery setArtist(artGallery v, String artist)
    {
        v.artist = artist;
        return v;
    }

    public static artGallery setYear(artGallery v, String year)
    {
        v.year = year;
        return v;
    }

    public static artGallery setHeight(artGallery v, String height)
    {
        v.height = height;
        return v;
    }

    public static artGallery setWidth(artGallery v, String width)
    {
        v.width = width;
        return v;
    }
}        
John
  • 31
  • 4

2 Answers2

0

It means that v was not initialized before you used it.

Your artGallery objects are never created -- you only created the array of them, but not each of them individually. So they stay null all the time.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

it means you are trying to access a null object,

       room[1] = setDirection(room[1], "in front of you");

in your call for setDirection you are sending room[1], this is actually null, you need instead either so sent

         room[1] = setDirection(new artGallery(), "in front of you");

or instantiate variable v in setDirection

This applies to all your similar methods

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43