NullpointerException beim Einlesen einer Datei

Status
Nicht offen für weitere Antworten.

Fridolin

Bekanntes Mitglied
Ich hoffe wieder auf eure Hilfe, ich habe heute ein bisschen programmiert
und eine kleine version von pacman ohne kollisionserkennung und ki gemacht(nicht nur heute dazu hab ich etwas länger gebraucht) Es ist schon alles gegangen, die schlieren waren auch weg. Ich hab mit ImageIO.read(File bild) immer von einer datei ausgelesen und dann auf deren BufferedImage das ImageIO.read zurückgibt die Bewegungsänderung der Figur gezeichnet und mit drawImage alles angezeigt. Ja, jetzt fragt ihr euch sicher was will
dieser trottl? :roll: Dann hab ich die datei mit dem bild gelöscht da sie einen abgeschnittenen rahmen hatte und eine
neue angelegt mit demselben namen ohne etwas im programm zu verändern und jetzt kommt eine nullpointer Exception an dieser stelle das ist die stelle in der update methode in der ich das bild lade und anzeige(zumindest ist es schonmal so gegangen) Das geschieht in einem Thread alle 20ms es kann sein das ich noch was anderes verändert habe als ich den jcreator geschlossen habe ohne zu speichern aber ich glaube das macht er eh automatisch

vielleicht könnt ihr mir helfen lg Fridolin

Code:
                                                try
			{
			File mappe = new File("C:\\Programme\\Xinox Software\\JCreator LE\\MyProjects\\VersuchPacman\\Readme\\Mappe.jpg");
			BufferedImage mappeanzeigen = ImageIO.read(mappe);
Graphics2D anzeigen = mappeanzeigen.createGraphics();
<<< Zeile 361 >>>
Code:
                                                if(!(xspeed == 0 & yspeed == 0))
			{
			
				if(winkel == 260)
				{
				weite = 4;
				}
			
				else if(winkel == 320)
				{
				weite = -4;
				}
				
				winkel+=weite;
				boardx2+=xspeed;
				boardy2+=yspeed;
			}
			anzeigen.setColor(Color.yellow);
			anzeigen.fillArc(boardx2,boardy2 + 4, 7, 7, 0, -winkel);
			g.drawImage(mappeanzeigen,30,30,this);
			}
			
			catch(IOException e)
			{
			e.printStackTrace();
			}
 
Hmm eine NullPointerException kann dabei eigetnlich garnicht auftreten (was das Laden des Files betrifft, weiter unten geht das). Denn dem Filekonstruktor ist egal was man ihm einfüttert solange es nicht null ist, der macht einfach. ImageIO liefert bei einem nul File eine IllegalArgumentException. createGraphics liefert garkeine Exception.

Also selbst mit bestem willen kann man dir ohne Stack Trace nicht wirklich helfen.
 
Ja gerne wenn ihr damit was anfangen könnt:
wisst ihr eigentlich warum unten 3477 in einer Zeile steht soviele Zeilen hat mein programm doch gar nicht
Mir ist gerade eingefallen das ich dasselbe schon einmal gehabt habe als ich ein BufferedImage mit ImageIO.write(BufferedImage,String format, File ziel) schreiben wollte dann hab ich einfach versucht nichts mehr in eine datei zu schreiben
http://www.java-forum.org/de/viewto...t=&sid=84b67806d337ab532d5be76f2329f5e0#32569

lg Fridolin

java.lang.NullPointerException
at VersuchPacman.update(VersuchPacman.java:361)
at sun.awt.RepaintArea.paint(RepaintArea.java:169)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
 
Hast du den Stacktrace eigentlich schon begriffen Fridolin?

Der Stacktrace zeigt die Methoden, die Klassen und die Zeilennummern an die zu dem Fehler geführt haben. Die Zeile "at java.awt.Component.dispatchEvent(Component.java:3477) " bedeutet also nichts weiter als das im der Klasse Component.java in Zeile 3477 in der Methode dispatchEvents eine weitere Methode aufgerufen wurde die dann später zum Fehler führte. So eine Art Wegbeschreibung wie es zu dem Fehler gekommen ist. Das dein Programm keine 3477 Zeilen hat ist dabei ziemlich egal weil es ja um Component.java geht. Da der Fehler in "at VersuchPacman.update(VersuchPacman.java:361" auftritt. also in der Klasse VersuchPacman in Zeile 361 in der Methode update wäre es sehr nett wenn du diesen Code noch posten würdest. Am besten mit einem Marker welches denn die Zeile 361 ist.
 
