#[環境]
Java version 16.0.1
VS Code
Mac OS Big Surの環境です。
java -version
java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)
MacBook:09_MinutesToYearsandDayCalculator $
パソコン MacBook(Retina, 12-inch,2017), memory 8GB
#[エラー]
MacBook:37_Composition $ javac Main.java
MacBook:37_Composition $ java Main
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Bed.make()" because "this.bed" is null
at Bedroom.makeBed(Bedroom.java:14)
at Main.main(Main.java:15)
MacBook:37_Composition $
エラーはコンパイルの後に出ました。
#[学習状況]
2021年5月中旬から独学でJavaを学習しはじめました。
progateを3巡、ドットインストール1巡、スッキリJavaの本を8割読みました。現在、UdemyのJava Programming Masterclass for Software Developersというコースで動画を見たり、演習問題に取り組んでいます。
この問題はUdemyでの演習35番目なので、まったくの初めてというわけではありません。
まだまだ初心者ですが真面目に取り組んでいるので、何卒、よろしくお願いします。
#[演習問題の内容]
コンポジションの学習をしましょう。
クラスを5つ作ります。
Bed,Ceiling,Lamp,Wall、
そしてこれらのクラスを取り込んだBedroomというクラスです。
#[わからない事]
BedroomクラスのmakeBed()メソッドを呼んでいるところでエラーがでています。
模範解答も同じようにコードを書いており、何が問題なのかわかりません。
#[自分コード]
java
1public class Main { 2 public static void main(String[] args) { 3 4 Wall wall1= new Wall("West"); 5 Wall wall2= new Wall("East"); 6 Wall wall3= new Wall("South"); 7 Wall wall4= new Wall("North"); 8 9 Ceiling ceiling = new Ceiling(12,55); 10 Bed bed = new Bed ("Modern", 4,3,2,1); 11 Lamp lamp = new Lamp("Classic", false, 75); 12 13 Bedroom bedRoom = new Bedroom("Roba room", wall1, wall2, wall3, wall4, 14 ceiling, bed, lamp); 15 bedRoom.makeBed(); 16 17 bedRoom.getLamp().turnOn(); 18 } 19 20}
java
1public class Bedroom { 2 private String name; private Wall wall1; private Wall wall2; private Wall wall3; 3 private Wall wall4; private Ceiling ceiling; private Bed bed; private Lamp lamp; 4 5 public Bedroom(String name, Wall wall1, Wall wall2, Wall wall3, Wall wall4, 6 Ceiling ceiling, Bed bed, Lamp lamp){ 7 this.name=name; this.wall1=wall1; this.wall2=wall2; this.wall3=wall3; 8 this.wall4=wall4; this.lamp=lamp; 9 } 10 public Lamp getLamp() { 11 return lamp; 12 } 13 public void makeBed(){ 14 bed.make(); 15 System.out.println("Bedroom -> Making bed | "); 16 } 17}
java
1public class Bed { 2 private String style; private int pillow;private int height; 3 private int sheets;private int quilt; 4 5 public Bed(String style,int pillow,int height,int sheets,int quilt){ 6 this.style=style;this.pillow=pillow;this.height=height; 7 this.sheets=sheets;this.quilt=quilt; 8 } 9 public void make(){ 10 System.out.println("Bed -> Making | "); 11 } 12 public String getStyle() { 13 return style; 14 } 15 public int getPillow() { 16 return pillow; 17 } 18 public int getHeight() { 19 return height; 20 } 21 public int getSheets() { 22 return sheets; 23 } 24 public int getQuilt() { 25 return quilt; 26 } 27} 28 29
java
1public class Ceiling { 2 private int height; 3 private int paintedColor; 4 5 public Ceiling(int height,int paintedColor){ 6 this.height=height; 7 this.paintedColor=paintedColor; 8 } 9 public int getHeight() { 10 return height; 11 } 12 public int getPaintedColor() { 13 return paintedColor; 14 } 15 16} 17 18
java
1public class Lamp { 2 private String style; 3 private boolean battery; 4 private int globRating; 5 6 public Lamp(String style,boolean battery,int globRating){ 7 this.style=style; 8 this.battery=battery; 9 this.globRating=globRating; 10 } 11 public void turnOn(){ 12 this.battery=true; 13 System.out.println("Lamp -> Turning on"); 14 } 15 public boolean isBattery(){ 16 return battery; 17 } 18 public String getStyle() { 19 return style; 20 } 21 public int getGlobRating() { 22 return globRating; 23 } 24} 25 26
java
1public class Wall { 2 private String direction; 3 4 5 public Wall(String direction){ 6 this.direction=direction; 7 } 8 9 public String getDirection() { 10 return direction; 11 } 12 13} 14 15
#[ネットで見つけた模範回答コード]
java
1public class Bedroom { 2 private String name; 3 private Wall wall1; 4 private Wall wall2; 5 private Wall wall3; 6 private Wall wall4; 7 private Ceiling ceiling; 8 private Bed bed; 9 private Lamp lamp; 10 11 public Bedroom(String name, Wall wall1, Wall wall2, Wall wall3, Wall wall4, Ceiling ceiling, Bed bed, Lamp lamp) { 12 this.name = name; 13 this.wall1 = wall1; 14 this.wall2 = wall2; 15 this.wall3 = wall3; 16 this.wall4 = wall4; 17 this.ceiling = ceiling; 18 this.bed = bed; 19 this.lamp = lamp; 20 } 21 22 public Lamp getLamp() { 23 return lamp; 24 } 25 26 public void makeBed(){ 27 System.out.println("Bedroom -> Making bed | "); 28 bed.make(); 29 } 30} 31 32public class Wall { 33 private String direction; 34 35 public Wall(String direction) { 36 this.direction = direction; 37 } 38 39 public String getDirection() { 40 return direction; 41 } 42} 43 44 45public class Ceiling { 46 private int height; 47 private int paintedColor; 48 49 public Ceiling(int height, int paintedColor) { 50 this.height = height; 51 this.paintedColor = paintedColor; 52 } 53 54 public int getHeight() { 55 return height; 56 } 57 58 public int getPaintedColor() { 59 return paintedColor; 60 } 61} 62 63 64public class Bed { 65 private String style; 66 private int pillows; 67 private int height; 68 private int sheets; 69 private int quilt; 70 71 public Bed(String style, int pillows, int height, int sheets, int quilt) { 72 this.style = style; 73 this.pillows = pillows; 74 this.height = height; 75 this.sheets = sheets; 76 this.quilt = quilt; 77 } 78 79 public void make(){ 80 System.out.println("Bed -> Making | "); 81 } 82 83 public String getStyle() { 84 return style; 85 } 86 87 public int getPillows() { 88 return pillows; 89 } 90 91 public int getHeight() { 92 return height; 93 } 94 95 public int getSheets() { 96 return sheets; 97 } 98 99 public int getQuilt() { 100 return quilt; 101 } 102} 103 104 105public class Lamp { 106 private String style; 107 private boolean battery; 108 private int globRating; 109 110 public Lamp(String style, boolean battery, int globRating) { 111 this.style = style; 112 this.battery = battery; 113 this.globRating = globRating; 114 } 115 116 public void turnOn(){ 117 System.out.println("Lamp -> Turning on"); 118 } 119 120 public String getStyle() { 121 return style; 122 } 123 124 public boolean isBattery() { 125 return battery; 126 } 127 128 public int getGlobRating() { 129 return globRating; 130 } 131}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/07/05 15:17
2021/07/05 22:21
2021/07/06 00:48 編集
2021/07/06 00:22
2021/07/06 00:44
2021/07/06 06:52
2021/07/06 06:55 編集