You are not logged in.

  • Login
Bitte besucht unser neues Forum unter https://forum.nitrado.net | Please visit our new Forum at https://forum.nitrado.net

Dear visitor, welcome to Nitrado.net Prepaid Gameserver Community-Support - Archiv. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

TekkitTeam

User / Kunde

  • "TekkitTeam" is male
  • "TekkitTeam" started this thread

Posts: 1,349

Location: /home

Thanks: 2

  • Send private message

1

Wednesday, April 30th 2014, 7:21pm

NullPointer O.o

Hallo Community

Ich bin gerade am verzweifeln ;(

Ich versuche einen BufferedImage und eine Graphic an die 2. Klasse zu übergeben.

Dabei funktioniert es so: (Habe ein Teil unwichtiges weggeschnitten)

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 BufferedImage image;
 Graphics buffer;
public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);   
		 
		 image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
		 buffer = image.getGraphics();
	}
	
	
	
     public void paint(Graphics g)
     {
    	 Ball b = new Ball(image, buffer);
    	 b.zeigeDich(40, 40, 20, Color.red, g);
     }


So funktioniert es nicht... Aber wieso? Möchte es gerne so machen, da die paint Methode in zusammenhang mit repaint(); eine Dauerschleife ist und ich nicht tausende Objekte von Ball erzeugen will.

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);   
		 
		 image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
		 buffer = image.getGraphics();
	}
	
	
		Ball b = new Ball(image, buffer);
     public void paint(Graphics g)
     {
    	 
    	 b.zeigeDich(40, 40, 20, Color.red, g);
     }


Ball.class

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Ball {
	
    
	BufferedImage image;
	Graphics buffer;
	public Ball(BufferedImage image, Graphics buffer) {
		this.image = image;
		this.buffer = buffer;
	}

	public int runde(double x){
		return (int) Math.round(x);
	}
	
	public void zeigeDich(double x, double y, int radius, Color c, Graphics g){
		buffer.setColor(c);
		buffer.fillOval(runde(x), runde(y), radius*2,radius*2);
		g.drawImage(image, 0, 0, null);
		
	}
	

	
	
}

Fehlermeldung

Spoiler Spoiler


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at main.Ball.zeigeDich(Ball.java:21)
	at main.Main.paint(Main.java:56)
	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$1100(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)


BtoBastian

Moderator

  • "BtoBastian" is male

Posts: 3,612

Occupation: Softwareentwickler

  • Send private message

2

Wednesday, April 30th 2014, 7:24pm

Was ist den Zeile 21 von Ball?
In der zeigeDich()-Methode?
Standard Standart eines Flamingos ist einbeinig.

TekkitTeam

User / Kunde

  • "TekkitTeam" is male
  • "TekkitTeam" started this thread

Posts: 1,349

Location: /home

Thanks: 2

  • Send private message

3

Wednesday, April 30th 2014, 7:26pm

Java source code

1
buffer.setColor(c);


buffer bzw. image sind null bzw. werdem gar nicht richtig übergeben: Wenn ich es mit System.out.println() ausgebe kommt auch null. Deswegen kann es an der ball.class eig nicht liegen :)

BtoBastian

Moderator

  • "BtoBastian" is male

Posts: 3,612

Occupation: Softwareentwickler

  • Send private message

4

Wednesday, April 30th 2014, 7:29pm

Initialisier das Ball-Attribut b mal erst im Konstruktor und nicht außerhalb eines Methoden-Rumpfs.
Standard Standart eines Flamingos ist einbeinig.

TekkitTeam

User / Kunde

  • "TekkitTeam" is male
  • "TekkitTeam" started this thread

Posts: 1,349

Location: /home

Thanks: 2

  • Send private message

5

Wednesday, April 30th 2014, 7:30pm

@BtoBastian

Aber dann kann ich ja nicht auf b zugreifen, da es in einer anderen Methode ist :)

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);   
		
		image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
		buffer = image.getGraphics();

		Ball b = new Ball(image, buffer);
	}


Edit:// klappt jetzt :D War nur zu doof :) Muss b ja nur initialisieren und nicht erzeugen :D

BtoBastian

Moderator

  • "BtoBastian" is male

Posts: 3,612

Occupation: Softwareentwickler

  • Send private message

6

Wednesday, April 30th 2014, 7:34pm

Definieren kannst du es außerhalb, aber initialisieren nicht.

Java source code

1
2
3
4
5
6
7
Ball b;
public Main() {
  // blabla
  image = new // usw.
  buffer = image.getGraphics();
  b = new Ball(image, buffer);
}

image und buffer unbedingt zuvor initialisieren!
Standard Standart eines Flamingos ist einbeinig.

Bitte besucht unser neues Forum unter https://forum.nitrado.net| Please visit our new Forum at https://forum.nitrado.net