Das ist der von weiter oben erstes posting
Ich hab schon gewusst das es verschiedene klassen sind die zu folgefehlern führen
und das die erste meine ist (vielleicht die zweite, dritte auch noch) vielleicht aber auch nicht

Roar ich hab mir deinen beitrag durchgelesen und bis auf drei fehler hab ich davon schon alle gehabt 8)

Wisst ihr jetzt was es sein könnte?
lg Fridolin
 
Ihr wollt die ganze klasse ich hab nur die update methode hingeschrieben hier die klasse
Das selbstzeichnen hat eh keinen sinn mehr da ich jetzt von einer datei laden will
Vorher hab ich versucht alles in dem String Array board einzulesen und dann jedes mal auf das
private Graphics dbg zu zeichnen synchron dazu hab ich noch versucht ein BufferedImage zu machen um auf
dessen Graphics2D hallo zu zeichnen welches ich dann in eine Datei speichern wollte zum anzeigen hab ich aber immer nur das Graphics dbg verwendet das andere ist nie gegangen weil die if abfrage nie betreten wurde und es schließlich beim neuzeichnen repaint im thread oben immer schlieren gezogen hat die auf dem Graphics am bildschirm zu sehen waren beim ersten repaint sollte die gezeichnete mappe mit dem bild aus der datei
ersetzt werden

Ich glaube ich habe wirklich nichts verändert gestern ist es schon gegangen
zweimal dann hab ich die datei mappe.jpg gelöscht und eine neue im paint gemacht
das programm wieder gestartet und es ist nichts mehr gelaufen

Fridolin

Code:
import java.applet.*;
import java.awt.event.*;
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.lang.Character;
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.StreamTokenizer; 

import javax.swing.JFrame;
import javax.imageio.ImageIO;

public class VersuchPacman extends JFrame
{
	private Graphics dbg;
	private Graphics2D hallo;
	StreamTokenizer tokenizer;
	BufferedImage anzeige;
	BufferedReader in;
	String board[][] = new String[19][42]; 
	int count1 = 0;
	int count2 = 0;
	int boardx = 30;
	int boardy = 30;
	int blocksizex = 7; 
	int blocksizey = 15; 
	int token;
	int winkel = 260;
	int weite = 0;
	int i = 1;
	int nflag = 0;
	int zflag = 0;
	int xspeed = 0;
	int yspeed = 0;
	int boardx2;
	int boardy2;
	
public VersuchPacman()
{
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	this.setFocusable(true);
	this.requestFocusInWindow();
	this.addKeyListener(new KeyAdapter()
	{
		public void keyPressed(KeyEvent e)
		{
			int key = e.getKeyCode();
			if(key == KeyEvent.VK_LEFT)
			{
			xspeed = -1;
			yspeed = 0;
			}
			
			else if(key == KeyEvent.VK_RIGHT)
			{
			xspeed = 1;
			yspeed = 0;
			}
			
			else if(key == KeyEvent.VK_UP)
			{
			xspeed = 0;
			yspeed = -1;
			}
			
			else if(key == KeyEvent.VK_DOWN)
			{
			xspeed = 0;
			yspeed = 1;
			}
			
			if(key == KeyEvent.VK_SPACE)
			{
			xspeed = 0;
			yspeed = 0;
			}
		}
	});
	
	new Thread()
	{
		public void run()
		{
			while(true)
			{
			repaint();
			try
				{
				Thread.sleep(10);
				}
				catch(InterruptedException e) { }
			}
		}
	}.start();
}

public void loadAndPaint()
{
	     try
		    {
			in = new BufferedReader(new FileReader(new File(
			"C:\\Programme\\Xinox Software\\JCreator LE\\MyProjects\\VersuchPacman\\Readme\\Kopie von Mappe.txt"))); 
			tokenizer = new StreamTokenizer(in); 
			tokenizer.wordChars('c', 'c');
			tokenizer.wordChars('w', 'w');
			tokenizer.wordChars('n', 'n');
			tokenizer.wordChars('s', 's');
			tokenizer.wordChars('e', 'e');
			tokenizer.whitespaceChars(' ',' ');
        	tokenizer.eolIsSignificant(true);
        	tokenizer.slashStarComments(true);
        	tokenizer.slashSlashComments(true);
        	
while ((token = tokenizer.nextToken()) != StreamTokenizer.TT_EOF)
{

switch (token)
{
      case StreamTokenizer.TT_WORD:	
            board[count1][count2] = tokenizer.sval;
            //System.out.println(""+board[count1][count2]+""+i);
            //i++;
            count2++;
            break;

      case StreamTokenizer.TT_EOL:
            count1++;
            count2 = 0;
            break;
            
      case StreamTokenizer.TT_EOF:
            count1 = 0;
            count2 = 0;
            nflag = 6;
            break;
}

}
       
 for(count1 = 0; count1 <= 18; count1++)
 {
 	
 if(count1 != 0)
 {
 boardy+=blocksizey;
 boardx = 30;
 }

	for(count2 = 0; count2 <= 41; count2++)
	{

	if(count1 == 19 & count2 == 42)
	{
	zflag = 3;
	}
				
  	if(board[count1][count2].equals("n"))
  	{
  	
  	if(dbg == null)
    {
  	dbg = getGraphics();
  	}
  	dbg.setColor(Color.blue);
  	nflag = 0;
  	}
  			
  	else if(board[count1][count2].equals("w"))
  	{
  	dbg.setColor(Color.black);
    nflag = 5;		
    } 
            	
    else if(board[count1][count2].equals("c"))
  	{
  	dbg.setColor(Color.blue);
	nflag = 3;		
    }
            	
    else if(board[count1][count2].equals("s"))
  	{
  	dbg.setColor(Color.blue);	
    nflag = 2;
    }
            	
    else if(board[count1][count2].equals("e"))
  	{
  	dbg.setColor(Color.blue);	
    nflag = 1;
    }
            	
    else if(board[count1][count2].equals("g"))
    {
    dbg.setColor(Color.blue);	
    nflag = 4;
    }
    
    paint(dbg);	
    boardx+=blocksizex;	
		    }	
		}
	} 
catch (IOException e) { }
}

