NullPointer Exceptions

Rob1

Mitglied
Ich habe hier meine Exceptions und finde das Problem nicht.
Ich habe eine Klasse Form von der alle anderen erben.
Danach Polygon . Polygon nimmt ein Array von Punkten entgegen.
Viereck und Dreieck nehmen Punkte entgegen. Und Punkt ist die letzte Klasse.





java.lang.NullPointerException
at Polygon.area(Polygon.java:45)
at Form.toString(Form.java:53)
at Polygon.<init>(Polygon.java:13)
at Dreieck.<init>(Dreieck.java:12)
at Test.starteTest(Test.java:41)
java.lang.NullPointerException
at Polygon.area(Polygon.java:46)
at Form.toString(Form.java:53)
at Polygon.<init>(Polygon.java:13)
at Dreieck.<init>(Dreieck.java:12)
at Test.starteTest(Test.java:41)
java.lang.NullPointerException
at Polygon.area(Polygon.java:46)
at Form.toString(Form.java:53)
at Polygon.<init>(Polygon.java:13)
at Dreieck.<init>(Dreieck.java:12)
at Test.starteTest(Test.java:41)
java.lang.NullPointerException
at Polygon.area(Polygon.java:46)
at Form.toString(Form.java:53)
at Polygon.<init>(Polygon.java:13)
at Dreieck.<init>(Dreieck.java:12)
at Test.starteTest(Test.java:41)
java.lang.NullPointerException
at Polygon.area(Polygon.java:50)
at Form.toString(Form.java:53)
at Polygon.<init>(Polygon.java:13)
at Dreieck.<init>(Dreieck.java:12)
at Test.starteTest(Test.java:41)
java.lang.NullPointerException
at Polygon.area(Polygon.java:50)
at Form.toString(Form.java:53)
at Polygon.<init>(Polygon.java:13)
at Dreieck.<init>(Dreieck.java:12)
at Test.starteTest(Test.java:41)

Java:
public  class Form
{
    // instance variables - replace the example below with your own
   
   private static int id=0;
   private String typ;
   private int obj;
   
   private String properties;
   
   public Form(){
       
       id++;
       obj=id;
       
    }
    public int getId(){
        return this.obj;
    }
    
    public String getTyp(){
        return this.typ;
    }
    
    public String setTyp(String typ1){
        return this.typ=typ1;
    }
    
    public String getProperties(){
        return this.properties;
    }
    
    public String setProperties(String prop){
        return this.properties=prop;
    }
   
    public  double area(){
       return 0.0;
    }
    
    public  double outline(){
       return 0.0;
    
    }
    public  String toString(){
    return this.area()+ " " + this.outline()+ " "+ this.typ + " " + properties + " " + "]";

    }
}

Java:
public class Polygon extends Form
{
    // instance variables - replace the example below with your own
   private Punkt[] array;
   
   {super.toString();}
   String s1 ="Polygon";
   
   public Polygon(Punkt[] aP){
       super();
       array= new Punkt[aP.length];
       for(int i=0;i<aP.length;i++){
           array[i]=aP[i];
           
        }
       
       super.setTyp(s1);
       super.setProperties(this.sum(aP));

    }
    
   public Polygon(Polygon p){
      this.array=p.getArray() ;
      super.setTyp("Polygon");
      super.setProperties(this.sum(p.getArray()));
    }
    
    public String sum(Punkt[] p){
        String s="";
        for(int i=0; i<p.length;i++){
            
        s+=p[i]+",";
    }
    return s;
   }
   
   public Punkt[] getArray(){
       return array;
    }
    
   public double area(){
       double s=0;
       for(int i=0; i<array.length-1;i++){
           if(array[i]==null)
           break;
           s+=(array[i].getY()+array[i+1].getY())*(array[i].getX()-array[i+1].getX());
           
        }
        s+=(array[array.length-1].getY()+array[0].getY())*(array[array.length-1].getX()-array[0].getX());
        if(s>=0){
            s=s/2;
        }else{
            s=-s/2;
        }
       
       return s;
    }
    
   public double outline(){
       double o=0;
       for(int i=0; i<array.length-1;i++){
           if(array[i]==null)
           break;
           o+=array[i].distance(array[i+1]);
           
        }
        o+=array[array.length-1].distance(array[0]);
        return o;
    }
}

