Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Stacks and Priority Queues Project

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Stacks and Priority Queues Project

    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;
    	}
     
     
    }
    Last edited by RadicalCaitlin; July 18th, 2014 at 11:40 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stacks and Priority Queues Project

    When it compiles, I get a null pointer.
    Please copy the full text of the error message and paste it here.

    BTW NullPointerExceptions happen when a program is executed, not when it is compiled.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stacks and Priority Queues Project

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.

    BTW NullPointerExceptions happen when a program is executed, not when it is compiled.
    Exception in thread "main" java.lang.NullPointerException
    at Priority.<init>(Priority.java:9)
    at Project2.main(Project2.java:37)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stacks and Priority Queues Project

    Exception in thread "main" java.lang.NullPointerException
    at Priority.<init>(Priority.java:9)
    There is a variable with a null value on line 9. Look at line 9 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 9 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stacks and Priority Queues Project

    this Priority (id=17)
    maxSize 0
    stateArray null
    count 56

    This is what it says.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stacks and Priority Queues Project

    What variable has the null value? Where should that variable have been assigned a value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stacks and Priority Queues Project

    The values are read in from a csv file... but I moved the code around to be:

     public Stack(int x){
     
    		maxSize = x;
    		stateArray = new State[maxSize];
    		top = -1;
     
    	}

    So now it says:

    Exception in thread "main" java.lang.NullPointerException
    at Stack.printStack(Stack.java:57)
    at Project2.main(Project2.java:74)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stacks and Priority Queues Project

    Exception in thread "main" java.lang.NullPointerException
    at Stack.printStack(Stack.java:57)
    Same as before. Look at line 57, find the variable and find why it does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    RadicalCaitlin (February 21st, 2014)

  10. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stacks and Priority Queues Project

    Hey Norm,

    I'm going to have a buddy help me figure it out in person. I think it's a bit too complicated to figure out my mess over the internet, but I appreciate your time.

    Thank you

Similar Threads

  1. regarding queues
    By IHeartProgramming in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2012, 07:24 PM
  2. [SOLVED] Please Help (Queues)
    By husain2213 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 30th, 2012, 12:01 AM
  3. Threads priority: low priority run faster than high priority
    By leonixyz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2011, 04:33 PM
  4. Help with queues
    By araujo3rd in forum Collections and Generics
    Replies: 2
    Last Post: March 10th, 2010, 11:20 AM
  5. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 02:28 PM

Tags for this Thread