	public void paint(Graphics g)
	{
		
		/*if(nflag == 6 & zflag == 3)
		{
			
			try
			{
			ImageIO.write(anzeige, "jpg", new File("C:\\Programme\\Xinox Software\\JCreator LE\\MyProjects\\VersuchPacman\\Readme\\Anzeige.jpg"));	
			}
			
			catch(IOException event)
			{
			event.printStackTrace();
			}
			
			nflag = 10;	
		}*/

		if(nflag == 0)
		{
			if(anzeige == null)
  			{
  			anzeige = new BufferedImage(356, 325, BufferedImage.TYPE_INT_ARGB);	
  			hallo = anzeige.createGraphics();
  			hallo.setColor(Color.blue); 
  			}
		hallo.drawRect(boardx,boardy,blocksizex,blocksizey);
		hallo.fillRect(boardx,boardy,blocksizex,blocksizey);
		g.drawRect(boardx,boardy,blocksizex,blocksizey);
		g.fillRect(boardx,boardy,blocksizex,blocksizey);
		}
		
		else if(nflag == 1)
		{
		hallo.setColor(Color.blue);
		hallo.drawRect(boardx,boardy,blocksizex,blocksizey);
		hallo.fillRect(boardx,boardy,blocksizex,blocksizey);
		hallo.setColor(Color.red);
		hallo.drawArc(boardx,boardy + 4, 7, 7, 0, -340);
		g.drawRect(boardx,boardy,blocksizex,blocksizey);
		g.fillRect(boardx,boardy,blocksizex,blocksizey);
		g.setColor(Color.red);
		g.drawArc(boardx,boardy + 4, 7, 7, 0, -340);
		}
		
		else if(nflag == 2)
		{
		hallo.setColor(Color.blue);
		hallo.drawRect(boardx,boardy,blocksizex,blocksizey);
		hallo.fillRect(boardx,boardy,blocksizex,blocksizey);
		hallo.setColor(Color.pink);
	                hallo.drawOval(boardx + 1,boardy + 3,blocksizex - 2,blocksizey - 6);
		hallo.fillOval(boardx + 1,boardy + 3,blocksizex - 2,blocksizey - 6);
		g.drawRect(boardx,boardy,blocksizex,blocksizey);
		g.fillRect(boardx,boardy,blocksizex,blocksizey);
		g.setColor(Color.pink);
	                g.drawOval(boardx + 1,boardy + 3,blocksizex - 2,blocksizey - 6);
		g.fillOval(boardx + 1,boardy + 3,blocksizex - 2,blocksizey - 6);
		}
		
		else if(nflag == 3)
		{
		hallo.setColor(Color.blue);
		hallo.drawRect(boardx,boardy,blocksizex,blocksizey);
		hallo.fillRect(boardx,boardy,blocksizex,blocksizey);
		hallo.setColor(Color.white);
	                hallo.drawOval(boardx + 1,boardy + 4,blocksizex - 2,blocksizey - 8);
		hallo.fillOval(boardx + 1,boardy + 4,blocksizex - 2,blocksizey - 8);
		g.drawRect(boardx,boardy,blocksizex,blocksizey);
		g.fillRect(boardx,boardy,blocksizex,blocksizey);
		g.setColor(Color.white);
	                g.drawOval(boardx + 1,boardy + 4,blocksizex - 2,blocksizey - 8);
		g.fillOval(boardx + 1,boardy + 4,blocksizex - 2,blocksizey - 8);
		}
		
		else if(nflag == 4)
		{
		hallo.setColor(Color.blue);
		hallo.drawRect(boardx,boardy,blocksizex,blocksizey);
		hallo.fillRect(boardx,boardy,blocksizex,blocksizey);
		hallo.setColor(Color.yellow);
		hallo.drawArc(boardx,boardy + 4, 7, 7, 0, -340);
		hallo.fillArc(boardx,boardy + 4, 7, 7, 0, -340);
		g.drawRect(boardx,boardy,blocksizex,blocksizey);
		g.fillRect(boardx,boardy,blocksizex,blocksizey);
		g.setColor(Color.yellow);
		g.drawArc(boardx,boardy + 4, 7, 7, 0, -340);
		boardx2 = boardx;
		boardy2 = boardy;
		g.fillArc(boardx,boardy + 4, 7, 7, 0, -340);
		}	
		
		else if(nflag == 5)
		{
		hallo.setColor(Color.black);
		hallo.drawRect(boardx,boardy,blocksizex,blocksizey);
		hallo.fillRect(boardx,boardy,blocksizex,blocksizey);
		g.drawRect(boardx,boardy,blocksizex,blocksizey);
		g.fillRect(boardx,boardy,blocksizex,blocksizey);
		}			
	}
	
