public void - Problem

Toastbot

Mitglied
Hallo liebes Forum, da ihr mir bis jetzt immer so super helfen konntet, dachte ich ich frag einfach nochmal 🙂
Ich habe in meinem Script ein "public void wand" erstellt damit ich ohne immer 30 Zeilen schreiben zu müssen wände erstellen kann.
Das Problem ist: Die Konsole gibt nur Fehlermeldungen raus.

Es liegt an der aufforderung "wand();"
jetzt meine Frage: Wie muss ich es richtig machen?
Liebe Grüße!
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class Move {

	private JFrame frame;
	int time;
	int x;
	int y;
	int raumx=250;
	int raumy=250;
	int schrit=25;
	int portx;
	int porty;
	int newport=0;
	int score=0;
	int move=0;
	int speed=0;
	int wall1x=3;
	int wall1y=4;
	int wall2x=3;
	int wall2y=3;
	int wall3x=2;
	int wall3y=3;
	int stopl=0;
	int stopr=0;
	int stopu=0;
	int stopd=0;
	Graphics2D g2d;

	public Move() {
		frame = new JFrame("Catch the ball!");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationByPlatform(true);
		MovePanel panel = new MovePanel();
		frame.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_UP) {
					if(move==0&&stopu==0){
						y-=1;
						move=1;
					}
					stopd=0;
					stopl=0;
					stopr=0;
				} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
					if(move==0&&stopd==0){
						y+=1;
						move=1;
					}
					stopu=0;
					stopl=0;
					stopr=0;
				} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
					if(move==0&&stopl==0){
						x-=1;
						move=1;
					}
					stopd=0;
					stopu=0;
					stopr=0;
				} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
					if(move==0&&stopr==0){
						x+=1;
						move=1;
					}
					stopd=0;
					stopl=0;
					stopu=0;
				}
				else if (e.getKeyCode() == KeyEvent.VK_X) {
					if(speed==0){
						speed=1;
					} else if(speed==1){
						speed=0;
					}
				}
				if (x==raumx/schrit){
					x=raumx/schrit-1;
				}
				if (y==raumy/schrit){
					y=raumy/schrit-1;
				}
				if (x==-1){
					x=0;
				}
				if (y==-1){
					y=0;
				}

				frame.repaint();
			}
		});
		frame.add(panel);

		Timer look = new Timer(100, panel);
		look.setRepeats(true);
		look.start();

		frame.pack();
		frame.setVisible(true);

	}
	public static void main(String[] args) {
		new Move();
	}

	class MovePanel extends JPanel implements ActionListener {
		private static final long serialVersionUID = 1L;

		private int counter;

		public MovePanel() {
			super();
			counter = 0;
		}


		public Dimension getPreferredSize() {
			return new Dimension(raumx, raumy);
		}
		
		public void wand(int wallx, int wally){
			if (wallx==x-1 && wally==y){
				stopl=1;
			}
			if (wallx==x+1 && wally==y){
				stopr=1;
			} 
			if (wallx==x && wally==y-1){
				stopu=1;
			} 
			if (wallx==x && wally==y+1){
				stopd=1;
			}
			if (wallx==portx && wally==porty){
				newport=0;
			}
			g2d.setColor(new Color(Integer.parseInt("000000", 16)));
			g2d.fillRect(wallx*schrit, wally*schrit, schrit, schrit);
		}
		
		public void paintComponent(Graphics g) {


			int boxX=schrit*x;
			int boxY=schrit*y;
			int boxU=schrit;
			int maxX=raumx/25-1;
			int maxY=raumy/25-1;

			Graphics2D g2d = (Graphics2D) g;
			g2d.setColor(new Color(Integer.parseInt("7CFC00", 16)));
			g2d.fillRect(0, 0, this.getWidth(), this.getHeight());

			if (newport==0){
				portx=(int)(Math.random()*maxX);
				porty=(int)(Math.random()*maxY);
				newport=1;
			} else if (portx==x && porty==y){
				newport=0;
				score+=1;
				System.out.println(score);
				repaint();
			}
			
			wand(2, 3);
			
			//Eine Wand
			if (wall1x==x-1 && wall1y==y){
				stopl=1;
			}
			if (wall1x==x+1 && wall1y==y){
				stopr=1;
			} 
			if (wall1x==x && wall1y==y-1){
				stopu=1;
			} 
			if (wall1x==x && wall1y==y+1){
				stopd=1;
			}
			if (wall1x==portx && wall1y==porty){
				newport=0;
				repaint();
			}
			g2d.setColor(new Color(Integer.parseInt("000000", 16)));
			g2d.fillRect(wall1x*schrit, wall1y*schrit, schrit, schrit);
			//Eine Wand
			if (wall2x==x-1 && wall2y==y){
				stopl=1;
			}
			if (wall2x==x+1 && wall2y==y){
				stopr=1;
			}
			if (wall2x==x && wall2y==y-1){
				stopu=1;
			}
			if (wall2x==x && wall2y==y+1){
				stopd=1;
			}
			if (wall2x==portx && wall2y==porty){
				newport=0;
				repaint();
			}
			g2d.setColor(new Color(Integer.parseInt("000000", 16)));
			g2d.fillRect(wall2x*schrit, wall2y*schrit, schrit, schrit);
			//Eine Wand
			if (wall3x==x-1 && wall3y==y){
				stopl=1;
			}
			if (wall3x==x+1 && wall3y==y){
				stopr=1;
			}
			if (wall3x==x && wall3y==y-1){
				stopu=1;
			}
			if (wall3x==x && wall3y==y+1){
				stopd=1;
			}
			if (wall3x==portx && wall3y==porty){
				newport=0;
				repaint();
			}
			g2d.setColor(new Color(Integer.parseInt("000000", 16)));
			g2d.fillRect(wall3x*schrit, wall3y*schrit, schrit, schrit);
			//Eine Wand
			
			
			

			if(move==0){
				g2d.setColor(new Color(Integer.parseInt("FFB90F", 16)));
			} else if (move==1){
				g2d.setColor(new Color(Integer.parseInt("D60000", 16)));
			}
			g2d.fillRect(boxX, boxY, boxU, boxU);
			g2d.setColor(new Color(Integer.parseInt("000000", 16)));
			g2d.fillOval(portx*25, porty*25, schrit, schrit);

			

		}

		public void actionPerformed(ActionEvent e) {
			setState(counter);
			counter = (counter % 10) + 1;
		}

		private void setState(int i) {
			if(move==1){
				if(i %2 == 1) {
					time+=1;
				}
				else {
					time+=1;
				}
			}
			if(time == 3&& speed==1){
				time=0;
				move=0;
			}
			if(time == 5&& speed==0){
				time=0;
				move=0;
			}
			repaint();
		}
	}
}

