AWT-EventQueue-0 NullPointerExeption bei drawLine

Simpel

Mitglied
Moin, in meinem programm wird in diesem stück:
Java:
    wand = new Himm[array1][array2];
    for( int j   = 20 - feldim;j<hoehe;) { // durchlaufen der spalten
       j +=feldim;
       for (int k = 10 - feldim; k < breite;) { // durchlaufen der zeilen
         k +=feldim;
         wand[k][j] = new Himm(k, j);

       }
     }
ein Himm Objekt pro Feld erzeugt
nun bekomme ich in der Himm klasse in diesem teil genauer schon bei wandw.drawLine
Java:
    if(n==true) {
      wandn.drawLine(x, y, x, y+Labyrinth.feldim);
    }
    if(o==true) {
      wando.drawLine(x, y+Labyrinth.feldim, x+Labyrinth.feldim, y+Labyrinth.feldim);
    }
    if(s==true) {
      wands.drawLine(x+Labyrinth.feldim, y, x+Labyrinth.feldim, y+Labyrinth.feldim);
    }
    if(w==true) {
      wandw.drawLine(x, y, x+Labyrinth.feldim, y);
    }
eine
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at System.Himm.<init>(Himm.java:89)
hier noch der gesammte code in Himm:
Java:
/*
* created 11.02.2019
*/

package System;

import java.awt.*;
import Gui.*;
import Gui.Dialog;

/**
* @author ma5761
*
*/
public class Himm {
  boolean n, o, s, w;
  int x,y;
  int     rand;
  int     wandwa = Dialog.getWandwa();
//  int wandwa =50;
  int     feldim = Labyrinth.getFeldim();
//  Graphics2D wand;
  Graphics wandn,wando,wands,wandw;
  /**
   *
   */
  public Himm(int x, int y) {
    this.x=x;
    this.y=y;
    rand = (int) (Math.random() * 100);
    if (rand < wandwa) {
      n = true;
      n = false;
    }
    rand = (int) (Math.random() * 100);
    if (rand < wandwa) {
      o = true;
      o = false;
    }
    rand = (int) (Math.random() * 100);
    if (rand < wandwa) {
      s = true;
      s = false;
    }
    rand = (int) (Math.random() * 100);
    if (rand < wandwa) {
      w = true;
      w = false;
    }
    rand = (int) (Math.random() * 100);
    if (x == 20) {
      n = true;
  }
    rand = (int) (Math.random() * 100);
    if (x == 20 && y == 10) {
      n = true;
      w = true;
    }
    rand = (int) (Math.random() * 100);
    if (x == 20 && y == 950 - Labyrinth.feldim) {
      n = false;
      o = true;
    }
    rand = (int) (Math.random() * 100);
    if (x == 560 - Labyrinth.feldim) {
      s = true;
    }
    rand = (int) (Math.random() * 100);
    if (x == 560 - Labyrinth.feldim && y == 10) {
      s = false;
      w = true;
    }
    rand = (int) (Math.random() * 100);
    if (x == 560 - Labyrinth.feldim && y == 950 - Labyrinth.feldim) {
      o = true;
      s = true;
    }
    rand = (int) (Math.random() * 100);
    if (y == 10 && x > 20 && x < 560 - Labyrinth.feldim) {
      w = true;
    }
    rand = (int) (Math.random() * 100);
    if (y == 950 - Labyrinth.feldim && x > 20 && x < 560 - Labyrinth.feldim)
      s = true;
//    this.paint(null);
   

    if(n==true) {
      wandn.drawLine(x, y, x, y+Labyrinth.feldim);
    }
    if(o==true) {
      wando.drawLine(x, y+Labyrinth.feldim, x+Labyrinth.feldim, y+Labyrinth.feldim);
    }
    if(s==true) {
      wands.drawLine(x+Labyrinth.feldim, y, x+Labyrinth.feldim, y+Labyrinth.feldim);
    }
    if(w==true) {
      wandw.drawLine(x, y, x+Labyrinth.feldim, y);
    }
   
  }
 

//  public void paint(Graphics g) {
//    if(n==true) {
//      g.drawLine(x, y, x, y+Labyrinth.feldim);
//    }
//    if(o==true) {
//      g.drawLine(x, y+Labyrinth.feldim, x+Labyrinth.feldim, y+Labyrinth.feldim);
//    }
//    if(s==true) {
//      g.drawLine(x+Labyrinth.feldim, y, x+Labyrinth.feldim, y+Labyrinth.feldim);
//    }
//    if(w==true) {
//      g.drawLine(x, y, x+Labyrinth.feldim,y);
//    }
//  }
 
