質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

2178閲覧

NullPointerException のエラーが消えない。

TomofumiKimura

総合スコア65

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2017/04/16 10:37

今Cell のクラスを使ってYoung tableau を作ろうとしています。これは課題なのですが、二番目のaddToRow(Cell curr, int value)ができているかどうか確認したいので、メインメソッドにテストのコードを書いたのですが、このようなNullPointerExceptionが出てしまいます。
Exception in thread "main" java.lang.NullPointerException
at week08.Tableau.addToRow(Tableau.java:44)
at week08.Tableau.addToRow(Tableau.java:54)
at week08.Tableau.main(Tableau.java:93)
ライン44はif(curr.value != 0){なのですが、どうしてcurr.value はnull なのでしょうか?t.smallest = new Cell(2);これでt.smallest セルのクラスのvalue は2になったので curr.value は2になると思うのですが。。。。お願いします。

java

1package week08; 2 3import java.util.Scanner; 4import java.util.function.Function; 5 6/** 7 * An implementation of Young's tableau using linked cells. 8 * 9 * @author Iain Hewson 10 */ 11public class Tableau { 12 13 /** The smallest value (or root) of this Tableau. */ 14 private Cell smallest = null; 15 16 /** 17 * Adds the given value to this tableau. 18 * 19 * @param value the value to be added to this tableau. 20 */ 21 public void addValue(Integer value) { 22 if (smallest == null) { 23 smallest = new Cell(value); 24 return; 25 } 26 // complete this method 27 } 28 29 /** 30 * Adds the given value to the row beginning with 31 * <code>curr</code>, keeping the row in ascending order. If the 32 * value gets added to the end of the row <code>null</code> is 33 * returned, otherwise the bumped value is returned. 34 * 35 * @param curr the first cell in the current row. 36 * @param value the value to be added to the row. 37 * @return the bumped value, or null if the value was added to the 38 * end of the row. 39 */ 40 protected Integer addToRow(Cell curr, int value) { 41 42 int maxV = value;// current value 43 // check the cells value 44 if(curr.value != 0){ 45 if (curr.value > value){ 46 // if yes, replace the currs value with the given one 47 maxV = curr.value;// current max value 48 curr.value = value;// curr value is now the given value 49 // returns the previous value 50 return addToRow(curr.right, maxV); 51 }else{ 52 /** if curr.value is smaller than the given value 53 */ 54 return addToRow(curr.right, maxV); 55 } 56 }else{ 57 /** if curr.value is 0 then 58 adds a new cell with the given value 59 */ 60 curr.right = new Cell(maxV); 61 return null; 62 63 } 64 } 65 66 /** 67 * Interate through every cell in the tableau printing them using 68 * the given function. 69 * 70 * @param f a function which when applied to a cell should return 71 * an integer. 72 */ 73 protected void print(Function<Cell,Integer> f) { 74 for (Cell i = smallest; i != null; i = i.below) { 75 for (Cell j = i; j != null; j = j.right) { 76 System.out.printf("[%2d]", f.apply(j)); 77 } 78 System.out.println(); 79 } 80 } 81 82 /** 83 * Entry point of the program. Reads numbers from stdin and adds 84 * them to a Tableau. If <code>p</code> is input then the 85 * tableau is printed. If <code>c</code> is input then a count 86 * of the neighbours of each cell is printed. 87 * 88 * @param args command line arguments are not used. 89 */ 90 public static void main(String[] args) { 91 Tableau t = new Tableau();// 1. Tableau instance 92 t.smallest = new Cell(2); 93 System.out.println(t.addToRow(t.smallest, 4)); 94 /** 95 Scanner input = new Scanner(System.in);//2. user input 96 while (input.hasNext()) { 97 if (input.hasNextInt()) { 98 t.addValue(input.nextInt());// 99 } else { 100 String command = input.next(); 101 if ("p".equals(command)) { 102 t.print(cell -> cell.value); 103 } else if ("c".equals(command)) { 104 t.print(cell -> cell.neighbours()); 105 } 106 } 107 } 108 */ 109 110 } 111 112 /** 113 * A cell which holds a value and links to neighbouring cells. 114 */ 115 protected static class Cell { 116 /** The value held by this cell. */ 117 int value; 118 /** The cell above this cell. */ 119 Cell above; 120 /** The cell below this cell. */ 121 Cell below; 122 /** The cell to the left of this cell. */ 123 Cell left; 124 /** The cell to the right of this cell. */ 125 Cell right; 126 127 /** Creates a new cell with the given value. 128 * @param value the value contained in this cell. 129 */ 130 Cell(int value) { 131 this.value = value; 132 } 133 134 /** Returns how many horizontal and vertical (but not diagonal) 135 * neighbours this cell has. 136 * @return how many neighbours this cell has. 137 */ 138 int neighbours() { 139 int count = left != null ? 1 : 0; 140 count += right != null ? 1 : 0; 141 count += above != null ? 1 : 0; 142 count += below != null ? 1 : 0; 143 return count; 144 } 145 } 146 147} 148

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

mainでnew Cell(2)とした段階では、valueが2であるだけで、Cellのメンバである上下左右のCellはすべてnullです。まずはこれを念頭においてください。

java

1 protected Integer addToRow(Cell curr, int value) { 2 3 int maxV = value;// current value 4 // check the cells value 5 if(curr.value != 0){ 6 if (curr.value > value){ 7 // if yes, replace the currs value with the given one 8 maxV = curr.value;// current max value 9 curr.value = value;// curr value is now the given value 10 // returns the previous value 11 return addToRow(curr.right, maxV); 12 }else{ 13 /** if curr.value is smaller than the given value 14 */ 15 return addToRow(curr.right, maxV); 16 } 17 }else{ 18 /** if curr.value is 0 then 19 adds a new cell with the given value 20 */ 21 curr.right = new Cell(maxV); 22 return null; 23 24 } 25 }

このCellと4を渡してaddToRowメソッドを起動します。
Cellのvalueは0ではない(if(curr.value != 0))ので、これとint値4を比較して、2<4です。
つまり、if (curr.value > value)はfalseなので、elseブロックに入ります。
この中のreturn addToRow(curr.right, maxV)の返り値を得るために、再帰します。
この時、第1引数に今のCellのright、つまり最初に確認した通り、nullが渡されてしまいます。
再帰したaddToRowメソッドでnullのCellに対し、valueメンバを呼ぼうとしましたが、それはできないよということで、NullPointerExceptionになったのです。

スタックトレースからも再帰していることが読み取れます。

Exception in thread "main" java.lang.NullPointerException at week08.Tableau.addToRow(Tableau.java:44) at week08.Tableau.addToRow(Tableau.java:54) at week08.Tableau.main(Tableau.java:93)

例外が発生したのは確かに2行目で示されている通り44行目なのですが、それを呼び出したのはその下のスタックトレースで示されている、54行目なのです。これがreturn addToRow(curr.right, maxV);なので、再帰となって呼び出されている、つまり引数が当初と違う可能性があるということがわかります。

投稿2017/04/16 10:51

編集2017/04/16 10:55
swordone

総合スコア20651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問