Java NullPointerException Help

overlord20

Gawd
Joined
Feb 5, 2010
Messages
623
I have run into this error and can't for the life of me figure out where it is coming from.

Enter Map Name: four.txt
Exception in thread "main" java.lang.NullPointerException
at Path.<init>(Path.java:12)
at Game.<init>(Game.java:29)
at endor.main(endor.java:19)

Here is the code and the objects listed above (Path, Game, endor). Its large, forgive me.

ENDOR:
Code:
import java.util.*;
import java.io.*;

public class endor
{
	public static void main(String[] args) throws IOException
	{
		Scanner scan = new Scanner(System.in);
		Random gen = new Random();
		Scanner file; 
		String map;
		Game play_one;
		
		System.out.print("Enter Map Name: ");
		map = scan.next();
		
		file = new Scanner(new FileReader(map));
		
		play_one = new Game(scan, gen, file);
		
		file.close();
		 	
	
	}

}

GAME:
Code:
import java.util.*;

public class Game
{
	private Scanner _scan;
	private Random _gen;
	private Polygon[] poly;
	private Path route;
	
	// 1) call new route
	// 2) fill route with route. 
	
	
	public Game(Scanner scan, Random gen, Scanner file)
	{
		int i, howmany, sides;
		boolean again = true;
		
		howmany = file.nextInt();
		
		poly = new Polygon[howmany];
		
		for (i = 0; i < howmany; i++)
		{
			sides = file.nextInt();
			poly[i] = new Polygon(file, sides);
		}
		
		route = new Path(scan);

		
		while (again == true)
		{ 
			route.randomPath(gen);
			again = detect(poly, route); 
		}
				

		System.out.println("Congradulations, you made it out of the"
				+ " forest!");
		
		
		
	}
	
	
	public boolean detect(Polygon[] polygons, Path route)
	{
		int i;
		
		for ( i = 0; i < polygons.length; i ++ )
			if ( polygons[i].intersect_path(route) )
				return true;
		
		return false;
	} 

}

PATH:
Code:
import java.util.*;

public class Path
{
	private Coord[] pathcoord;
	public Line[] path;
	
	public Path(Scanner scan) 
	{		
		pathcoord = new Coord[8];
		
		pathcoord[0].selectCoord(scan);
		pathcoord[7].selectCoord(scan);
	}

	public void randomPath(Random gen)
	{
		int i; 
		
		for (i = 1; i < 7; i++)
		{	
			pathcoord[i] = new Coord(gen);
		}
		
		path = new Line[7];
		
		for (i = 0; i < path.length; i++)
		{
			path[i] = new Line(pathcoord[i], pathcoord[i+1]	);
		}

	}
}

I don't want the answer but I do want help getting there or an complete explanation of things to check. If you need the other three objects let me know.

Help is appreciated, straight up answers aren't. CHEERS! :D
 
like it says on line 12, you are trying to do something to an item that is actually null.

hint, look up to the previous line to see why your item is null.

hint two, look down a bit to see where your item would not be null.
 
Last edited:
It works if I set up pathcoord to go to a constructor which I needed a bogus element for.

I can't think of how to make it not null.

Now the thing isn't detecting if it has crossed paths with another line. this == headache.
 
Last edited:
When you initialize an array of objects in java, each object is null. So, for example,
Code:
Foo[] manyFoo = new Foo[100];
will have an array with 100 Foo references, and each will be null. Before you can use them, you need to initialize them. So, if I wanted to use Foo[42], i'd have to do something like this:
Code:
Foo[42] = new Foo();

That would make it so Foo[42] is not null, but is actually a Foo. After which, I could do something like
Code:
Foo[42].EatBar();

However, trying to do this would give a NullPointerException:
Code:
Foo[0].EatBar();
 
Back
Top