  public boolean getHimmN() {
    return n;
  }
  public boolean getHimmO() {
    return o;
  }
  public boolean getHimmS() {
    return s;
  }
  public boolean getHimmW() {
    return w;
  }
  public boolean setHimmN() {
    if(n==true)
      n=false;
    else n=true;
    return n;
  }
  public boolean setHimmO() {
    if(o==true)
      o=false;
    else o=true;
    return o;
  }
  public boolean setHimmS() {
    if(s==true)
      s=false;
    else s=true;
    return s;
  }
  public boolean setHimmW() {
    if(w==true)
      w=false;
    else w=true;
    return w;
  }
}

Weiß nicht warum da nicht gezeichnent wird. Achja, das zeichnen mach ich auf einen JPanel mit Borderlayout aber das sit glaub ich erstmahl belanglos für die nullPointerExeption
Würde mich um Hilfe freuen
 

Robat

Top Contributor
wandn, wando, wands und wandw sind null, da sie nirgends initialisiert werden.
Abgesehen davon: Magst du den Sinn von diesen 4 Variablen mal erklären? Wozu brauchst du 4 Graphics-Objekte?
 

Simpel

Mitglied
dachte das macht sinn für 4 Wände jeweils ein Objekt zu haben.
ok habe jetzt die ganzen wand zu g umgeändert aber wie initialisiere ich jetzt Graphics g in meinem Fall?
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
J "Exception in thread "AWT-EventQueue-0"" Fehler AWT, Swing, JavaFX & SWT 3
A Swing Exception in thread "AWT-EventQueue-0" AWT, Swing, JavaFX & SWT 1
K Swing AWT-EventQueue-1 java.lang.NoClassDefFoundError bei setVisible(true) AWT, Swing, JavaFX & SWT 3
VfL_Freak AWT Exception in der Eventqueue AWT, Swing, JavaFX & SWT 8
X Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 AWT, Swing, JavaFX & SWT 6
Q "AWT-EventQueue-0" Exception Problem AWT, Swing, JavaFX & SWT 4
S Frage zu java.awt.EventQueue AWT, Swing, JavaFX & SWT 1
A Swing JOptionPane.showConfirmDialog, EventQueue wird weiter abgearbeitet AWT, Swing, JavaFX & SWT 2
L exception in thread awt-eventqueue-0 java.lang.nullpointerexception AWT, Swing, JavaFX & SWT 2
S Swing Exception in thread "AWT-EventQueue-0" bei Jlabel AWT, Swing, JavaFX & SWT 4
C Event Handling Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException AWT, Swing, JavaFX & SWT 43
R Swing JFrame in der EventQueue oder nicht? AWT, Swing, JavaFX & SWT 6
O Immer Exception in AWT-EventQueue-0 AWT, Swing, JavaFX & SWT 5
F Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null AWT, Swing, JavaFX & SWT 5
A AWT Methodenaufruf "AWT-EventQueue-0" java.lang.NullPointerException AWT, Swing, JavaFX & SWT 4
S Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException AWT, Swing, JavaFX & SWT 7
SuperSeppel13 Multithreading und awt.EventQueue AWT, Swing, JavaFX & SWT 7
Lony AbstractTableModel Exception in thread "AWT-EventQueue- AWT, Swing, JavaFX & SWT 3
A Exception in thread "AWT-EventQueue-0" java.lang.N AWT, Swing, JavaFX & SWT 4
M MouseEvent in der EventQueue vor versetzen AWT, Swing, JavaFX & SWT 4
S Swing Finde Grund für NullPointerExeption nicht. AWT, Swing, JavaFX & SWT 2
H drawLine im JFrame FullScreenWindow AWT, Swing, JavaFX & SWT 12
U drawLine mit Tooltip AWT, Swing, JavaFX & SWT 1
J drawLine() auf JFrame funktioniert nicht AWT, Swing, JavaFX & SWT 4
S Frage zu drawLine AWT, Swing, JavaFX & SWT 14
T Label + DrawLine AWT, Swing, JavaFX & SWT 6
G Method drawLine() AWT, Swing, JavaFX & SWT 14
X Durch DrawLine verschwindet das Fenster und die Objekte AWT, Swing, JavaFX & SWT 13
N Alle Pixel-Koordinaten von z.B. g.drawLine(.) speichern AWT, Swing, JavaFX & SWT 7
L Linie mit drawLine auf JPanel AWT, Swing, JavaFX & SWT 7
M drawLine() in JPanel hinzufügen ohne Neuzeichnen AWT, Swing, JavaFX & SWT 3
K drawLine mit Kollisionsabfrage? AWT, Swing, JavaFX & SWT 4
A Repaint unterdrücken bei DrawLine in JPanel AWT, Swing, JavaFX & SWT 4
R drawLine gibt hässliche linie AWT, Swing, JavaFX & SWT 4

Ähnliche Java Themen

Neue Themen


Oben