Hallo,
ich habe zwei Klassen und eine Klasse, die die main beinhaltet.
Und immer bei saharaExpress.addCamel(ataAllah) kommt die NullPointerException. Aber ich verstehe nicht so ganz warum? Habe doch ein Camel Objekt erzeugt, in der Caravan Klasse scheint mir die Methode addCamel auch richtig.
Wo liegt der Fehler und warum?
Danke
mfg werdas34
ich habe zwei Klassen und eine Klasse, die die main beinhaltet.
Java:
public class Camel {
private int mp;
private int l;
private Camel nextCamel;
final int highestMP = mp;
public Camel(int mp){
if(mp > 0) {
this.mp = mp;
}
}
int getMaxpace() {
return this.highestMP;
}
void setLoad(int l) {
if(l >= 0 && mp > l) {
this.mp = mp - l;
}
if(l >= mp) {
this.mp = 0;
}
}
int getLoad() {
return this.l;
}
int getMP() {
return this.mp;
}
void setNext(Camel c) {
nextCamel = c;
}
Camel getNext() {
return nextCamel;
}
}
Java:
import java.util.ArrayList;
public class Caravan {
private ArrayList<Camel> list;
Caravan(){
ArrayList<Camel> list = new ArrayList<Camel>();
}
int getPace() {
int lowestMP = Integer.MAX_VALUE;
for(int i = 0; i < list.size(); i++) {
Camel pace = (Camel) list.get(i);
if(pace.getMP() < lowestMP) {
lowestMP = pace.getMP();
}
}
return lowestMP;
}
void addCamel(Camel c) {
list.add(c);
}
void removeCamel(Camel c) {
list.remove(c);
}
void unload() {
for(int i = 0; i < list.size(); i++) {
Camel unload = (Camel) list.get(i);
unload.setLoad(0);
}
}
void addLoad(int l) {
}
}
Code:
class ECamel2010 {
public static void main(String... args) {
Camel ataAllah = new Camel(8);
Camel desertWind = new Camel(7);
Caravan saharaExpress = new Caravan();
saharaExpress.addCamel(ataAllah);
saharaExpress.addCamel(desertWind);
System.out.println(saharaExpress.getPace()); // 7
saharaExpress.addLoad(5);
System.out.println(saharaExpress.getPace()); // 5
}
}
Und immer bei saharaExpress.addCamel(ataAllah) kommt die NullPointerException. Aber ich verstehe nicht so ganz warum? Habe doch ein Camel Objekt erzeugt, in der Caravan Klasse scheint mir die Methode addCamel auch richtig.
Wo liegt der Fehler und warum?
Danke
mfg werdas34