Here is my assignment:

https://docs.google.com/document/edit?id=1lf2t8LOqKmXK8tu8lW7ghDOd1IJw9BXBRi_PDXFtPQo&authkey=CLnyu7UE&hl=en#

Here are the other classes I am using:

https://docs.google.com/leaf?id=0B_UHOsQPEyMBNDBhMWExNTgtMTJmNy00MTQxLWI1ZGItMDZiZjM3YTZiZjRi&authkey=CP2ujcoM&hl=en

I am receiving a NullPointer on line 53: regularNumbers.enqueue(i);
But, I am not sure why. Additionally, I need to display my results 12 primes per line, and a space between each. Suggestions? Thanks all for taking the time to read my post.

Here is my code:

import java.util.*;

public class Sieve {
	
	private Queue<Integer> regularNumbers;// = new LinkedQueue<Integer>();
	private Queue<Integer> primeNumbers;// = new LinkedQueue<Integer>();
	private boolean hasResults = false;
	private Integer firstNumber;
	private int max;
	private int count = 1;
	
	/*
	 * Default Sieve is created in this constructor.
	 */
	public Sieve(){
		
		Queue<Integer> regularNumbers = new LinkedQueue<Integer>();
		Queue<Integer> primeNumbers = new LinkedQueue<Integer>();
		//primeNumbers = null;
		//regularNumbers = null;
		
	}
	
	/*
	 * This method uses the "Sieve of Eratosthenes" algorithm to compute a list
	 * of prime numbers ranging from 2 to the user defined maximum n.
	 */
	
	public void computeTo(int n)
	{
		/*
		 * This boolean is used to see whether or not the computeTo method has
		 * been called and will be used in future methods for IllegalStateExceptions.
		 */
		
		hasResults = true;
		max = n;
		
		/* 
		 * Illegal argument exception is thrown when the integer is less than 2.
		 */
		
		if ( n < 2){
			throw new IllegalArgumentException("Must be 2 or greater");
		}
		
		/*
		 * This for loop is used to add all numbers ranging from 2 to its max (n),
		 * which will be provided by the user to the queue "regularNumbers"
		 */
		
		for(int i = 2; i < n; i++ ){
			regularNumbers.enqueue(i);
			}
		
		
		do {
			
			Queue<Integer> tempQueue = new LinkedQueue<Integer>();
			Iterator<Integer> iterator = regularNumbers.iterator();
			
			firstNumber = regularNumbers.dequeue();
			
			if(firstNumber != null){
				primeNumbers.enqueue(firstNumber);
				//while(regularNumbers.peek() != null){
				for(int i = 0; i < regularNumbers.size(); i++){
					Integer secondNumber = regularNumbers.dequeue();
					if(secondNumber % firstNumber != 0){
						tempQueue.enqueue(secondNumber);
					}
				}
				//primeNumbers = regularNumbers;
				regularNumbers = tempQueue;
			}
		}
		while(firstNumber < Math.sqrt(n));
	}
	
	public void reportResults(){
		
		if(!hasResults){
			throw new IllegalStateException("Primes need to be computed before calling this method.");
		}
		System.out.print(primeNumbers);
	}
	
	public int getMax(){
		if( !hasResults ){
			throw new IllegalStateException("Primes need to be computed before calling this method.");
		}
		return max;
	}
	
	public int getCount(){
		if( !hasResults ){
			throw new IllegalStateException("Primes need to be computed before calling this method.");
		}
		return count;
	}
	
}

Recommended Answers

All 11 Replies

LinkedQueue is not within the Queue class heirarchy.

When I tried posting your code on my JAVA IDE, it can't create an object of it.
Do you have your own LinkedQueue class?

Sorry i didnt see the package zip

Yes, I attached the code on the second link.

Additionally, I added the following code where my null pointer was, but it did no good:

for(int i = 2; i < n; i++ ){
			Integer intObj = new Integer(i);
			regularNumbers.enqueue(intObj);
			}

Hmmm, I've never really done anything like this before but I did some testing and from what it looks like.

regularNumbers.enqueue(i);

It doesn't even go to the LinkedQueue class to add that input into the List in the LinkedQueue class.

public void enqueue(E value) {
        System.out.println("it went here");
        elements.addLast(value);
    }

Which I assume is why you are getting the null pointer exception.

Also, since Queue is an interface, and because regularNumber is of type Queue, can that work?
I'm thinking because the Queue class is an interface and doesn't actually have any implementations of the method enqueue, can you actually go to a class that implements that "enqueue" method?