	public void update(Graphics g)
	{
		/*if(nflag == 10)
		{
		g.drawImage(anzeige,30,30,this);
		boardx2+=xspeed;
		boardy2+=yspeed;
		g.setColor(Color.yellow);
		g.drawArc(boardx2,boardy2 + 4, 7, 7, 0, -340);
		g.fillArc(boardx2,boardy2 + 4, 7, 7, 0, -340);
                                //nflag = 6;	
		}*/
		
		//else
		//{
			try
			{
			File mappe = new File("C:\\Programme\\Xinox Software\\JCreator LE\\MyProjects\\VersuchPacman\\Readme\\Mappe.jpg");
			BufferedImage mappeanzeigen = ImageIO.read(mappe);
Graphics2D anzeigen = mappeanzeigen.createGraphics();
<<< Zeile 361 >>>
Code:
			if(!(xspeed == 0 & yspeed == 0))
			{
			
				if(winkel == 260)
				{
				weite = 4;
				}
			
				else if(winkel == 320)
				{
				weite = -4;
				}
				
				winkel+=weite;
				boardx2+=xspeed;
				boardy2+=yspeed;
			}
			anzeigen.setColor(Color.yellow);
			anzeigen.fillArc(boardx2,boardy2 + 4, 7, 7, 0, -winkel);
			g.drawImage(mappeanzeigen,30,30,this);
			}
			
			catch(IOException event2)
			{
			event2.printStackTrace();
			}
		}
	//}
	public static void main(String args[])
	{
	VersuchPacman neu = new VersuchPacman();
	neu.setSize(356,325);
	neu.setLocation(100,100);
	neu.setVisible(true);
	neu.loadAndPaint();
	} 
}
 
Das hab ich jetzt zwar nicht verstanden aber wegen der textdatei kannst du einfach ein paar n n w w eingeben oder
du nimmst die hier

Code:
n s n n c n w w w n c n n c n n c n n c n n c n w w w n c n n c n n c n w w w n s n
w w w n c n w w w n c n w w w n c n w w w n c n w w w n c n w w w n c n w w w n c n
w w w n c n w w w n c n n c n n c n w w w n c n w w w n c n w w w n c n w w w n c n
n c n n c n n c n n c n w w w n c n n c n n c n n c n n c n n c n n c n n c n n c n
n c n w w w w w w n c n w w w n c n w w w n c n w w w w w w w w w n c n w w w n c n
n c n w w w w w w n c n w w w n c n w w w n c n n n n n e n n n n n c n w w w n c n
n c n w w w w w w n c n w w w n c n w w w n c n w w w n e n w w w n c n w w w n c n
n c n n c n n c n n c n w w w n c n n c n n c n w w w n e n w w w n c n n c n n c n
n c n w w w w w w n c n w w w n c n w w w n c n w w w w w w w w w n c n w w w n c n
n c n w w w w w w n c n w w w n c n n c n n c n n c n w w w n c n n c n w w w n c n
n c n n c n n c n n c n w w w w w w w w w w w w w w w w w w n c n w w w n c n n c n
n c n w w w w w w n c n w w w n c n n c n n g n n c n n c n n c n w w w n c n w w w
n c n n c n n c n n c n n c n n c n w w w n c n w w w w w w n c n w w w n c n w w w
w w w n c n w w w n c n w w w n c n w w w n c n n c n w w w n c n n c n n c n w w w
w w w n c n w w w n c n w w w n c n w w w w w w n c n w w w n c n w w w n c n w w w
w w w n c n w w w n c n w w w n c n w w w n c n n c n n c n n c n n c n n c n w w w
w w w n c n n c n n c n w w w n c n n c n n c n w w w w w w w w w w w w w w w n c n
w w w n c n w w w n c n w w w n c n w w w n c n n c n n c n n c n n c n n c n n c n
n s n n c n n c n n c n w w w n c n n c n n c n w w w w w w w w w w w w w w w n s n

du kannst auch den kompletten teil mit dem streamtokenizer und der abfrage mit dem board und dem paint das darin aufgerufen wird streichen ich glaub das ist in der methode loadAndPaint und nur versuchen ein Bild von einer Datei zu laden
das hintergrundbild ist doch fürs testen eigentlich egal es muss nur ein jpg bild mit 300 x 291 auflösung sein
und noch was wer es ausprobieren will muss die mappe so umgestalten das sie genau 19 zeilen mit jeweils 42 zeichen und nach jedem zeichen einen abstand
hat
 
Wie ich bereits sagte, aber vieleicht ignorierst du mich auch ;-) Wenn ich nicht weiss welches Zeile 361 kann ich dir nicht helfen. Ich habe deinen Code mal kopiert und bin in Zeile 361 gegangen, dann lande ich hier:

Code:
         g.drawImage(mappeanzeigen,30,30,this); 
         } 
          
         catch(IOException event2) 
         { 
         event2.printStackTrace(); 
         } 
      }   <- Zeile 361 
   //}
 
Warte kurz ich habe aus meinem code viele kommentare rausgenommen damit er schön leserlich ist
aber ich hab dir jetzt zeile 361 markiert für die Mappe.jpg nimmst du irgendein Bild mit der Auflösung 300 x 291
und für die Kopie von Mappe.txt ein Textfeld mit 19 Zeilen x 42 Zeichen und nach jedem Zeichen einen Abstand dazwischen machen
Fridolin
 
Code:
File mappe = new File"C:\\Programme\\Xinox\\Software\\JCreator\\MyProjects\\VersuchPacman\\Readme\\Mappe.jpg"); 
BufferedImage mappeanzeigen = ImageIO.read(mappe); 
Graphics2D anzeigen = mappeanzeigen.createGraphics();

Wenn Graphics2D anzeigen = mappeanzeigen.createGraphics(); eine NullPointerException liefert dann ist mappeanzeigen wohl null. Also würde ich mir mal mappe anschauen. Füg doch da mal ein mappe.canRead ein. Das mit der Gross und Kleinschreibung des Files wie von Beni hast du ja sicher schon berücksichtigt.
 
Hallo,
Die datei kann man lesen, ich hab schon probiert ob ich nicht mit Graphics neu = alt.create()
ein Objekt kopiere und dann jedesmal lösche und wieder kopiere so damit die änderungen am bild nicht sichtbar sind
Aber wenn du noch was weißt, kannst du es gerne sagen

Fridolin
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben