6 Replies - 433 Views - Last Post: 29 November 2012 - 11:57 AM Rate Topic: -----

#1 ihavenoideawhatsgoingon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

Java input/output files

Posted 29 November 2012 - 08:12 AM

Hello! Here's some code I've been working on for a class but have been stuck getting the error message for the past few days. I'm new to java and am using this as a last resort of help because no other classmates can seem to figure out this problem either. Just trying to be honest and any help will be much appreciated! Here's the error message:

Exception in thread "main" java.lang.NullPointerException
at project3.moveData(project3.java:142)
at project3.main(project3.java:68)
Java Result: 1

It doesn't seem to like when I try to find how long the maximum line in the array is for space and claims I'm using a null object. Silly java...


 * This program uses arrays to store information about each state in the U.S. including:
 * nickname, capital, flower, and population. It will prompt the user to enter
 * the name of the state in which they are interested in finding this information for and will display the 
 * correct information respectively. 
 */

/**
 *
 * @author <removed>
 */
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.text.DecimalFormat; 
import java.io.FileNotFoundException;
import java.io.IOException;

public class project3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, FileNotFoundException{
        
        int size;
        String input;
        String[] state;
        String[] nickname;
        String[] capital;
        String[] flower;
        String[] population;
        char userChoice = ' ';
        
        File statesData = new File("state_data.txt");
        if(!statesData.exists())
        {
            System.out.println("The input file was not found");
            System.out.println("The system will now exit");
            System.exit(0);
        }
        
            System.out.println("Input file: " +statesData); 
        
        Scanner keyboard = new Scanner (System.in);
        PrintWriter outputFile = new PrintWriter ("Output_Data.txt");
        Scanner statesInfo = new Scanner (statesData);
  

        
        size = calculateSize(statesInfo);
        

        
        state = new String[size];
        nickname = new String[size];
        capital = new String[size];
        flower = new String[size];
        population = new String[size];
        
        calculateSize(statesInfo);
        compileData(statesInfo, nickname, flower, state, capital, population);
        statesInfo.close();
        
        moveData(outputFile, nickname, flower, state, capital, population);

        System.out.println("");
        System.out.println("Which state would you like to learn about?");
        input = keyboard.next();
        
        findData(input, nickname, flower, state, capital, population);
        outputFile.close();
        
