ich habe das problem, dass mir immer beim laufen des programms eine null pointer exception geworfen wird (beim compilieren ist alles ok). dabei handelt es sich um ein 2dimensionales array (resource[][]) obwohl man mit System.outPrintln eindeutig belegen kann, dass der abgefragte wert im array auch eine belgung hat (ist da im quelltext dabei) wird trotzdem immer behauptet, dass resource[0][0] null wäre. was soll denn das? was is da los?! das begreif ich nicht!
ich bitte vielmals um hilfe :S
ich bitte vielmals um hilfe :S
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.JButton;
public class SiedlerVonCatan implements KeyListener{
static Graphics2D g2d;
static Frame main = new Frame();
static int[][] resource = new int[5][5];
static Panel setting = new Panel();
public SiedlerVonCatan(){
main.addWindowListener(new MyFinishWindow());
main.setLayout(new BorderLayout());
main.add(setting);
main.addKeyListener(this);
createAndAddComponents();
}
public class MyFinishWindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public void createAndAddComponents(){
//Setup für Länder bauen
setting.setLayout(new BorderLayout());
MapPanel map = new MapPanel();
map.setPreferredSize(new Dimension(693,600));
//map.setBackground(Color.green);
Panel resources = new Panel();
resources.setPreferredSize(new Dimension(107,600));
//resources.setBackground(Color.blue);
setting.add(map,BorderLayout.CENTER);
setting.add(resources,BorderLayout.EAST);
//Länder zufällig belegen;
int wood = 4;
int clay = 4;
int sheep = 3;
int corn = 4;
int ore = 4;
double rand;
for (int y=0; y<5; y++){
for (int x=0; x<3+Math.abs(Math.abs(2-y)-2); x=x){
System.out.println(x+" "+y);
resource[x][y]=0;
rand = Math.random();
System.out.println(rand);
if (rand<0.2 && wood>0){
resource[x][y] = 1;
wood--;
}
else if (rand<0.4 && clay>0){
resource[x][y] = 2;
clay--;
}
else if (rand<0.6 && sheep>0){
resource[x][y] = 3;
sheep--;
}
else if (rand<0.8 && corn>0){
resource[x][y] = 4;
corn--;
}
else if (rand<1.0 && ore>0){
resource[x][y] = 5;
ore--;
}
else{
x--;
}
x++;
if (x<5)
System.out.println(resource[x][y]);
}
}
double d = Math.sqrt(2700);
g2d.drawString(""+resource[0][0],(int)(346-2*d),300-180);
g2d.drawString(""+resource[1][0],346,300-180);
g2d.drawString(""+resource[2][0],(int)(346+2*d),300-180);
g2d.drawString(""+resource[0][1],(int)(346-3*d),300-90);
g2d.drawString(""+resource[1][1],(int)(346-d),300-90);
g2d.drawString(""+resource[2][1],(int)(346+d),300-90);
g2d.drawString(""+resource[3][1],(int)(346+3*d),300-90);
g2d.drawString(""+resource[0][2],(int)(346+4*d),300);
g2d.drawString(""+resource[1][2],(int)(346+2*d),300);
g2d.drawString(""+resource[2][2],346,300);
g2d.drawString(""+resource[3][2],(int)(346-2*d),300);
g2d.drawString(""+resource[4][2],(int)(346-4*d),300);
g2d.drawString(""+resource[0][3],(int)(346-3*d),300+90);
g2d.drawString(""+resource[1][3],(int)(346-d),300+90);
g2d.drawString(""+resource[2][3],(int)(346+d),300+90);
g2d.drawString(""+resource[3][3],(int)(346+3*d),300+90);
g2d.drawString(""+resource[0][4],(int)(346-2*d),300+180);
g2d.drawString(""+resource[1][4],346,300+180);
g2d.drawString(""+resource[2][4],(int)(346+2*d),300+180);
}
public class MapPanel extends Panel{
MapPanel(){}
public void paint(Graphics g){
g2d = (Graphics2D) g;
g2d.setColor(new Color(100,150,255));
g2d.fillPolygon(new int[]{0,173,519,692,519,173}, //kantenlänge 346
new int[]{300,0,0,300,600,600},
6);
g2d.setColor(Color.black);
double d = Math.sqrt(2700);
drawHexagon(346-2*d,300-180,0);
drawHexagon(346,300-180,0);
drawHexagon(346+2*d,300-180,0);
drawHexagon(346-3*d,300-90,0);
drawHexagon(346-d,300-90,0);
drawHexagon(346+d,300-90,0);
drawHexagon(346+3*d,300-90,0);
drawHexagon(346+4*d,300,0);
drawHexagon(346+2*d,300,0);
drawHexagon(346,300,0);
drawHexagon(346-2*d,300,0);
drawHexagon(346-4*d,300,0);
drawHexagon(346-3*d,300+90,0);
drawHexagon(346-d,300+90,0);
drawHexagon(346+d,300+90,0);
drawHexagon(346+3*d,300+90,0);
drawHexagon(346-2*d,300+180,0);
drawHexagon(346,300+180,0);
drawHexagon(346+2*d,300+180,0);
}
}
public static void drawHexagon(double xm, double ym, int resource){
double r = 60;
double d = Math.sqrt(2700);
g2d.drawPolygon(new int[]{(int)(xm),(int)(xm+d),(int)(xm+d),(int)(xm),(int)(xm-d),(int)(xm-d)},
new int[]{(int)(ym-r),(int)(ym-r/2.0),(int)(ym+r/2.0),(int)(ym+r),(int)(ym+r/2.0),(int)(ym-r/2.0)},
6);
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int input = e.getKeyCode();
if (input==32){
}
}
public void keyReleased(KeyEvent e) {}
public static void main (String[] args){
SiedlerVonCatan surface = new SiedlerVonCatan();
main.setVisible(true);
main.setSize(800+6,600+32);
main.setResizable(false);
main.validate();
main.requestFocus();
}
}