Kleines Problemm mit einer NullPointerException

  • Themenstarter Themenstarter Problemkind713
  • Beginndatum Beginndatum
P

Problemkind713

Gast
Ich habe folgendes Problem:

Muss ein Programm mit Polynomen schreiben.
dass folgende Methoden beinhalten soll:

Java:
public Polynom(double[] coeffs)  //Konstruktor
public double eval(double x)     //Wert an der Stelle x
public int degree()              //hoechster Exponent (mit Koeffizient != 0)
public void display()            //Ausgabe in ueblicher mathem. Form (s.o.)
public Polynom diff()            //1.Ableitung (symbolisch)
public Polynom mul(Polynom p)    //liefert this * p; requires p!=null

Also, momentan kompiliert mein Programm, genauso wie mein Testprogramm
Doch sobald ich starte bekomm ich bei der degree sofort ne NullPointerException
Ich kopier euch mal schnell meinen Konstruktor und die degree rein .... bitte um schnelle hilfe und danke im voraus ^^

Java:
class Polynom {

	private double coeffs[];
	private int x;
	private double helper;

	//----------------------------------------
	
	public Polynom(double[] coeffs) { // Konstruktor
	
		System.out.println ("Folgendes Polynom wurde erstellt:");
		
		x = coeffs.length -1;
		
			while (x>1){
					if (coeffs [x] > 0) {
						System.out.print ("+" + coeffs [x] + "x^" + x);
						x--;
					}else if (coeffs [x] < 0) {
						System.out.print ("-" + coeffs [x] + "x^" + x);
						x--;
					}else {
						x--;
					}
					
			}
			
			if (x == 1) {
				if (coeffs [x] > 0) {
					System.out.print ("+" + coeffs [x] + "x");
					x--;
				}else if (coeffs [x] < 0) {
					System.out.print ("-" + coeffs [x] + "x");
					x--;
				}else {
					x--;
				}
			}
		
			if (x == 0) {
				if (coeffs [x] > 0) {
					System.out.print ("+" + coeffs [x]);
				}else if (coeffs [x] < 0) {
					System.out.print ("-" + coeffs [x]);
				}else {
				}
				System.out.println ();
				System.out.println ();
			}
	}	
	
	public double eval (int x) {
		System.out.println ( coeffs [x-1]);
		return coeffs [x-1] ;
	}	
	
	public int degree (){
		x = 1;
		while(coeffs [coeffs.length - x] ==0){
			x++;
		}
		return coeffs.length - x;
	}
}
 
Zuletzt bearbeitet von einem Moderator:
Die Instanzvariable wird ja auch nirgendswo instanziiert. Vermutlich fehlt im Konstruktor ein einfaches:
Java:
this.coeffs = coeffs;
Dann solltest du auch aufpassen dass du mit diesem Objekt weiterarbeitest.
Für deine zukünftigen Fragen solltest du für den Code JAVA-Tags benutzen. Steht ja auch dezent in ROT auf der Seite
 

Zurück
Oben