Methoden Datentyperstellung | NullPointerExeption

Darkj53

Mitglied
Hey ihr lieben, ich wollte euch fragen ob ihr mir helfen könnt?

Ich bekomme ständig eine NullPointer Exeption, wenn ich mein Objekt Stone als Array deklarieren will....

Java:
package Steinschnitt;

import java.util.Scanner;

import varaibles.Stone;

import arithmetik.*;

public class main {

   

    /**
     * @param args
     */
    public static void main(String[] args) {
        int l = 299;
        int p = 9999;
    Scanner sc = new Scanner(System.in);
        double geslaenge = 1;
        double fuge = 0.5;
        double hoehe = 30;
        double stones = 1;
       
   
    System.out.print("Geben sie bitte die Gesamtlänge des zu zerteilenden Stückes an: ");
    geslaenge = sc.nextDouble();
    System.out.print("Geben soe bitte die Breite der Fugen an: ");
    fuge = sc.nextDouble();
    System.out.print("Geben sie bitte eine Steinhöhe an: ");
    hoehe = sc.nextDouble();
   
   
    stones = scut.Steinschnitt_Devide(geslaenge, fuge, hoehe)[2] +1 ;
    System.out.println("stones = " +stones );
   
    Stone[] s = new Stone[(int) stones];
System.out.println("test");




System.out.print("test2");

    for (int i=1;i<=stones;i++ ){
        System.out.print("i= " +i) ;
        s[i].setLenght(scut.Steinschnitt_Devide(geslaenge, fuge, hoehe)[3]);  //hier gibt es die NullPointerExeption....
        s[i].setWidth(50);
        s[i].setHeight(50);
        s[i].setOffset( i*scut.Steinschnitt_Devide(geslaenge, fuge, hoehe)[3] + fuge, 0, 0);
       
    }
   

   
for(int i=1;i>stones;i++){
   
    System.out.println("A ->" + " x: " + s[1].getCoordsA()[1] + " y: " + s[i].getCoordsA()[2] + " z: " + s[i].getCoordsA()[3] + "\n" +
               "B ->" + " x: " + s[i].getCoordsB()[1] + " y: " + s[i].getCoordsB()[2] + " z: " + s[i].getCoordsB()[3] + "\n" +
               "C ->" + " x: " + s[i].getCoordsC()[1] + " y: " + s[i].getCoordsC()[2] + " z: " + s[i].getCoordsC()[3] + "\n" +
               "D ->" + " x: " + s[i].getCoordsD()[1] + " y: " + s[i].getCoordsD()[2] + " z: " + s[i].getCoordsD()[3] + "\n" +
               "E ->" + " x: " + s[i].getCoordsE()[1] + " y: " + s[i].getCoordsE()[2] + " z: " + s[i].getCoordsE()[3] + "\n" +
               "F ->" + " x: " + s[i].getCoordsF()[1] + " y: " + s[i].getCoordsF()[2] + " z: " + s[i].getCoordsF()[3] + "\n" +
               "G ->" + " x: " + s[i].getCoordsG()[1] + " y: " + s[i].getCoordsG()[2] + " z: " + s[i].getCoordsG()[3] + "\n" +
               "H ->" + " x: " + s[i].getCoordsH()[1] + " y: " + s[i].getCoordsH()[2] + " z: " + s[i].getCoordsH()[3] + "\n" +
               "Länge = " + s[i].getLength() + "\n" +
               "Breite = " + s[i].getWidth() + "\n" +
               "Höhe = " + s[i].getHeight() + "\n" +
               "Offset = " + " x :" + s[i].getOffset()[1] + " y: " + s[i].getOffset()[2] + " z: " + s[i].getOffset()[3] + "\n"+
               "Mitte= "   + " x: " + s[i].getMiddle()[1] + " y: " + s[i].getMiddle()[2] + " z: " + s[i].getMiddle()[3]
                       + "\n call Sdevide " +  scut.Steinschnitt_Devide(geslaenge, fuge, hoehe)[3] );
   
}
   
        sc.close();
    }

}



Java:
//hier die Logik der Klasse Stone

package varaibles;

public class Stone  {

    private double Lenght = 0;
    private double Height = 0;
    private double Width = 0;
    private double[] A = {0,0,0,0};
    private double[] B = {0,0,0,0};
    private double[] C = {0,0,0,0};
    private double[] D = {0,0,0,0};
    private double[] E = {0,0,0,0};
    private double[] F = {0,0,0,0};
    private double[] G = {0,0,0,0};
    private double[] H = {0,0,0,0};
    private double[] offset = {0,0,0,0};
    private double[] middle = {0,0,0,0};
   
