So I have 3 classes in this project, and I'm to the point where I have no idea what I'm doing, so if anyone could help me with anything, I would greatly appreciate it.
When it compiles, I get a null pointer.
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class Project2 { static Scanner in = new Scanner(System.in); static State[] stateArray; public static void main(String args[]){ System.out.println("Project 2"); System.out.println(""); System.out.println("Stacks and Priority Queues"); System.out.print("Enter the file name: "); String filename = in.next(); Stack myStack = null; Priority sQueue = null; Priority wQueue = null; Priority mwQueue = null; try { BufferedReader br = new BufferedReader(new FileReader(filename));//reads in file if found String line = ""; int count = -1; while ((line = br.readLine()) != null){ //counts how many elements will be in array to give a length count = count + 1; } myStack = new Stack(count); sQueue = new Priority(count); wQueue = new Priority(count); mwQueue = new Priority(count); br.close(); //closes array count br = new BufferedReader(new FileReader(filename)); //reads in the same file if found and adds elements to array br.readLine(); count = 0; while ((line = br.readLine()) != null){ String[] myState = line.split(","); State newState = new State(myState[0], myState[1], myState[2], Integer.parseInt(myState[3]), myState[4], Integer.parseInt(myState[5])); if(newState.getRegion().toLowerCase() == "south" || newState.getRegion().toLowerCase()=="west" || newState.getRegion().toLowerCase()=="midwest"){ myStack.push(newState); } } br.close(); //closes array reader } catch (FileNotFoundException e) { System.out.println(""); System.out.println("File not found."); System.out.println(""); } catch (IOException e) { System.out.println(""); System.out.println("File could not be opened."); System.out.println(""); } System.out.println(""); System.out.println("There were " + Integer.toString(myStack.size()) + " put on the stack."); System.out.println(""); myStack.printStack(); while(myStack.size()>0) { State tmp = myStack.pop(); switch(tmp.getRegion().toLowerCase()) { case "south": sQueue.insert(tmp); break; case "west": wQueue.insert(tmp); break; case "midwest": mwQueue.insert(tmp); break; } } sQueue.printQueue(); wQueue.printQueue(); mwQueue.printQueue(); for(int i = 0; i<sQueue.Size(); i++) { State tmp = sQueue.remove(); myStack.push(tmp); } for(int i = 0; i<wQueue.Size(); i++) { State tmp = wQueue.remove(); myStack.push(tmp); } for(int i = 0; i<mwQueue.Size(); i++) { State tmp = mwQueue.remove(); myStack.push(tmp); } myStack.printStack(); } }
public class Stack { static State[] stateArray; private int maxSize; private int top; public Stack(int x){ maxSize = x; stateArray = new State[maxSize]; top = -1; } public boolean push(State length){ if(isFull()) return false; else{ stateArray[top+1] = length; top++; return true; } } public State pop(){ if(isEmpty()) return null; else{ return stateArray[top+1]; } } public boolean isEmpty(){ return (top == - 1); } public boolean isFull(){ return (top+1 == maxSize); } public void printStack(){ System.out.println("Stack Contents:"); System.out.println(""); System.out.format("%-40s%-40s%-32s%-32s%-32s%-32s\n","State Name","Capital City","State Abbr","State Population","Region","US House Seats"); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------"); for(int x = stateArray.length-1; x > -1 ; x--){ stateArray[x].printName(); } } public int size(){ int count = 0; for (int i = 0; i < stateArray.length; i++){ if (stateArray[i] != null) count++; } return count; } }
public class Priority { private int maxSize; private State[] stateArray; public Priority(int count){ maxSize = stateArray.length; stateArray = new State[maxSize]; } public void insert(State item){ int y; if(Size() == 0) stateArray[Size()+1] = item; else{ for(y = Size() - 1; y >= 0; y--){ if( item.getStatePopulation() > stateArray[y].getStatePopulation()) stateArray[y + 1] = stateArray[y]; else{ break; } } stateArray[y+ + 1] = item; Size(); } } public State remove(){ return stateArray[Size()-1]; } public boolean isEmpty(){ return (Size() == 0); } public boolean isFull(){ return (Size() == maxSize); } public void printQueue(){ System.out.println("Region Priority Queue Contents: "); System.out.println(""); System.out.format("%-40s%-40s%-32s%-32s%-32s%-32s\n","State Name","Capital City","State Abbr","State Population","Region","US House Seats"); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------"); for(int x = 0; x < stateArray.length; x++){ stateArray[x].printName(); } } public int Size () { int count = 0; for (int i = 0; i < stateArray.length; i++){ if (stateArray[i] != null) count++; } return count; } }