Hey I found out why.
If you look at your default constructor,

private Queue<Integer> regularNumbers;// = new LinkedQueue<Integer>();
	private Queue<Integer> primeNumbers;// = new LinkedQueue<Integer>();

public Sieve(){

		Queue<Integer> regularNumbers = new LinkedQueue<Integer>();
		Queue<Integer> primeNumbers = new LinkedQueue<Integer>();
		//primeNumbers = null;
		//regularNumbers = null;

	}

your declaring the data type twice. So, when you go

s.reportResults();

it calls upon the global variable which a variable that doesn't refer to anything

Solution is this:

public Sieve(){

		regularNumbers = new LinkedQueue<Integer>();
		primeNumbers = new LinkedQueue<Integer>();
		//primeNumbers = null;
		//regularNumbers = null;

I dont understand. Doesnt this code from LinkedQueue allow me to do that:

// post: given value inserted at the end of the queue
    public void enqueue(E value) {
        elements.addLast(value);
    }

If not, could you explain why not? Also, how would you recommend I do that? I still need to count the prime numbers, and print the primes 12 per row, a space in between each. Everything seems to be on hold because I cant test lol.

My theory was not correct on my first try. However, I did find why it was giving a null exception error
If you look at your default constructor,

private Queue<Integer> regularNumbers;// = new LinkedQueue<Integer>();
	private Queue<Integer> primeNumbers;// = new LinkedQueue<Integer>();

public Sieve(){

		Queue<Integer> regularNumbers = new LinkedQueue<Integer>();
		Queue<Integer> primeNumbers = new LinkedQueue<Integer>();
		//primeNumbers = null;
		//regularNumbers = null;

	}

your declaring the data type twice. So, when you go

s.reportResults();

it calls upon the global variable which a variable that doesn't refer to anything

Solution is this:

public Sieve(){

		regularNumbers = new LinkedQueue<Integer>();
		primeNumbers = new LinkedQueue<Integer>();
		//primeNumbers = null;
		//regularNumbers = null;

Im confused. Like this? (Commented out). If so, this isnt working.

import java.util.*;

public class Sieve {
	
	//private Queue<Integer> regularNumbers;// = new LinkedQueue<Integer>();
	//private Queue<Integer> primeNumbers;// = new LinkedQueue<Integer>();
	private boolean hasResults = false;
	private Integer firstNumber;
	private int max;
	private int count = 1;
	
	/*
	 * Default Sieve is created in this constructor.
	 */
	public Sieve(){
		
		private Queue<Integer> regularNumbers = new LinkedQueue<Integer>();
		private Queue<Integer> primeNumbers = new LinkedQueue<Integer>();

No, you need those declarations to be global because your using those variables throughout many methods of that class.

Uncomment line 5 & 6.
take out the

private Queue<Integer> //for line 17 & 18 of the code above
commented: Good insight! +1

Ah thank you very much! Now I can at least play around with it. Do you have any other suggestions for me? I'm sure if you saw it, you will see it doesnt work very well lol :)

To be honest, I have no idea what the "Sieve of Eratosthenes" is. (Not in Comp Sci)

I was looking at the methods in LinkedQueue class and I think the displaying isn't as tricky as it sounds.

If you look at your Sieve class at the method "reportResults()", if you print
the variable "PrimeNumbers" it outputs this.

"front [some values stored in the list] back". That is ALL the elements in the List.

to display 7 digits per row separated by spaces, you can either manipulate the string output or you can go

PrimeNumbers.dequeue() // to remove the FIRST on the list.

I'll let you figure it out. Please read my signature at the bottom as a small favor. Thanks

Hmmm.. nothing im doing seems to be working... Im trying to look at it a different way... new perspective..

Try something like this:

public void reportResults(){

		if(!hasResults){
			throw new IllegalStateException("Primes need to be computed before calling this method.");
		}

                int counter = 0;
                String output = "";
                //keep looping until everything is displayed
                for (int i = 0; i <= primeNumbers.size(); i++){
                    
                    
                    if (counter <= 7 ){
                        /*if counter is less than 7 then keep adding the .dequeue to the same line.*/
                    output += String.format("%s ", primeNumbers.dequeue()); //output = output +primeNumber.dequeue();
                    }
                     else{

                        // if counter is greater than 7 then create a new line and repeat the process
                        output += String.format("%s \n", primeNumbers.dequeue());
                        counter = 0; //reset counter to 0
                     }
                    
                     //System.out.println(output);
                    counter ++;
                }
                System.out.println(output);
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.