    public void setOffset (double x, double y, double z){
        this.offset[1] = x;
        this.offset[2] = y;
        this.offset[3] = z;
       
        this.A[1] = this.A[1] + this.offset[1];
        this.A[2] = this.A[2] + this.offset[2];
        this.A[3] = this.A[3] + this.offset[3];
       
        this.B[1] = this.B[1] + this.offset[1];
        this.B[2] = this.B[2] + this.offset[2];
        this.B[3] = this.B[3] + this.offset[3];
       
        this.C[1] = this.C[1] + this.offset[1];
        this.C[2] = this.C[2] + this.offset[2];
        this.C[3] = this.C[3] + this.offset[3];
       
        this.D[1] = this.D[1] + this.offset[1];
        this.D[2] = this.D[2] + this.offset[2];
        this.D[3] = this.D[3] + this.offset[3];
       
        this.E[1] = this.E[1] + this.offset[1];
        this.E[2] = this.E[2] + this.offset[2];
        this.E[3] = this.E[3] + this.offset[3];
       
        this.F[1] = this.F[1] + this.offset[1];
        this.F[2] = this.F[2] + this.offset[2];
        this.F[3] = this.F[3] + this.offset[3];
       
        this.G[1] = this.G[1] + this.offset[1];
        this.G[2] = this.G[2] + this.offset[2];
        this.G[3] = this.G[3] + this.offset[3];
       
        this.H[1] = this.H[1] + this.offset[1];
        this.H[2] = this.H[2] + this.offset[2];
        this.H[3] = this.H[3] + this.offset[3];
       
    }

    public double[] getOffset(){
        for (int i=0;i<=this.A[1];i++){
            offset[1]=i;
        }
        for (int i=0;i<=this.A[2];i++){
            offset[2]=i;
        }
        for (int i=0;i<=this.A[2];i++){
            offset[2]=i;
        }
        return this.offset;
    }
   
   

    public void setLenght (double cm){
        this.Lenght = cm;
       
        //Länge AB
        if (this.B[1] - this.A[1] != this.Lenght){
            this.B[1] = this.A[1] + this.Lenght;
        }
       
        //Länge EF
        if (this.F[1] - this.E[1] != this.Lenght){
            this.F[1] = this.E[1] + this.Lenght;
        }
        //Länge DC
        if (this.C[1] - this.D[1] != this.Lenght){
            this.C[1] = this.D[1] + this.Lenght;
        }
        //Länge HG
        if (this.G[1] - this.H[1] != this.Lenght){
            this.G[1] = this.H[1] + this.Lenght;
        }
       
       
           
    }
    public void setWidth (double cm){
        this.Width = cm;
       
        //Breite AE
        if (this.E[3] - this.A[3] != this.Width){
            this.E[3] = this.A[3] + this.Width;
        }
       
        //Breite BF
        if (this.F[3] - this.B[3] != this.Width){
            this.F[3] = this.B[3] + this.Width;
        }
       
        //Breite CG
        if (this.G[3] - this.C[3] != this.Width){
            this.G[3] = this.C[3] + this.Width;
        }
       
        //Breite DH
        if (this.H[3] - this.D[3] != this.Width){
            this.H[3] = this.D[3] + this.Width;
        }
       
               

    }
    public void setHeight (double cm){
        this.Height = cm;
       
        //Höhe AD
        if (this.D[2] - this.A[2] != this.Height){
            this.D[2] = this.A[2] + this.Height;
        }
       
        //Höhe BC
        if (this.C[2] - this.B[2] != this.Height){
            this.C[2] = this.B[2] + this.Height;
        }
        //Höhe FG
        if (this.G[2] - this.F[2] != this.Height){
            this.G[2] = this.F[2] + this.Height;
        }
        //Höhe EH
        if (this.H[2] - this.E[2] != this.Height){
            this.H[2] = this.E[2] + this.Height;
        }
       
    }

