NullPointerException error

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1518 Views - Last Post: 18 April 2011 - 12:37 PM Rate Topic: -----

#1 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

NullPointerException error

Posted 17 April 2011 - 04:59 PM

I'm stuck on this code, and my problem is reading into the file and then sorting the array so that the pairings can be made.


A Java program to create tournament pairings is to be created. The team names along with
their wins should be stored in an array of objects. For this assignment the data will be
obtained from a text file arranged in descending order by number of wins. The data for
each team should be read from the input file. An object for that team should be created
and then the object reference variable value should be stored in the present array
element. After all data is read from the file and stored in the array, the pairings can be
made.

The error I'm getting is Exception in thread "main" java.lang.NullPointerException
at Tournment.main(Tournment.java:25)



 
import java.io.*;
import java.util.Scanner;


public class Tournment  
{
	public static void main(String [] args) throws IOException
	{
		File file = new File("Teams.txt");
      Scanner input = new Scanner(file);
		
		String w; 
		int wins; 
		String teamName; 
		int gameCounter=0;  
		
		Teams [] game = new Teams [8];
		for (int i= 0; i < game.length; i+=2)
		{
		
			teamName = input.next(); 
			w= input.next();
			wins= Integer.parseInt(w);
			
			System.out.println(game[i].getName() + " : " + "Wins: " + game[i].getWins());

         System.out.println("  VS.");
			
			System.out.println(game[i+1].getName() + " : " + "Wins: " + game[i+1 ].getWins());
					  
		}  

   }
}

public class Teams
{
	String teamName;
	int wins;
	
	public Teams()
	{
	}
	
	public Teams (String teamName, int wins)
	{
		this.teamName = teamName;
		this.wins = wins;
	}
	
	public String getName()
   {
      return teamName;
	}
	public int getWins()
	{
		return wins; 
	}
	
	

	
	
}
 



Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException error

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: NullPointerException error

Posted 17 April 2011 - 05:06 PM

The line:
Teams [] game = new Teams [8];



Does NOT make 8 instantiated objects - it makes SPACE FOR them... You need to instatiate the objects first. And frankly, I don't see the point in saving an printing in the same loop - why even have the array then? I think is is more what you are looking for.

Teams [] game = new Teams [8];
		for (int i= 0; i < game.length; i++) // Why did you have i+=2?
		{
		
			teamName = input.next(); 
			w= input.next();
			wins= Integer.parseInt(w);
			game[i] = new Teams(teamName, wins);
		}

		for (int i = 0; i < game.length-1; i+=2) {
			
			System.out.println(game[i].getName() + " : " + "Wins: " + game[i].getWins());

         System.out.println("  VS.");
			
			System.out.println(game[i+1].getName() + " : " + "Wins: " + game[i+1 ].getWins());
					  
		}

Was This Post Helpful? 1
  • +
  • -

#3 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

Re: NullPointerException error

Posted 17 April 2011 - 05:17 PM

It outputted the match up between the teams , but it didn't pair the first team with the last team, and so. What i'm trying to do is have it like a bracket were the team with the most wins is paired with the team with the least wins.
The team.txt is already in descending order by wins, I just have to pair them.

Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: NullPointerException error

Posted 17 April 2011 - 05:22 PM

Oh...then you're going to have to sort it by number of wins.

Use a comparator as described in the following tutorial, but apply it to the getWins() method. Then, use the sort() method to use this Comparator to...well...sort them. Then you just have to print them out.
http://www.dreaminco...are-to-compare/
Was This Post Helpful? 0
  • +
  • -

#5 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

Re: NullPointerException error

Posted 17 April 2011 - 07:42 PM

Well my prof. said that were supposed to use two counters and so I was going to nest a for loop inside the already existing code, but it gives an error that says ArrayIndexOutOfBoundsException.

