Null Pointer Exception bei Canvas-objekt-Aufruf

Status
Nicht offen für weitere Antworten.

BlueLamp

Mitglied
Hallöchen ,

Folgendes Problem :

Ich möchte ein Canvas-objekt einem Container hinzufügen , aber ich bekomme immer folgende Meldung :

Exception in thread "main" java.lang.NullPointerException
at paint_it.init_painting(paint_it.java:20)
at window.init_game(window.java:204)
at window.<init>(window.java:125)
at window.main(window.java:269)

Das ist das Canvas - objekt das in der Hauptklasse erstellt wurde und der Container :
Code:
private paint_it pt = new paint_it();
private Container cp;


Das ist die Initialisierungsfunktion vom Canvas in der Hauptklasse :
Code:
public void init_game () {
    pt.init_painting();
    pt.setBounds(0,0,800,640);
    pt.setBackground(default_color);
    cp.add(pt);
  }


Das ist die Canvas - klasse :
Code:
import java.awt.*;

public class paint_it extends Canvas {
  Graphics bufferGraphics = null;
  Image offscreen = null;
  Dimension dim = null;

  public paint_it () {
    super();
  }
  
  public void init_painting () {
    dim = getSize();
    offscreen = createImage(dim.width,dim.height);
    bufferGraphics = offscreen.getGraphics();
  }

  @Override
  public void update(Graphics g) {
    paint(g);
  }

  @Override
  public void paint(Graphics g) {

    // clear the screen
    bufferGraphics.setColor(Color.white);
    bufferGraphics.clearRect(0,0,dim.width,dim.height);

    bufferGraphics.setColor(Color.black);
    bufferGraphics.drawString("Hallo",500,500);

    // Paint the image
    g.drawImage(offscreen,0,0,this);
  }
}

ich weiß nicht weiter... bitte helfen ^^
 
Der Stack-Trace scheint nicht zur geposteten Klasse zu passen - Zeile 20 des Codes liegt nicht im Bereich von paint_it#init_painting.
 
so ich hab jetz mal ein bisschen rumprobiert, habs aber imer noch nicht hingekriegt, hier meine neue paint_it - klasse :

Code:
import java.awt.*;

/**
  * @version 1.0 vom 23.10.2007
  * @author dübdidüp
  */

public class paint_it extends Canvas {
  private Image offscreen;
  private Graphics bufferGraphics;

  public void paint_it () {
    offscreen = createImage(800,640);
    bufferGraphics = offscreen.getGraphics();
  }

  @Override
  public void update(Graphics g) {
    paint(g);
  }

  @Override
  public void paint(Graphics g) {
  
    // clear the screen
    bufferGraphics.setColor(Color.white);
    bufferGraphics.clearRect(0,0,800,600);

    bufferGraphics.setColor(Color.black);
    bufferGraphics.drawArc(300,300,100,100,0,360);

    // Paint the image
    g.drawImage(offscreen,0,0,this);
  }
}

der aufruf ist wie vorher
 
folgender fehler besteht immernoch :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at paint_it.paint(paint_it.java:26)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:301)
at java.awt.Component.dispatchEventImpl(Component.java:4486)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
 
Marco13 hat gesagt.:
Ein kleines compilierbares Beispiel wo der fehler auftritt?

da 🙂 :

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame {

  private Container cp;
  private Color default_color;
  private paint_it pt = new paint_it();

  public test(String title) {
    // Frame-Initialisierung
    super(title);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { System.exit(0); }
    });
    int frameWidth = 800;
    int frameHeight = 640;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2 ;
    setLocation(x, y);
    cp = getContentPane();
    cp.setLayout(null);

    this.init_game();
      
    setResizable(false);
    setVisible(true);
  }

  public void init_game () {
    pt.setBounds(0,0,800,640);
    pt.setBackground(default_color);
    cp.add(pt);
  }
  
  public static void main(String[] args) {
    new test("test");
  }
}
 
Schreibe statt
Code:
public void paint_it
folgendes:
Code:
public paint_it
 
Code:
import java.awt.*;

/**
  * @version 1.0 vom 23.10.2007
  * @author dübdidüp
  */

public class paint_it extends Canvas {
  private Image offscreen;
  private Graphics bufferGraphics;

  public paint_it () {

  }

  @Override
  public void update(Graphics g) {
    paint(g);
  }

  @Override
  public void paint(Graphics g) {
    if(offscreen == null) {
      offscreen = createImage(800,640);
      bufferGraphics = offscreen.getGraphics();
    }


    // clear the screen
    bufferGraphics.setColor(Color.white);
    bufferGraphics.clearRect(0,0,800,600);

    bufferGraphics.setColor(Color.black);
    bufferGraphics.drawArc(300,300,100,100,0,360);

    // Paint the image
    g.drawImage(offscreen,0,0,this);
  }
}

So funktioniert es jetzt.
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben