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

New Topic/Question
Reply



MultiQuote




|