        while (userChoice != 'N') 
        {
            System.out.println();
            System.out.println("Would you like to learn about another state? (Y/N)");
            userChoice = keyboard.next().charAt(0);
            if (userChoice == 'Y' || userChoice == 'y') {
                System.out.println("Enter the name of a state:");
                input = keyboard.next();
                findData(input, nickname, flower, state, capital, population);
            } else {
                userChoice = 'N';
                System.out.println();
                System.out.println("The system will now exit.");
                System.out.println("The state information has been written to the file \"Output_Data.txt.\"");
                System.out.println();
            }
        }
}   
        /**
     *
     * @param data
     * @return
     * @throws IOException
     */
    public static int calculateSize(Scanner statesInfo)
        {
        int i = 0;
        while (statesInfo.hasNextLine())
        {
            statesInfo.nextLine();
            i++;
        }
        i /= 5;
        return i; 
        }

    public static void compileData(Scanner data, String [] nickname, String[] flower, String[] state, String[] capital, String[] population)
    {
        int i = 0;
        int s = 0;
        DecimalFormat outputFormat = new DecimalFormat ("###,###,###");
        
        while (data.hasNextLine())
        {
            nickname[i] = data.nextLine();
            flower[i] = data.nextLine();
            state[i] = data.nextLine();
            capital[i] = data.nextLine();
            population[i] = outputFormat.format(s);
            s=Integer.parseInt(data.nextLine());
            i++;
        }
          
    }
        
    public static void moveData(PrintWriter data, String[] nickname, String[] flower, String[] state, String[] capital, String[] population)
    {
        int nicknameSize = 0;
        int flowerSize = 0;
        int stateSize = 0;
        int capitalSize = 0;
        int popSize = 0;
        
        for (int i = 0; i < nickname.length; i++)
        {
            if (nicknameSize < nickname[i].length())
            {
                nicknameSize = nickname[i].length();
            }
        }
        for (int i = 0; i < flower.length; i++) 
        {
            if (flowerSize < flower[i].length())
            {
                flowerSize = flower[i].length();
            }
        }
        for (int i = 0; i < state.length; i++)
        {
            if (stateSize < state[i].length())
            {
               stateSize = state[i].length();
            }
        }
        for (int i = 0; i < capital.length; i++)
        {
            if (capitalSize < capital[i].length())
            {
                capitalSize = capital[i].length();
            }
        }
        for (int i = 0; i < population.length; i++)
        {
            if (popSize < population[i].length())
            {
                popSize = population[i].length();
            }
        }
        nicknameSize += 3;
        flowerSize += 3;
        stateSize += 3;
        flowerSize += 3;
        data.print(String.format("%-" + nicknameSize + "s", "nickname"));
        data.print(String.format("%-" + flowerSize + "s", "flower"));
        data.print(String.format("%-" + stateSize + "s", "state"));
        data.print(String.format("%-" + capitalSize + "s", "capital"));
        data.print(String.format("%-" + popSize + "s", "population"));
        data.println();
        for (int i = 0; i < state.length; i++)
        {
            data.print(String.format("%-" + nicknameSize + "s", nickname[i]));
            data.print(String.format("%-" + flowerSize + "s", flower[i]));
            data.print(String.format("%-" + stateSize + "s", state[i]));
            data.print(String.format("%-" + capitalSize + "s", capital[i]));
            data.print(String.format("%-" + popSize + "s", population[i]));
            data.println();
        }
        
    }
    public static void findData(String input,String[] nickname, String[] flower, String[] state, String[] capital, String[] population)
    {
     boolean exists = false;
     for (int i = 0; i < state.length; i++)
     {
         if (input.equalsIgnoreCase(state[i]))
         {
             System.out.println("");
             System.out.println("Nickname: " + nickname[i]);
             System.out.println("Flower: " + flower[i]);
             System.out.println("State: " + state[i]);
             System.out.println("Capital: " + capital[i]);
             System.out.println("Population: " +population [i]);
             exists = true;
         }
     }
    
    if (exists == false)
    {
        System.out.println("There is no information for that state.");
    }

    }       
    
}

This post has been edited by macosxnerd101: 29 November 2012 - 01:39 PM
Reason for edit:: highlight the text and just click the 'code' button in the text editor, removed personal information


Is This A Good Question/Topic? 0
  • +

Replies To: Java input/output files

#2 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Java input/output files

Posted 29 November 2012 - 08:41 AM

you forgot the
popSize += 3;
capitalSize += 3;



at line 174

you also repeated the flowerSize+=3 , two times
Was This Post Helpful? 0
  • +
  • -

#3 ihavenoideawhatsgoingon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

Re: Java input/output files

Posted 29 November 2012 - 08:49 AM

View PostoQMr FoxQo, on 29 November 2012 - 08:41 AM, said:

you forgot the
popSize += 3;
capitalSize += 3;



at line 174

you also repeated the flowerSize+=3 , two times


Thank you for your quick reply and keen eye! I made those corrections but am still receiving the same error message.
Was This Post Helpful? 0
  • +
  • -

#4 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Java input/output files

Posted 29 November 2012 - 09:11 AM

compile this code and tell me what error are you receiving ?
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.text.DecimalFormat; 
import java.io.FileNotFoundException;
import java.io.IOException;

public class project3 {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) throws IOException, FileNotFoundException{
       
       int size;
       String input;
       String[] state;
       String[] nickname;
       String[] capital;
       String[] flower;
       String[] population;
       char userChoice = ' ';
       
