2016-10-07 15 views
0

我的項目是創建一個程序,它讀取用戶輸入的一個數字並返回與輸入相對應的相關信息。我已經完成了我的代碼,它編譯,但是當我進入一個房間號則返回此錯誤:不理解正在返回的錯誤代碼

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

提示:在Java中的數組是基於零! – Jens

回答

0

這意味着v你使用它之前未初始化。

您的artGallery對象從不創建 - 您只創建它們的數組,但不是單獨創建它們的每一個。所以他們一直都是空的。

0

這意味着你試圖訪問一個空對象,

 room[1] = setDirection(room[1], "in front of you"); 
在呼籲要發送的房間setDirection

[1],這實際上是空,你需要,而不是要麼使發送

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

或實例變量v

這適用於所有的類似的方法