Witam,
Mam problem, a mianowicie chcę wczytać zmienną obiektu, który znajduje się w tablicy "tablica". Sami zobaczcie poniżej między gwiazdkami a także na rodzaj błędu. Dodam także że zmienna "zmPomX" jest typu "public int".
Kod Java:
package pangpang;
import java.awt.Color;
public class PangPang
{
static Configuration conf;
// private Ball ball;
// private Ball ball2;
// private Ball ball3;
public Ball tablica [];
private Background bg;
PangPang()
{
conf = new Configuration();
setLevel();
}
public void startGame()
{
Paint.backgroundGraphics.setColor(Color.white);
Paint.backgroundGraphics.fillRect(0, 0, PangPang.conf.getWidthWindow(), PangPang.conf.getHighthWindow());;
bg.gameBackground();
// ball.move();
// ball2.move();
// ball3.move();
Ball z = tablica[0];
//*****************************************************************
System.out.println(z.zmPomX); //przy tablica[0].zmPomX też wywala błąd
//*****************************************************************
}
public void setLevel()
{
bg = new Background();
// ball = new Ball();
// ball2= new Ball();
// ball3= new Ball();
tablica = new Ball[3];
for(Ball x:tablica)
{
x = new Ball();
}
}
}
Błędy w Konsoli:
Konstruktor Ball wywołano razy: 0
zmPomX: 219
Konstruktor Ball wywołano razy: 1
zmPomX: 52
Konstruktor Ball wywołano razy: 2
zmPomX: 35
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at pangpang.PangPang.startGame(PangPang.java:32)
at pangpang.Paint.paint(Paint.java:91)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)