       File statesData = new File(state_data.txt");
       if(!statesData.exists())
       {
           System.out.println("The input file was not found");
           System.out.println("The system will now exit");
           System.exit(0);
       }
       
           System.out.println("Input file: " +statesData); 
       
       Scanner keyboard = new Scanner (System.in);
       PrintWriter outputFile = new PrintWriter ("Output_Data.txt");
       Scanner statesInfo = new Scanner (statesData);
 

       
       size = calculateSize(statesInfo);
       

       
       state = new String[size];
       nickname = new String[size];
       capital = new String[size];
       flower = new String[size];
       population = new String[size];
       
       calculateSize(statesInfo);
       compileData(statesInfo, nickname, flower, state, capital, population);
       statesInfo.close();
       
       moveData(outputFile, nickname, flower, state, capital, population);

       System.out.println("");
       System.out.println("Which state would you like to learn about?");
       input = keyboard.next();
       
       findData(input, nickname, flower, state, capital, population);
       outputFile.close();
       
       while (userChoice != 'N') 
       {
           System.out.println();
           System.out.println("Would you like to learn about another state? (Y/N)");
           userChoice = keyboard.next().charAt(0);
           if (userChoice == 'Y' || userChoice == 'y') {
               System.out.println("Enter the name of a state:");
               input = keyboard.next();
               findData(input, nickname, flower, state, capital, population);
           } else {
               userChoice = 'N';
               System.out.println();
               System.out.println("The system will now exit.");
               System.out.println("The state information has been written to the file \"Output_Data.txt.\"");
               System.out.println();
           }
       }
}   
       /**
    *
    * @param data
    * @return
    * @throws IOException
    */
   public static int calculateSize(Scanner statesInfo)
       {
       int i = 0;
       while (statesInfo.hasNextLine())
       {
           statesInfo.nextLine();
           i++;
       }
       i /= 5;
       return i; 
       }

   public static void compileData(Scanner data, String [] nickname, String[] flower, String[] state, String[] capital, String[] population)
   {
       int i = 0;
       int s = 0;
       DecimalFormat outputFormat = new DecimalFormat ("###,###,###");
       
       while (data.hasNextLine())
       {
           nickname[i] = data.nextLine();
           flower[i] = data.nextLine();
           state[i] = data.nextLine();
           capital[i] = data.nextLine();
           population[i] = outputFormat.format(s);
           s=Integer.parseInt(data.nextLine());
           i++;
       }
         
   }
       
   public static void moveData(PrintWriter data, String[] nickname, String[] flower, String[] state, String[] capital, String[] population)
   {
       int nicknameSize = 0;
       int flowerSize = 0;
       int stateSize = 0;
       int capitalSize = 0;
       int popSize = 0;
       
       for (int i = 0; i < nickname.length; i++)
       {
           if (nicknameSize < nickname[i].length())
           {
        	   
               nicknameSize = nickname[i].length();
               
           }
       }
       for (int i = 0; i < flower.length; i++) 
       {
           if (flowerSize < flower[i].length())
           {
               flowerSize = flower[i].length();
           }
       }
       for (int i = 0; i < state.length; i++)
       {
           if (stateSize < state[i].length())
           {
              stateSize = state[i].length();
           }
       }
       for (int i = 0; i < capital.length; i++)
       {
           if (capitalSize < capital[i].length())
           {
               capitalSize = capital[i].length();
           }
       }
       for (int i = 0; i < population.length; i++)
       {
           if (popSize < population[i].length())
           {
               popSize = population[i].length();
           }
       }
       nicknameSize += 3;
       flowerSize += 3;
       stateSize += 3;
       popSize += 3;
       capitalSize += 3;
       
       data.print(String.format("%-" + nicknameSize + "s", "nickname"));
       data.print(String.format("%-" + flowerSize + "s", "flower"));
       data.print(String.format("%-" + stateSize + "s", "state"));
       data.print(String.format("%-" + capitalSize + "s", "capital"));
       data.print(String.format("%-" + popSize + "s", "population"));
       data.println();
       for (int i = 0; i < state.length; i++)
       {
           data.print(String.format("%-" + nicknameSize + "s", nickname[i]));
           data.print(String.format("%-" + flowerSize + "s", flower[i]));
           data.print(String.format("%-" + stateSize + "s", state[i]));
           data.print(String.format("%-" + capitalSize + "s", capital[i]));
           data.print(String.format("%-" + popSize + "s", population[i]));
           data.println();
       }
       
   }
   public static void findData(String input,String[] nickname, String[] flower, String[] state, String[] capital, String[] population)
   {
    boolean exists = false;
    for (int i = 0; i < state.length; i++)
    {
        if (input.equalsIgnoreCase(state[i]))
        {
            System.out.println("");
            System.out.println("Nickname: " + nickname[i]);
            System.out.println("Flower: " + flower[i]);
            System.out.println("State: " + state[i]);
            System.out.println("Capital: " + capital[i]);
            System.out.println("Population: " +population [i]);
            exists = true;
        }
    }
   
   if (exists == false)
   {
       System.out.println("There is no information for that state.");
   }

   }       
   
}



