-1

This is the beginning of my project:

//class variables
private static int numberOfCouples;
//object arrays 
private static Witch[] witch;
private static Wizard[] wizard;
//ranking arrays
private static int[][] witchPartnerRanking;
private static int[][] wizardPartnerRanking;
//proposal array
private static boolean[][] proposal;

//DO NOT ALTER THE MAIN METHOD
public static void main(String[] args) throws FileNotFoundException {
    go( "coven_dance.txt" );

    System.out.println( "Witch array:" );

    for ( int i = 0; i < witch.length; i++ ) {
        System.out.printf( "Witch %d:, name: %-20s, current partner id: %2d, proposals made: %d%n", 
                            witch[ i ].getId(), witch[ i ].getName(), witch[ i ].getParnerWizardId(), witch[ i ].getNumberOfProposals() );

A NullPointerException happens on the last line. Even though I have this:

 private static void go( String fileName ) throws FileNotFoundException { 


//1. READ THE FILE
    Scanner inStream = new Scanner( new File ( fileName ));
     numberOfCouples = inStream.nextInt();
   // System.out.println(howManyCouples);
    //System.out.print(
   inStream.nextLine();

//2. CREATE AND POPULATE THE witch ARRAY
    /*Witch[]*/ witch = new Witch[numberOfCouples*2];
    /*int [][]*/ witchPartnerRanking = new int[numberOfCouples*2][numberOfCouples];
    for (int i = 0; i < numberOfCouples*2; i++) {
      if(inStream.hasNextInt()){

      //4. CREATE AND POPULATE THE witchPartnerRanking ARRAY

        int j = 0;
         while(inStream.hasNextInt() && j<numberOfCouples){

            int prefer = inStream.nextInt();
              //System.out.println(prefer);
            witchPartnerRanking[i][j] = prefer;
         j++;

         }
         String bob = inStream.nextLine();
          //System.out.println(bob);
      }
      else{
        String name = inStream.next();
          //System.out.println(name);
       Witch the = new Witch(i, name);
        witch[i] = the; 

      }
    } 

I'm not sure what's going on. If anyone could help, that would be awesome.

Here's the file I'm reading from if you need it:

4  
 NannyOgg 0 2 3 1  
TiffanyAching 3 2 0 1  
MagratGarlick 0 3 2 1  
GrannyWeatherwax 0 3 2 1  
TheLibrarian 2 1 3 0  
PonderStibbons 2 1 3 0  
LordVetinari 1 2 0 3  
DEATH 3 2 1 0  
Floern
  • 33,559
  • 24
  • 104
  • 119
Shauna
  • 1
  • 1
  • that means that the file "coven_dance.txt" does not exist. or it is not in your classpath, so you have to put the absolute path to this file. As it appears in the code of `go(String fileName), if the file is not foundm the values are not set. so you have called methods on null references – user1314742 Apr 10 '16 at 16:03
  • You help the experts finding your question when you tell them your programming language and also include it as tag. – Meier Apr 10 '16 at 17:11
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – TylerH May 04 '16 at 21:59

1 Answers1

0

You witch array is being accessed without being initialized.

You have private static Witch[] witch; which is declared but not initialized

You need to initialize it. One way to do so is:

private static Witch[] witch = new Witch[10]; // replace 10 with any array size you want

In Java, arrays are treated as regular objects. Whenever you are accessing an object that is null, you get a NullPointerException.

Victor2748
  • 4,149
  • 13
  • 52
  • 89
  • I'm not allowed to touch the main. And I tried to initialize like you said in my method but NetBeans in yelling at me because it says it's already define – Shauna Apr 11 '16 at 00:46