Java:
public class Viereck extends Polygon
{
    // instance variables - replace the example below with your own
   
    
    {super.toString();}
    
    public Viereck(Punkt p1,Punkt p2,Punkt p3,Punkt p4){
       super(new Punkt[]{p1,p2,p3,p4});
        
       String s="Dreieck";
        
            this.setTyp(s);
            this.setProperties(this.sum(new Punkt[]{p1,p2,p3,p4}));
        }
            
        
        
    
}
Java:
public class Dreieck extends Polygon//ngi33
{
    // instance variables - replace the example below with your own
   public Dreieck(Punkt p1, Punkt p2, Punkt p3){
       super(new Punkt[]{p1,p2,p3});
       String s="Dreieck";
       
       setTyp(s);
       setProperties(this.sum(new Punkt[]{p1,p2,p3}));
        
            System.out.println("Kein Dreieck");
            
        }
       
    
}
Java:
public class Punkt extends Form
{

private double x;
private double y;




public Punkt(double a, double b){

this.x=a;
this.y=b;
super.setTyp("Punkt");
super.setProperties(","+ x + "," + y);


}

public Punkt(Punkt point){
this.x=point.getX();
this.y=point.getY();
super.setTyp("Punkt");
super.setProperties(","+ x + "," + y);
}

public double getX(){

return this.x;
}

public double getY(){
return this.y;
}

public double distance(Punkt p){
return Math.sqrt(((x-p.getX())*(x-p.getX())) +((y-p.getY())*(y-p.getY())));
}


public String toString(){
return "Punkt[" + getId() + ","+this.x+","+this.y+"]";

}


    // instance variables - replace the example below with your own
    
}
 
Zuletzt bearbeitet:
siehst du den StackTrace?
wenn da
> at Polygon.area(Polygon.java:45)
steht, dann heißt das dass der Fehler in Zeile 45 steht,
wäre günstig zu wissen welche Zeile das hier im Forum ist,

andererseits sind ja mehrere Zeilen betroffen, viel kommt dabei nicht raus,
es wird schon irgendwas mit dem Array sein

schau dir doch ganz einfach mit Debugger oder System.out.println() an, ob das Array und sein Inhalt immer gesetzt ist,
NullPointerExceptions sind immer ganz einfach: eine verwendete Variable ist null, herausfinden welche, prüfen warum sie nicht null sein sollte, wo sie gesetzt wird usw..

> if(array==null) break;
> s+=(array.getY()+array[i+1].getY()) ...
sieht etwas komisch aus, array wird geprüft, auf array[i+1] dann aber ohne Prüfung zugegriffen?