This post has been edited by macosxnerd101: 29 November 2012 - 01:39 PM
Reason for edit:: Removed the OP's personal information

Was This Post Helpful? 0
  • +
  • -

#5 ihavenoideawhatsgoingon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

Re: Java input/output files

Posted 29 November 2012 - 09:14 AM

I updated my code. The array was showing up as null because the program ran through the array list, reached the bottom and stopped there. I had to recall the scanner to reopen the array and begin reading from the beginning of the file. However, now whenever I run my program and it prompts me to enter a state, every time, no matter what the case, it says there is no information on that State. Any help for this new problem?
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.text.DecimalFormat; 
import java.io.FileNotFoundException;
import java.io.IOException;

public class xxxxxxx_project3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, FileNotFoundException{
        
        int size;
        String input;
        String[] state;
        String[] nickname;
        String[] capital;
        String[] flower;
        String[] population;
        char userChoice = ' ';
        
        File statesData = new File("state_data.txt");
        if(!statesData.exists())
        {
            System.out.println("The input file was not found");
            System.out.println("The system will now exit");
            System.exit(0);
        }
        
            System.out.println("Input file: " +statesData); 
        
        Scanner keyboard = new Scanner (System.in);
        PrintWriter outputFile = new PrintWriter ("Output_Data.txt");
        Scanner statesInfo = new Scanner (statesData);
  

        
        size = calculateSize(statesInfo);
        

        
        state = new String[size];
        nickname = new String[size];
        capital = new String[size];
        flower = new String[size];
        population = new String[size];
        
        calculateSize(statesInfo);
        statesInfo.close();
        
        Scanner statesInfo2 = new Scanner(statesData);
        compileData(statesInfo2, nickname, flower, state, capital, population);
        statesInfo2.close();
        
        moveData(outputFile, nickname, flower, state, capital, population);

        System.out.println("");
        System.out.println("Which state would you like to learn about?");
        input = keyboard.next();
        
        findData(input, nickname, flower, state, capital, population);
        outputFile.close();
        
