I am trying to read a custom file extension When reading a string from the file it output the correct string, however when I attempt to read an int it does not output the correct int. Here is my code:

import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.*;


public class TestRead {

	public TestRead () throws IOException{
		JFrame f = new JFrame ("Test Read From Custom File Extention");
		JPanel p = new JPanel ();
		int inFile = 0;
		String file = "";
		BufferedReader stream;
		try {
			stream = new BufferedReader (new FileReader("C:\\Users\\Adam\\Desktop\\TestFile.wpf"));
			inFile = stream.read();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		JLabel text = new JLabel ("" + inFile);
		
		
		
		
		p.add (text);
		f.add(p);
		f.setSize (200,200);
		f.setVisible (true);
	}
	
	
	public static void main (String [] args) throws IOException{
		TestRead tr = new TestRead();
	}
	
}

How would I solve this issue?

Thanks for the help.

Recommended Answers

All 10 Replies

how do you expect us to know this?
what's the contents of the file, what do you expect to get, what are you getting, do you get error messages, ...

just skimmed your code ...
I think I know what your problem is:

change

int inFile = 0;

into

String inFile = "";

and

inFile = stream.read();

into

inFile = stream.readLine();

if this solves your problem, you might want to check here for the differences between the two. It's midnight here, so I 'm not going to go to deep into it for now, but I think you'll be able to get it on your own when you check the api's.

Did you read the API doc for the read method?
What does it return? Is it a String or something else?
What did you see in the JLabel?
Try printing out the value of inFile as an int.
Then print it casting it to a char and see what you get.

Try this:
System.out.println("a=" + (char)97);

The contents of the file is a "1" (one) and when I read the file I am expecting to see a 1. I am getting a 49. No error messages.

The (char) worked, but is there a way to make it so that it automatically converts?

Use another method to read the data. One that will convert what is read to a String.
Read the API doc for BufferedReader.
Or use another class with methods that do what you want.

ok thanks. I have this other error when trying to print rectangles on a screen with a for loop. Here is my code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class DropG extends JPanel implements KeyListener, ActionListener{

	int x = 290,y = 10; // frame 600 Wide, 400 Tall
	boolean onPlatform = false;
	int [] pX1, pX2 = new int [10];
	int [] pY = new int [10];
	int [] pWidth1, pWidth2 = new int [10];
	int velocity = 3, gravity = 5;
	Timer timer = new Timer (50, this);
	
	public void game (){
		StartPlatformGen();
		//timer.start();
		grabFocus();
		addKeyListener (this);
		
	}
	
	

	public void paintComponent (Graphics g){
		super.paintComponent(g);
		g.setColor (Color.red);
		g.fillRect(x, y, 20, 20);
		g.setColor (Color.green);
		for (int i = 0; i < 10; i ++){
			g.fillRect(pX1[i], pY[i], pWidth1[i], 10);
			g.fillRect(pX2[i], pY[i], pWidth2[i], 10);
		}
		
	}
		

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++){ // check if platform is off of the screen
			if (((pY[i])+ 10) <= 0){
				platformGen (i);
				
			}
		}
		
		if (onPlatform == false){
			y += gravity;
		}
	}

	private void platformGen(int i) {
		// TODO Auto-generated method stub
		int rand = (int)(Math.random () * 7) + 1; // generates a number between 1 and 7;
		/*
		 * Generates the platform that went off the screen
		 * */
		
		
		if (rand == 1){
			pWidth1[i] = 240;
			pWidth2[i] = 280;
			pX1[i] = 0;
			pX2[i] = (600 - 280);
			pY[i] = 400;
		}
		else if (rand == 2){
			pWidth1[i] = 260;
			pWidth2[i] = 260;
			pX1[i] = (600 - 260 - 260);
			pX2[i] = (600 - 260);
			pY[i] = 400;
		}
		else if (rand == 3){
			pWidth1[i] = 260;
			pWidth2[i] = 260;
			pX1[i] = 0;
			pX2[i] = (0 + 260);
			pY[i] = 400;
		}
		else if (rand == 4){
			pWidth1[i] = 360;
			pWidth2[i] = 160;
			pX1[i] = 0;
			pX2[i] = (600 - 160);
			pY[i] = 400;
		}
		else if (rand == 5){
			pWidth1[i] = 160;
			pWidth2[i] = 360;
			pX1[i] = 0;
			pX2[i] = (600 - 360);
			pY[i] = 400;
		}
		else if (rand == 6){
			pWidth1[i] = 40;
			pWidth2[i] = 480;
			pX1[i] = 0;
			pX2[i] = (600 - 480);
			pY[i] = 400;
		}
		else if (rand == 7){
			pWidth1[i] = 480;
			pWidth2[i] = 40;
			pX1[i] = 0;
			pX2[i] = (600 - 40);
			pY[i] = 400;
		}
		
	}
	
	private void StartPlatformGen() {
		// TODO Auto-generated method stub
		int platform = -40;
		for (int i = 0; i < 10; i ++){
			int rand = (int)(Math.random () * 7) + 1; // generates a number between 1 and 7;
			/*
			 * Generates the starting platforms.
			 * */
			platform += 40;
			
			if (rand == 1){
				pWidth1[i] = 240;
				pWidth2[i] = 280;
				pX1[i] = 0;
				pX2[i] = (600 - 280);
				pY[i] = 400 + platform;
			}
			else if (rand == 2){
				pWidth1[i] = 260;
				pWidth2[i] = 260;
				pX1[i] = (600 - 260 - 260);
				pX2[i] = (600 - 260);
				pY[i] = 400 + platform;
			}
			else if (rand == 3){
				pWidth1[i] = 260;
				pWidth2[i] = 260;
				pX1[i] = 0;
				pX2[i] = (0 + 260);
				pY[i] = 400 + platform;
			}
			else if (rand == 4){
				pWidth1[i] = 360;
				pWidth2[i] = 160;
				pX1[i] = 0;
				pX2[i] = (600 - 160);
				pY[i] = 400 + platform;
			}
			else if (rand == 5){
				pWidth1[i] = 160;
				pWidth2[i] = 360;
				pX1[i] = 0;
				pX2[i] = (600 - 360);
				pY[i] = 400 + platform;
			}
			else if (rand == 6){
				pWidth1[i] = 40;
				pWidth2[i] = 480;
				pX1[i] = 0;
				pX2[i] = (600 - 480);
				pY[i] = 400 + platform;
			}
			else if (rand == 7){
				pWidth1[i] = 480;
				pWidth2[i] = 40;
				pX1[i] = 0;
				pX2[i] = (600 - 40);
				pY[i] = 400 + platform;
			}
		}
		
		repaint();
	}
}