    public void setCoordsA (double x, double y, double z){
        this.A[1] = x;
        this.A[2] = y;
        this.A[3] = z;
   
        //Länge AC
        if (this.B[1] - this.A[1] != this.Lenght){
            this.Lenght = this.B[1] - this.A[1];
        }
        //Höhe AD
        if (this.D[2] - this.A[2] != this.Height){
            this.Height = this.D[2] - this.A[2];
        }
        //Höhe AE
        if (this.E[3] - this.A[3] != this.Width){
            this.Width = this.E[3] - this.A[3];
        }
       
       

    }
    public void setCoordsB (double x, double y, double z){
        this.B[1] = x;
        this.B[2] = y;
        this.B[3] = z;
        //Länge BA
        if (this.B[1] - this.A[1] != this.Lenght){
            this.Lenght = this.B[1] - this.A[1];
        }
        //Höhe BC
        if (this.C[2] - this.B[2] != this.Height){
            this.Height = this.C[2] - this.B[2];
        }
        //Breite BF
        if (this.F[3] - this.B[3] != this.Width){
            this.Width = this.F[3] - this.B[3];
        }
       
    }
    public void setCoordsC (double x, double y, double z){
        this.C[1] = x;
        this.C[2] = y;
        this.C[3] = z;
       
        //Länge
        if (this.D[1] - this.C[1] != this.Lenght){
            this.Lenght = this.D[1] - this.C[1];
        }
        //Höhe
        if (this.C[2] - this.B[2] != this.Height){
            this.Height = this.C[2] - this.B[2];
        }
        //Breite
        if (this.G[3] - this.C[3] != this.Width){
            this.Width = this.G[3] - this.C[3];
        }
       
       
    }
    public void setCoordsD (double x, double y, double z){
        this.D[1] = x;
        this.D[2] = y;
        this.D[3] = z;
       
        //Länge
        if (this.C[1] - this.D[1] != this.Lenght){
            this.Lenght = this.C[1] - this.D[1];
        }
        //Höhe
        if (this.D[2] - this.A[2] != this.Height){
            this.Height = this.D[2] - this.A[2];
        }
        //Breite
        if (this.H[3] - this.D[3] != this.Width){
            this.Width = this.H[3] - this.D[3];
        }
       
    }

    public void setCoordsE (double x, double y, double z){
        this.E[1] = x;
        this.E[2] = y;
        this.E[3] = z;
       
        //Länge
        if (this.F[1] - this.E[1] != this.Lenght){
            this.Lenght = this.F[1] - this.E[1];
        }
        //Höhe
        if (this.H[2] - this.E[2] != this.Height){
            this.Height = this.H[2] - this.E[2];
        }
        //Breite
        if (this.F[3] - this.E[3] != this.Width){
            this.Width = this.F[3] - this.E[3];
        }
    }
    public void setCoordsF (double x, double y, double z){
        this.F[1] = x;
        this.F[2] = y;
        this.F[3] = z;
       
        //Länge
        if (this.F[1] - this.E[1] != this.Lenght){
            this.Lenght = this.F[1] - this.E[1];
        }
        //Höhe
        if (this.G[2] - this.F[2] != this.Height){
            this.Height = this.G[2] - this.F[2];
        }
        //Breite
        if (this.F[3] - this.B[3] != this.Width){
            this.Width = this.F[3] - this.B[3];
        }
    }
    public void setCoordsG (double x, double y, double z){
        this.G[1] = x;   
        this.G[2] = y;
        this.G[3] = z;
               
        //Länge HG
        if (this.G[1] - this.H[1] != this.Lenght){
            this.Lenght = this.G[1] - this.H[1];
        }
        //HöheFG
        if (this.G[2] - this.F[2] != this.Height){
            this.Height = this.G[2] - this.F[2];
        }
        //BreiteCG
        if (this.G[3] - this.C[3] != this.Width){
            this.Width = this.G[3] - this.C[3];
        }
       
       
    }
    public void setCoordsH (double x, double y, double z){
        this.H[1] = x;   
        this.H[2] = y;
        this.H[3] = z;
               
        //Länge
        if (this.G[1] - this.H[1] != this.Lenght){
            this.Lenght = this.G[1] - this.H[1];
        }
        //Höhe
        if (this.H[2] - this.E[2] != this.Height){
            this.Height = this.H[2] - this.E[2];
        }
        //Breite
        if (this.H[3] - this.D[3] != this.Width){
            this.Width = this.H[3] - this.D[3];
        }
    }

    public double[] getCoordsA(){
        return this.A;
    }
    public double[] getCoordsB(){
        return this.B;
    }
    public double[] getCoordsC(){
        return this.C;
    }   
    public double[] getCoordsD(){
        return this.D;
    }   

    public double[] getCoordsE(){
        return this.E;
    }
    public double[] getCoordsF(){
        return this.F;
    }   
    public double[] getCoordsG(){
        return this.G;
    }
    public double[] getCoordsH(){

        return this.H;
    }
   
    public double getLengthAB(){
        return this.B[1] - this.A[1];
    }   
    public double getLengthEF(){
        return this.F[1] - this.E[1];
    }
    public double getLenghtCD(){
        return this.C[1] - this.D[1];
    }
    public double getLenghtGH(){
        return this.G[1] - this.H[1];
    }
   
    public double getHeightDA(){
        return this.D[2] - this.A[2];
    }
    public double getHeightCB(){
        return this.C[2] - this.B[2];
    }
    public double getHeightGF(){
        return this.G[2] - this.F[2];
    }
    public double getHeightHE(){
        return this.H[2] - this.E[2];
    }
   
    public double getWidthAE(){
        return this.E[3]- this.A[3];
    }
    public double getWidthFB(){
        return this.F[3]- this.B[3];
    }
    public double getWidthGC(){
        return this.G[3]- this.C[3];
    }
    public double getWidthHD(){
        return this.H[3]- this.D[3];
    }
   
   
    public double getLength(){
        return this.Lenght;
    }
    public double getWidth(){

        return this.Width;
    }
    public double getHeight(){
        return this.Height;
    }

    public double[] getMiddle(){
        this.middle[1] = this.A[1] + this.Lenght/2;
        this.middle[2] = this.A[2] + this.Height/2;
        this.middle[3] = this.A[3] + this.Width/2;
        return this.middle;
    }
   
    public void setMiddle(double x, double y, double z){
        this.middle[1] = x;
        this.middle[2] = y;
        this.middle[3] = z;
       
        this.A[1] = this.middle[1] - (this.Lenght / 2);
        this.A[2] = this.middle[2] - (this.Height / 2);
        this.A[3] = this.middle[3]  - (this.Width / 2);
       
        this.B[1] = this.middle[1]  + (this.Lenght / 2);
        this.B[2] = this.middle[2]  - (this.Height / 2);
        this.B[3] = this.middle[3]  - (this.Width / 2);
       
        this.C[1] = this.middle[1]  + (this.Lenght / 2);
        this.C[2] = this.middle[2]  + (this.Height / 2);
        this.C[3] = this.middle[3]  - (this.Width / 2);
       
        this.D[1] = this.middle[1]  - (this.Lenght / 2);
        this.D[2] = this.middle[2]  + (this.Height / 2);
        this.D[3] = this.middle[3]  - (this.Width / 2);
       
        this.E[1] = this.middle[1]  - (this.Lenght / 2);
        this.E[2] = this.middle[2]  - (this.Height / 2);
        this.E[3] = this.middle[3]  + (this.Width / 2);
       
        this.F[1] = this.middle[1]  + (this.Lenght / 2);
        this.F[2] = this.middle[2]  - (this.Height / 2);
        this.F[3] = this.middle[3]  + (this.Width / 2);
       
        this.G[1] = this.middle[1]  + (this.Lenght / 2);
        this.G[2] = this.middle[2]  + (this.Height / 2);
        this.G[3] = this.middle[3]  + (this.Width / 2);
       
        this.H[1] = this.middle[1]  - (this.Lenght / 2);
        this.H[2] = this.middle[2]  + (this.Height / 2);
        this.H[3] = this.middle[3]  + (this.Width / 2);
    }
   
    }


Ich glaube es ist etwas sehr triviales, denn wenn ich das Objekt Stone als MEthode aufrufe denn funktioniert alles... doch ich brauche mehrere Instanzen des Objektes Stone, da aus diesen eine Mauer zusammengebaut wird :)


Vielen Dank für eure Hilfe :)
 
Zuletzt bearbeitet von einem Moderator:

Joose

Top Contributor
Du erstellst zwar ein Stone[] aber nirgends befüllst du diese Array mit Objekten. In der for-Schleife greifst du dann auf die einzelnen Index zu und willst Methode aufrufen, da aber keine Objekte vorhanden sind von dem etwas aufrufen könnte fliegt eine NullPointer Exception.

Abgesehen davon ist deine for-Schleife etwas falsch. Ein Array beginnt immer bei Index 0 und geht bis Arraylänge-1 ;)
 

Darkj53

Mitglied
Danke dir für deine Schnelle antwort joose, doch wie befülle ich denn nun das array richtig?
was habe ich vergessen? könntest du mir des etwas präziser erklären?
Danke :)
 

Darkj53

Mitglied
ja so hatte ich es vorher.... und wie verbinde ich das beides miteinander? bzw wie tue ich die einzelnen Steine in das Array? Bitte erkläre mir wie du das meinst...
 

Ähnliche Java Themen

Neue Themen


Oben