不了解返回的错误代码

时间:2016-10-07 11:57:10

标签: java

我的项目是创建一个程序,该程序读取用户输入的数字并返回与输入相对应的相关信息。我已完成我的代码并进行编译,但是当我输入房间号码时,它会返回此错误:

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)

我的完整程序如下。我已经查看了与错误相对应的行,但无法说明错误。有什么帮助吗?

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;
    }
}        

2 个答案:

答案 0 :(得分:0)

这意味着v在您使用之前未初始化。

您的artGallery对象永远不会创建 - 您只创建了它们的数组,但不是单独创建它们中的每一个。所以他们一直都是空的。

答案 1 :(得分:0)

这意味着您正在尝试访问空对象,

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

在你的setDirection调用中你正在发送空间[1],这实际上是空的,你需要这样发送

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

或在setDirection中实例化变量v

这适用于所有类似的方法