        while (userChoice != 'N') 
        {
            System.out.println();
            System.out.println("Would you like to learn about another state? (Y/N)");
            userChoice = keyboard.next().charAt(0);
            if (userChoice == 'Y' || userChoice == 'y') {
                System.out.println("Enter the name of a state:");
                input = keyboard.next();
                findData(input, nickname, flower, state, capital, population);
            } else {
                userChoice = 'N';
                System.out.println();
                System.out.println("The system will now exit.");
                System.out.println("The state information has been written to the file \"Output_Data.txt.\"");
                System.out.println();
            }
        }
}   
        /**
     *
     * @param data
     * @return
     * @throws IOException
     */
    public static int calculateSize(Scanner statesInfo)
        {
        int i = 0;
        while (statesInfo.hasNextLine())
        {
            statesInfo.nextLine();
            i++;
        }
        i /= 5;
        return i; 
        }

    public static void compileData(Scanner data, String[] nickname, String[] flower, String[] state, String[] capital, String[] population)
    {
        int i = 0;
        int s = 0;
        DecimalFormat outputFormat = new DecimalFormat ("###,###,###");
        
        while (data.hasNextLine())
        {
            nickname[i] = data.nextLine();
            flower[i] = data.nextLine();
            state[i] = data.nextLine();
            capital[i] = data.nextLine();
            s = Integer.parseInt(data.nextLine());
            population[i] = outputFormat.format(s);
            i++;
        }
          
    }
        
    public static void moveData(PrintWriter data, String[] nickname, String[] flower, String[] state, String[] capital, String[] population)
    {
        int nicknameSize = 0;
        int flowerSize = 0;
        int stateSize = 0;
        int capitalSize = 0;
        int popSize = 0;
        
        for (int i = 0; i < nickname.length; i++)
        {
            if (nicknameSize < nickname[i].length())
            {
                nicknameSize = nickname[i].length();
            }
        }
        for (int i = 0; i < flower.length; i++) 
        {
            if (flowerSize < flower[i].length())
            {
                flowerSize = flower[i].length();
            }
        }
        for (int i = 0; i < state.length; i++)
        {
            if (stateSize < state[i].length())
            {
               stateSize = state[i].length();
            }
        }
        for (int i = 0; i < capital.length; i++)
        {
            if (capitalSize < capital[i].length())
            {
                capitalSize = capital[i].length();
            }
        }
        for (int i = 0; i < population.length; i++)
        {
            if (popSize < population[i].length())
            {
                popSize = population[i].length();
            }
        }
        nicknameSize += 3;
        stateSize += 3;
        flowerSize += 3;
        popSize += 3;
        capitalSize += 3;
        data.print(String.format("%-" + nicknameSize + "s", "nickname"));
        data.print(String.format("%-" + flowerSize + "s", "flower"));
        data.print(String.format("%-" + stateSize + "s", "state"));
        data.print(String.format("%-" + capitalSize + "s", "capital"));
        data.print(String.format("%-" + popSize + "s", "population"));
        data.println();
        for (int i = 0; i < state.length; i++)
        {
            data.print(String.format("%-" + nicknameSize + "s", nickname[i]));
            data.print(String.format("%-" + flowerSize + "s", flower[i]));
            data.print(String.format("%-" + stateSize + "s", state[i]));
            data.print(String.format("%-" + capitalSize + "s", capital[i]));
            data.print(String.format("%-" + popSize + "s", population[i]));
            data.println();
        }
        
    }
    public static void findData(String input,String[] nickname, String[] flower, String[] state, String[] capital, String[] population)
    {
     boolean exists = false;
     for (int i = 0; i < state.length; i++)
     {
         if (input.equalsIgnoreCase(state[i]))
         {
             System.out.println("");
             System.out.println("Nickname: " + nickname[i]);
             System.out.println("Flower: " + flower[i]);
             System.out.println("State: " + state[i]);
             System.out.println("Capital: " + capital[i]);
             System.out.println("Population: " +population [i]);
             exists = true;
         }
     }
    
    if (exists == false)
    {
        System.out.println("There is no information for that state.");
    }

    }       
    
}

This post has been edited by jon.kiparsky: 29 November 2012 - 09:36 AM
Reason for edit:: code tags

Was This Post Helpful? 0
  • +
  • -

#6 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Java input/output files

Posted 29 November 2012 - 09:31 AM

1- when you put your code wrap it into [ code ] tag
2- is it homework ?
3-is it your code or you just copied it from somewhere ?
4- when are you typing the state are you making sure the state exist in the file state_data.txt?

This post has been edited by oQMr FoxQo: 29 November 2012 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#7 ihavenoideawhatsgoingon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 12

Re: Java input/output files

Posted 29 November 2012 - 11:57 AM

View PostoQMr FoxQo, on 29 November 2012 - 09:31 AM, said:

1- when you put your code wrap it into [ code ] tag
2- is it homework ?
3-is it your code or you just copied it from somewhere ?
4- when are you typing the state are you making sure the state exist in the file state_data.txt?


Sorry about the formatting errors, I'm new to this site. This is code for an extra credit project in one of my classes. I don't mean to simply have you guys do it but maybe you have come across an error like this before
All of the states exist in the file, I believe it may have something to do with the last for loop. Maybe change it to a while loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1