Dzięki za wszystkie odpowiedzi, w rezultacie póki co skorzystam z wyjątków. Mam takie pytanie już co do kodu. Oto i on:
package crossword.board;
public class BoardCell {
public boolean position[] = new boolean[3];
public boolean direction[] = new boolean[2];
public boolean enabled;
private String letter;
public void enable() {
enabled = true;
}
public void disable() {
enabled = false;
}
public void horiz() {
direction[0] = true;
}
public void vert() {
direction[1] = true;
}
public void start() {
position[0] = true;
}
public void inner() {
position[1] = true;
}
public void end() {
position[2] = true;
}
public void setContent(String content) {
if (content.length() > 0 && content.length() < 2)
letter = content;
}
public String getContent() {
return letter;
}
}
I druga klasa:
package crossword.board;
import java.util.*;
public class Board {
private BoardCell[][] board;
public Board(int x, int y) {
board = new BoardCell[x][y];
}
public int getWidth() {
return board.length;
}
public int getHeight() {
if (getWidth() > 0)
return board[0].length;
return 0;
}
public BoardCell getCell(int x, int y) {
if (x < getWidth() && y < getHeight())
return board[x][y];
return null;
}
public void setCell(int x, int y, BoardCell c) {
if (x <= getWidth() && y <= getHeight()) {
board[x][y] = c;
}
}
public LinkedList<BoardCell> getStartCells() {
LinkedList<BoardCell> temp = new LinkedList<BoardCell>();
for (int i = 0; i < getWidth(); ++i) {
for (int j = 0; j < getHeight(); ++j) {
if (board[i][j].position[0] == true)
temp.add(board[i][j]);
}
}
return temp;
}
public String createPattern(int fromx, int fromy, int tox, int toy) {
String temp = new String();
if (fromx > -1 && fromy > -1 && tox < getWidth() && toy < getHeight()) {
if (fromx == tox) {
for (int i = fromy; i <= toy; ++i) {
if (board[fromx][i].enabled == true)
temp += getCell(fromx, i);
else
temp += ".";
}
return temp;
}
if (fromy == toy) {
for (int i = fromx; i <= tox; ++i) {
if (board[i][fromy].enabled == true)
```java
```
temp += getCell(i, fromy);
else
temp += ".";
}
return temp;
}
}
return null;
}
}
```
Nie mogę dojść dlaczego, ale w funkcjach getStartCells i createPattern, kiedy sprawdzam atrybut komórki tablicy board (w funkcji getStartCells sprawdzam position[0] a w createPattern enabled) i jest ona fałszywa to również otrzymuję błąd NullPointerExeption. Faktycznie mam funkcję, która może zwrócić null i jest uzywana (getCell()) a nie obsługuję jej wyjątkiem, ale alalizująć kod linijka po linijce wyszło że kompilator nigty tam nie dochodzi. Ma ktoś jakiś pomysł? Tutaj wrzucę przykładowego maina:
package crossword.dictionary;
import crossword.board.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
Board board = new Board(3,2);
System.err.println(board.getWidth() + " " + board.getHeight());
BoardCell cell = new BoardCell();
cell.enable();
cell.start();
cell.vert();
cell.setContent("e");
BoardCell cell1 = new BoardCell();
cell1.enable();
cell1.start();
cell1.horiz();
cell1.setContent("a");
board.setCell(0, 0, cell);
board.setCell(1, 1, cell1);
System.out.println(board.getCell(0, 0).getContent());
LinkedList<BoardCell> list = new LinkedList<BoardCell>();
list = board.getStartCells();
Iterator<BoardCell> itr = list.iterator();
while (itr.hasNext())
System.err.print(itr.next().getContent());
System.out.print(board.createPattern(0, 0, 2, 0));
}
}
Dzieki z góry za wszystkie odpowiedzi :)