ein break schon bei i == 0 und dann wenig später
> s+=(array[array.length-1].getY()
könnte eine zweite Problemstelle sein,

wie es aber gleich zu drei NullPointer-Code-Stellen kommt, sehe ich noch nicht 😉

java.lang.NullPointerException
at Polygon.area(Polygon.java:45)
..
java.lang.NullPointerException
at Polygon.area(Polygon.java:46)
..
java.lang.NullPointerException
at Polygon.area(Polygon.java:50)


kommt es doch irgendwie zu array == null?
die Konstruktoren sehen sicher aus, und 45, 46, 50 kommt nicht hin,
na kannst du alles bestens testen
 
Zuletzt bearbeitet von einem Moderator:
In Polygon machst Du das hier (warum eigentlich):
Java:
{super.toString();}
Wegen super ist das die toString()-Methode von Form. In dieser wird die überschreibbare (also nicht finale) Methode area() aufgerufen und zwar die von Polygon leider nicht von Form. Zu diesem Zeitupunkt ist das Array, auf das die Methode zugreifen möchte aber noch garnicht initialisiert.

Das Grundproblem ist, dass Du in Superklassen etwas wild überschreibbare Methoden aufrufst, die dort, wo sie überschrieben sind, noch nicht auf ein valide initialisiertes Objekt zugreifen können. Genau aus solchem Grund sollte man das auch vermeiden (außer bei speziellen Patterns wie Template-Method).
 
Zuletzt bearbeitet von einem Moderator:
Ich hab meine Form-Klasse umgeschrieben und fast alles rausgeworfen. Jetzt klappt es.
Hier meine Lösung:
Java:
public  class Form
{
    // instance variables - replace the example below with your own
   
   private static int id=0;
   private String typ=" ";
   private int obj=0;
   
   private String properties;
   
   public Form(){
       
       id++;
       obj=id;
       
    }
    public int getId(){
        return this.obj;
    }
    
    public String getTyp(){
        return this.typ;
    }
    
    public String setTyp(String typ1){
        return this.typ=typ1;
    }
    
   
   
   
    public  double area(){
       return 0.0;
    }
    
    public  double outline(){
       return 0.0;
    
    }
    public  String toString(){
    return "  ";

    }
}
Java:
public class Polygon extends Form
{
    // instance variables - replace the example below with your own
   private Punkt[] array;
   
   
   
   
   public Polygon(Punkt[] aP){
       super();
       array= new Punkt[aP.length];
       for(int i=0;i<aP.length;i++){
           array[i]=aP[i];
           
        }
       
       super.setTyp("Polygon");
       
    }
    
   public Polygon(Polygon p){
      this.array=p.getArray() ;
      super.setTyp("Polygon");
      
    }
    
    
   
   public Punkt[] getArray(){
       return array;
    }
    
   public double area(){
       double s=0;
       for(int i=0; i<array.length-1;i++){
           if(array[i]==null)
           break;
           s+=(array[i].getY()+array[i+1].getY())*(array[i].getX()-array[i+1].getX());
           
        }
        s+=(array[array.length-1].getY()+array[0].getY())*(array[array.length-1].getX()-array[0].getX());
        if(s>=0){
            s=s/2;
        }else{
            s=-s/2;
        }
       
       return s;
    }
    
   public double outline(){
       double o=0;
       for(int i=0; i<array.length-1;i++){
           if(array[i]==null)
           break;
           o+=array[i].distance(array[i+1]);
           
        }
        o+=array[array.length-1].distance(array[0]);
        return o;
    }
    
    public String toString(){
        String s="";
        for(int i=0; i<array.length-1;i++){
            s+=array[i].toString()+",";
        }
        s+=array[array.length-1].toString();
        
        return this.getTyp() +"["+s+"]";
    }
}
Java:
public class Punkt extends Form
{

private double x;
private double y;




public Punkt(double a, double b){

this.x=a;
this.y=b;
super.setTyp("Punkt");



}

public Punkt(Punkt point){
super();
this.x=point.getX();
this.y=point.getY();
super.setTyp("Punkt");

}

public double getX(){

return this.x;
}

public double getY(){
return this.y;
}

public double distance(Punkt p){
return Math.sqrt(((x-p.getX())*(x-p.getX())) +((y-p.getY())*(y-p.getY())));
}


public String toString(){
return getTyp()+"[" + getId() + ","+this.x+","+this.y+"]";

}


    // instance variables - replace the example below with your own
    
}
Java:
public class Kreis extends Form
{

    // instance variables - replace the example below with your own
   private Punkt mittelpunkt;
   private double radius;
   
   {super.toString();}

   public Kreis(Punkt m, double r){
       this.mittelpunkt=m;
       this.radius=r;
       
       

    }
   
    
   public Kreis(Kreis k){
       this.mittelpunkt=k.getMittelpunkt();
       this.radius=k.getRadius();
       

       

    }
    
   public Punkt getMittelpunkt(){
       return mittelpunkt;
    }
    
   public double getRadius(){
       return radius;
    }
    
   public double area(){
       double s=0;
       s=Math.PI*radius*radius;
       return s;
    }
    
   public double outline(){
       double s=0;
       s=2*(Math.PI*radius);
       return s;
    }


public String toString(){
return "Kreis"+"["+getId()+","+radius+","+mittelpunkt.toString()+"]";

}
}
Java:
public class Viereck extends Polygon
{
    // instance variables - replace the example below with your own
   
    
    {super.toString();}
    
    public Viereck(Punkt p1,Punkt p2,Punkt p3,Punkt p4){
       super(new Punkt[]{p1,p2,p3,p4});
        
       
        
            
            System.out.println("Viereck"+super.toString());
        }
            
        
        
    
}
Java:
public class Dreieck extends Polygon//ngi33
{
    // instance variables - replace the example below with your own
   public Dreieck(Punkt p1, Punkt p2, Punkt p3){
       super(new Punkt[]{p1,p2,p3});
       
       
       
       
        System.out.println("Dreieck"+super.toString());
            
            
        }
       
   
}
 

Neue Themen


Zurück
Oben