Hallo,
Ich habe ein Programm geschrieben welches ein Image lädt und über ein Panel bewegen lässt!
Das Image referenziere ich über eine eigene Klasse, in welcher auch x und y koord. gespeichert sind!
Nun will ich Objekte dieser Klasse via actionListener und Button erstellen!
Dabei kriege ich eine NPE und ich finde den Fehler nicht!
Dazu die Klasse Zelle:
Die methode move() ist aus dem interface Moveable überschrieben! Genauso die Methode zeichne()
Kann mir jemand mit meiner NPE helfen?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at seb.zelle.ZellenGui.paintComponent(ZellenGui.java:93)
at javax.swing.JComponent.paint(JComponent.java:1017)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5119)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1220)
at javax.swing.JComponent.paint(JComponent.java:1003)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Danke, Luk
Ich habe ein Programm geschrieben welches ein Image lädt und über ein Panel bewegen lässt!
Das Image referenziere ich über eine eigene Klasse, in welcher auch x und y koord. gespeichert sind!
Nun will ich Objekte dieser Klasse via actionListener und Button erstellen!
Dabei kriege ich eine NPE und ich finde den Fehler nicht!
Java:
package seb.zelle;
import seb.login.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class ZellenGui extends JPanel implements Runnable {
private boolean game_running;
private int speed;
private boolean stopped;
ArrayList<Zelle> list;
Zelle zelle;
public static void starten() {
new ZellenGui(450, 400);
}
public ZellenGui (int w, int h) {
this.setPreferredSize(new Dimension(w, h));
JFrame frame = new JFrame("Zelle");
game_running = true;
stopped = true;
/** GUI-INITIALISIEREN */
JPanel hauptPanel = new JPanel();
hauptPanel.setLayout(new BoxLayout(hauptPanel, BoxLayout.Y_AXIS));
JButton start_stoppButton = new JButton("Start/Stopp");
JButton erstellenButton = new JButton("Zelle erstellen");
JButton speed = new JButton("Speed up/down");
hauptPanel.add(start_stoppButton);
hauptPanel.add(erstellenButton);
hauptPanel.add(speed);
start_stoppButton.addActionListener(new Start_stoppButtonListener());
erstellenButton.addActionListener(new ErstellenListener());
speed.addActionListener(new SpeedListener());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.getContentPane().add(BorderLayout.WEST, this);
frame.getContentPane().add(BorderLayout.EAST, hauptPanel);
this.setBackground(Color.white);
//System.out.println("Kostruktor abgeschlossen");
initialisieren();
frame.setVisible(true);
//System.out.println("Initialisieren abgeschlossen");
}
private void initialisieren () {
/**
Hier wurde die Zelle vorher initialisiert!
*/
//zelle = new Zelle(200, 200, this, "F:/Info/LoginGUI.2/pics/zelle4.png", "F:/Info/LoginGUI.2/pics/hintergrund1.png");
list = new ArrayList<Zelle>();
list.add(zelle);
//repaint();
Thread t = new Thread(this);
t.start();
//System.out.println("Nach initialisieren");
}
public void run () {
try {
Thread.sleep(1000);
} catch (Exception ex) {}
goSchleife();
//System.out.println("Vor run");
//System.out.println("Nach run");
}
public void goSchleife () {
//System.out.println("Vor der Schleife");
while (game_running == true) {
while (stopped == false) {
//Move-Methoden abrufen
}
}
}
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
//System.out.println("Vorm zeichnen");
for (Drawable draw : list) {
draw.drawObjects(g);
//System.out.println("Object gezeichnet");
}
}
public ZellenGui getGui () {
return this;
}
class Start_stoppButtonListener implements ActionListener {
public void actionPerformed (ActionEvent ev) {
if (!stopped) {
stopped = true;
}
else {
stopped = false;
}
}
}
class SpeedListener implements ActionListener {
public void actionPerformed (ActionEvent ev) {
}
}
class ErstellenListener implements ActionListener {
public void actionPerformed (ActionEvent ev) {
zelle = new Zelle(200, 200, getGui(), "F:/Info/LoginGUI.2/pics/zelle4.png", "F:/Info/LoginGUI.2/pics/hintergrund1.png");
stopped = false;
}
}
}
Dazu die Klasse Zelle:
Java:
package seb.zelle;
import seb.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Zelle implements Drawable, Moveable {
ZellenGui gamePanel;
Image image;
Image image_h;
String path;
private int speed_1;
private int speed_2;
private int speed_3;
private int x;
private int y;
private int rndZahl;
private int a;
private boolean kleinerN;
private int zahl_1;
private int zahl_2;
private int zahl_3;
private int zahl_4;
private int standart;
public Zelle (int x, int y, ZellenGui z, String path, String path_h) {
this.x = x;
this.y = y;
gamePanel = z;
image = new ImageIcon(path).getImage();
image_h = new ImageIcon(path_h).getImage();
standart = 10;
speed_1 = (int) Math.sqrt(2.0)*standart;
speed_2 = (int) Math.sqrt(5.0)*standart;
speed_3 = (int) Math.sqrt(10.0)*standart;
}
public void drawObjects (Graphics g) {
g.drawImage(image_h, 0, 0, null);
g.drawImage(image, x, y, null);
}
/**Move-Methoden */
@Override
public void move() {
//Move-Methoden die ich hier weglasse!
}
@Override
public void zeichne (int i) {
gamePanel.repaint();
try {
Thread.sleep(i);
} catch (Exception ex) {
}
}
}
Die methode move() ist aus dem interface Moveable überschrieben! Genauso die Methode zeichne()
Kann mir jemand mit meiner NPE helfen?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at seb.zelle.ZellenGui.paintComponent(ZellenGui.java:93)
at javax.swing.JComponent.paint(JComponent.java:1017)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5119)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1220)
at javax.swing.JComponent.paint(JComponent.java:1003)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Danke, Luk