and here is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at DropG.paintComponent(DropG.java:31)
	at javax.swing.JComponent.paint(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._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(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)

Thanks for any help.

ava.lang.NullPointerException
at DropG.paintComponent(DropG.java:31)

Look at line 31 in the DropG class.
What variable is null at that line?
If you can't tell, add a println to print out the values of all the variables on that line.
Then back track to see why that variable is null.

I changed it so that I have set a definite value for the rectangles (as a test) and I get this error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at DropG.StartPlatformGen(DropG.java:192)
	at Drop.game(Drop.java:26)
	at DropMenu.mousePressed(DropMenu.java:86)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at DropG.paintComponent(DropG.java:37)
	at javax.swing.JComponent.paint(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._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(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)

Here is the code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class DropG extends JPanel implements KeyListener, ActionListener{

	int x = 290,y = 10; // frame 600 Wide, 400 Tall
	boolean onPlatform = false;
	int [] pX1, pX2 = new int [10];
	int [] pY = new int [10];
	int [] pWidth1, pWidth2 = new int [10];
	int velocity = 3, gravity = 5;
	Timer timer = new Timer (50, this);
	
	public void game (){
		//StartPlatformGen();
		//timer.start();
		grabFocus();
		addKeyListener (this);
		pY[0] = 0;
		pX1[0] = 0;
		pX2[0] = 70;
		pWidth1[0] = 50;
		pWidth2[0] = 70;
		repaint();
	}
	
	

	public void paintComponent (Graphics g){
		super.paintComponent(g);
		g.setColor (Color.red);
		g.fillRect(x, y, 20, 20);
		g.setColor (Color.green);
		//for (int i = 0; i < 10; i ++){
			
			g.fillRect(pX1[0], pY[0], pWidth1[0], 10);
			g.fillRect(pX2[0], pY[0], pWidth2[0], 10);
			
		//}
		
	}
		

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++){ // check if platform is off of the screen
			if (((pY[i])+ 10) <= 0){
				platformGen (i);
				
			}
		}
		
		if (onPlatform == false){
			y += gravity;
		}
	}

	private void platformGen(int i) {
		// TODO Auto-generated method stub
		int rand = (int)(Math.random () * 7) + 1; // generates a number between 1 and 7;
		/*
		 * Generates the platform that went off the screen
		 * */
		
		
		if (rand == 1){
			pWidth1[i] = 240;
			pWidth2[i] = 280;
			pX1[i] = 0;
			pX2[i] = (600 - 280);
			pY[i] = 400;
		}
		else if (rand == 2){
			pWidth1[i] = 260;
			pWidth2[i] = 260;
			pX1[i] = (600 - 260 - 260);
			pX2[i] = (600 - 260);
			pY[i] = 400;
		}
		else if (rand == 3){
			pWidth1[i] = 260;
			pWidth2[i] = 260;
			pX1[i] = 0;
			pX2[i] = (0 + 260);
			pY[i] = 400;
		}
		else if (rand == 4){
			pWidth1[i] = 360;
			pWidth2[i] = 160;
			pX1[i] = 0;
			pX2[i] = (600 - 160);
			pY[i] = 400;
		}
		else if (rand == 5){
			pWidth1[i] = 160;
			pWidth2[i] = 360;
			pX1[i] = 0;
			pX2[i] = (600 - 360);
			pY[i] = 400;
		}
		else if (rand == 6){
			pWidth1[i] = 40;
			pWidth2[i] = 480;
			pX1[i] = 0;
			pX2[i] = (600 - 480);
			pY[i] = 400;
		}
		else if (rand == 7){
			pWidth1[i] = 480;
			pWidth2[i] = 40;
			pX1[i] = 0;
			pX2[i] = (600 - 40);
			pY[i] = 400;
		}
		
	}
	
	public void StartPlatformGen() {
		// TODO Auto-generated method stub
		int platform = -40;
		for (int i = 0; i < 10; i ++){
			int rand = (int)(Math.random () * 7) + 1; // generates a number between 1 and 7;
			
			/*
			 * Generates the starting platforms.
			 * */
			platform += 40;
			System.out.println (rand);
			if (rand == 1){
				pWidth1[i] = 240;
				pWidth2[i] = 280;
				pX1[i] = 0;
				pX2[i] = (600 - 280);
				pY[i] = 400 + platform;
			}
			else if (rand == 2){
				pWidth1[i] = 260;
				pWidth2[i] = 260;
				pX1[i] = (600 - 260 - 260);
				pX2[i] = (600 - 260);
				pY[i] = 400 + platform;
			}
			else if (rand == 3){
				pWidth1[i] = 260;
				pWidth2[i] = 260;
				pX1[i] = 0;
				pX2[i] = (0 + 260);
				pY[i] = 400 + platform;
			}
			else if (rand == 4){
				pWidth1[i] = 360;
				pWidth2[i] = 160;
				pX1[i] = 0;
				pX2[i] = (600 - 160);
				pY[i] = 400 + platform;
			}
			else if (rand == 5){
				pWidth1[i] = 160;
				pWidth2[i] = 360;
				pX1[i] = 0;
				pX2[i] = (600 - 360);
				pY[i] = 400 + platform;
			}
			else if (rand == 6){
				pWidth1[i] = 40;
				pWidth2[i] = 480;
				pX1[i] = 0;
				pX2[i] = (600 - 480);
				pY[i] = 400 + platform;
			}
			else if (rand == 7){
				pWidth1[i] = 480;
				pWidth2[i] = 40;
				pX1[i] = 0;
				pX2[i] = (600 - 40);
				pY[i] = 400 + platform;
			}
			
			//System.out.println ("pX1 " + pX1[i] + ". pX2 " + pX2[i] + ". pY " + pY[i] + ". pWidth1" + pWidth1[i] + ". pWidth2" + pWidth2[i]);
			//pWidth1[i]
		}
		repaint();
			
	}
}

I am pretty sure the second error is all you need to worry about, for the first set did not have an error until the second one appeared.

java.lang.NullPointerException
at DropG.StartPlatformGen(DropG.java:192)

What variable is null at line 192? Why is it null?
Did you ever assign it a value?

java.lang.NullPointerException
at DropG.paintComponent(DropG.java:37)

Same thing here.

at line 192 the value is null, but that method is not called, so it should not create an error, also at line 37, all the values needed have been assigned, so there should not be an error there.

Edit: I fixed it so the other class does not call the startPlatformGen method. Here are the new errors I get.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at DropG.game(DropG.java:21)
	at Drop.game(Drop.java:26)
	at DropMenu.mousePressed(DropMenu.java:86)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at DropG.paintComponent(DropG.java:37)
	at javax.swing.JComponent.paint(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._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)ActivatedGame Method

	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(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)

Like I mentioned previously the first set appeared when the second error set appeared. Before then there was no error with them.

java.lang.NullPointerException
at DropG.game(DropG.java:21)

Look at my previous posts, except change the line number to 21.

Where do you assign a value to the variable that is null?
Copy and paste that line here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.