z.135 mein "public void"
z.179 mein "wand();"
Darunter 3 Wände per "Hand" erstellt, welche auch 1A funktionieren.
 
Zuletzt bearbeitet:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Move$MovePanel.wand(Move.java:151)
at Move$MovePanel.paintComponent(Move.java:179)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.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)



-Das wieder holt sich da etwa 1000 mal-

Und nur das "//wand(2, 3)" reicht um die Fehlermeldung NICHT zu haben!
 
Zuletzt bearbeitet:
Wie du aus der Fehlermeldung herauslesen kannst tritt in der Klasse Move in Zeile 151 eine NullPointerException auf.
Diese kommt zustande, wenn auf ein Objekt, das nicht initialisiert wurde (oder null ist) eine Methode aufgerufen wird.

Bei dir ist das das g2d Objekt.
Dieses wurde nirgends gesetzt.

Damit du die Membervariable auch setzt, müsstest du in Zeile 164 folgendes schreiben.

Java:
          g2d = (Graphics2D) g;
 
Zuletzt bearbeitet:
Erklärt sich von ganz alleine.

Deine globale Variable g2d wird nie gesetzt. Schöner ist es, wenn man Graphics Objekte nicht global gestaltet sondern der Methode überreicht.

1. Wirf die globale g2d weg

2. Füge den Parameter g2d in deine Methode wand ein:

Java:
public void wand(int wallx, int wally, Graphics2D g2d){ ... }

3. Beim aufruf natürlich übergeben.

Java:
wand(2, 3, g2d);
 
Hat funktioniert!
Das erleichter das Wände stellen ungemein ^^,

Wo ich grad dabei bin, eine weiter kleine Frage:
Ich gebe derzeitig den Score nur in der Console aus, kann ich den Text der Console auch an einer bestimmten Position auf meinem Bild ausgeben?

L.G. 😀
 
Wenn du es händisch zeichnest kannst du es mit
Code:
g.drawString(...)
machen.
 
Also ich hab dir das schon einmal in den anderen Topic gepostet und es steht sogar in den Forumregeln drinnen. Benutze Google -> erster Treffer und schon hättest du es: Suche
 

Zurück
Oben