for (int i = 0; i < game.length-1; i+=2)
		 {
			
			System.out.println(game[i].getName() + " : " + "Wins: " + game[i].getWins());

         System.out.println("  VS.");
			
			for (int j = 0; j < game.length; j+=2)
			{
			
				System.out.println(game[j+7].getName() + " : " + "Wins: " + game[j+7].getWins());
			
			}


This post has been edited by fathia09: 17 April 2011 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: NullPointerException error

Posted 17 April 2011 - 07:52 PM

That is totally incorrect. If j equals the length of the elements, how are you supposed to access 7 past that? You simply can't.
Was This Post Helpful? 0
  • +
  • -

#7 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

Re: NullPointerException error

Posted 17 April 2011 - 07:58 PM

ok well there was another that i was doing, and i have done in other programs, but for this one it keeps giving me this error
Tournment.java:37: cannot find symbol
symbol : variable length


here are the instructions the prof gave us
-Create counter1 that starts at the end of the array (array.length - 1)
-Create counter2 that starts at the beginning of the array
-While counter1 -Ouput the team name at counter1 vs. the team name at counter2

for (int i = 0; i < game.length-1; i+=2)
		 {
			
			System.out.println(game[i].getName() + " : " + "Wins: " + game[i].getWins());

         System.out.println("  VS.");
			
			for (int j = 0; j < game[i].length; j+=2)
			{
			
				System.out.println(game[j].getName() + " : " + "Wins: " + game[j].getWins());
			
			}


This post has been edited by fathia09: 17 April 2011 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: NullPointerException error

Posted 17 April 2011 - 08:01 PM

game[i].length

You can't simply copy-n-paste from other projects. This form is used in 2D array iteration. Right now, game[i] is a Tournament object, because you set the array to be a Tournament[]. The form you used above assumes an array of arrays of Tounaments. Make 2 independent loops and sort them. If you want help sorting them, I will be glad to show you one way to do so.
Was This Post Helpful? 1
  • +
  • -

#9 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

Re: NullPointerException error

Posted 17 April 2011 - 08:15 PM

I would greatly appreciate it.


Thanks
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: NullPointerException error

Posted 17 April 2011 - 08:21 PM

You can use a bubblesort algorithm. Since macosxnerd101 made a snippet on it, I'll refer you there:
http://www.dreaminco...snippet4504.htm

You will need to compare the getWins() method.

If you need to approach it a special way due to class requirements, let me know, but I think that should do it.
Was This Post Helpful? 0
  • +
  • -

#11 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

Re: NullPointerException error

Posted 17 April 2011 - 09:31 PM

well i think she just wants us to use loops because she wants to use two different counters
i try this out


for (int i = 0; i < game.length - 1; i++)
		{
			for(int j =0; j < game.length; j--);
			{
				if(game[i] < game[j])
				{  
				System.out.println(game[i].getName()+ "  VS." + game[j].getName());
				}
			}
			
		}



but its says the variable j is not found even though i declared it
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: NullPointerException error

Posted 17 April 2011 - 09:36 PM

Remove the semi-colon at the end of your for loop here for(int j =0; j < game.length; j--);. That closes the for loop, leaving the braces as a non-related block of code.
Was This Post Helpful? 0
  • +
  • -

#13 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

Re: NullPointerException error

Posted 17 April 2011 - 09:38 PM

i realized that after i sent it. I removed the semi-colon but it gives this error
operator < cannot be applied to Teams,Teams
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: NullPointerException error

Posted 17 April 2011 - 09:40 PM

Do you want to compare the Teams on their numbers of wins? If so, invoke the getWins() method off of each Team object and compare the results of those method calls.
Was This Post Helpful? 0
  • +
  • -

#15 fathia09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-April 11

Re: NullPointerException error

Posted 17 April 2011 - 09:46 PM

I'm sorry, but i just become really lost.

So you suggest in the class Teams invoke the getWins method or should i do that in the tournament class.


Thanks for all the help
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2