今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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。