Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:22:43+00:00 2026-06-11T17:22:43+00:00

I am creating a stock exchange type program and so far I’ve got the

  • 0

I am creating a stock exchange type program and so far I’ve got the input working so that it takes the command from the user properly. However what it does to the input isn’t working as expected. The first thing I am confused on is why it is throwing me a NullPointerException when I run the code.

Basically the program will take an input of 3 things followed by whitespace between them. So for instance I want to buy 30 shares at $20 each, I would type the input like the following:

b 30 20

It would then split it into 3 parts and store it into an array. After that it will compare the first index of the array to see what the program should do, in this example it will buy so it will invoke the buy method and store the share amount and share value into my CircularArrayQueue.

It gets the share value and share amount stored into a Node, but when I try to invoke the enqueue method with my CirularArrayQueue to store the Node into the Queue, it gives me a NullPointerException.

Another issue I’ve been running into was the termination for the program. The program is supposed to terminate when it sees that the first index value of the input is “q”. I’ve made a while loop stating that it will loop when the boolean quit is false. Then within the while loop I’ve made an if statement checking to see if the value of stockParts[0] is “q”. If so it would change the value of quit to be true so it can end the loop, but for some reason it isn’t terminating and it is still looping.

I’ve been scratching my head on these issues for a couple hours but I cannot seem to find the root of the problem. Could someone please assist me on this? The following is the code from my main class and the CircularArrayQueue class:

import java.util.Scanner;
import java.lang.Integer;

public class StockTran {
String command = "";
String[] stockParts = null;
CircleArrayQueue Q;
boolean quit = false;

public StockTran(String inputCommand) {
    try {
        Scanner conReader = new Scanner(System.in);
        this.command = inputCommand.toLowerCase();
        this.stockParts = command.split("\\s"); // splits the input into three parts

        buyShares(Integer.parseInt(stockParts[1]), Integer.parseInt(stockParts[2]));        //testing purpose only

        while (quit == false) {
            if (this.stockParts[0] == "q") {        // ends transaction and terminates program
                System.out.println("Share trading successfully cancelled.");
                quit = true;    
            }

            if (this.stockParts == null || this.stockParts.length > 3) {
                throw new Exception("Bad input.");
            }

            if (stockParts[0] == "b") {     // checks to see if it is a buying of shares
                int shares = Integer.parseInt(stockParts[1]);       // stores share amount
                int value = Integer.parseInt(stockParts[2]);        // stores selling value
                buyShares(shares, value);       // calls buyShares method and adds share to queue
            }
            else if (stockParts[0] == "s") {        // checks to see if it is a selling of shares
                int shares = Integer.parseInt(stockParts[1]);       // stores share amount
                int value = Integer.parseInt(stockParts[2]);        // stores selling value
                sellShares(shares, value);      // calls sellShares method
            }
            else if (stockParts[0] == "c") {        // checks to see if it is capital gain
                capitalGain();      // calls capitalGain and calculates net gain
            }
            System.out.println("Enter your next command or press 'q' to quit: ");
            command = conReader.nextLine().toLowerCase();
            stockParts = command.split("\\s");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}


public void buyShares(int shareAmout, int shareValue) {     // takes in share total and values for each share
    Node temp = new Node(shareAmout, shareValue);       // stores values into node
    try {
        Q.enqueue(temp);        // enqueues the node into the CircularArrayQueue
        //System.out.println(Q.toString());

    } catch (FullQueueException e) {
        e.printStackTrace();
    }

}

public void sellShares(int shareAmount, int sharePrice) {   // ToDo

}

public int capitalGain() {  // ToDo
    return 0;
}

public static void main(String[] args) {
    String inputCommand = "";
    Scanner mainReader = new Scanner(System.in);

    System.out.println("Enter 'b' to purchase share, 's' to sell share, 'c' for capital gain, or 'Q' to quit: ");
    inputCommand = mainReader.nextLine();

    StockTran tran = new StockTran(inputCommand);
}
}
public class CircleArrayQueue implements Queue {
protected Node Q[];     // initializes an empty array for any element type
private int MAX_CAP = 0;        // initializes the value for the maximum array capacity
private int f, r;

public CircleArrayQueue(int maxCap) {
    MAX_CAP = maxCap;
    Q = new Node[MAX_CAP];  // sets Q to be a specific maximum size specified
    f = 0;      // sets front value to be 0
    r = 0;      // sets rear value to be 0;
}

public int size() {
    return (MAX_CAP - f + r) % MAX_CAP;     // returns the size of the CircularArrayQueue
}

public boolean isEmpty() {      // if front and rear are of equal value, Queue is empty
    return f == r;
}

public Node front() throws EmptyQueueException {        // method to get the front value of the CircularArrayQueue
    if (isEmpty()) throw new EmptyQueueException("Queue is empty.");
        return Q[f];        // returns object at front of CircularArrayQueue
}

public Node dequeue() throws EmptyQueueException {  // method to remove from the front of the CircularArrayQueue
    if (isEmpty()) throw new EmptyQueueException("Queue is empty.");
        Node temp = Q[f];       // stores front object in local variable
        Q[f] = null;        // sets the value to be null in the array
        f = (f + 1) % MAX_CAP;      // sets the new front value to be this
        return temp;        // returns the object that was originally in the front
}

public void enqueue(Node element) throws FullQueueException {       // method to add to the end of the CircualarArrayQueue
    if (size() == MAX_CAP - 1) throw new FullQueueException("Queue has reached maximum capacity.");
        Q[r] = element;     // stores the new element at the rear of array
        r = (r + 1) % MAX_CAP;      // sets the new rear value to be the location after element insertion
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:22:44+00:00Added an answer on June 11, 2026 at 5:22 pm

    You haven’t initialized the reference Q. Since it’s a field variable, it’s initialized by default to null.

        CircleArrayQueue Q;
    

    When you’re faced with a problem like this, you have to debug it. One source of information is the stack trace from the exception, which will tell you where the exception was thrown. You may also be able to ask the debugger in your development environment to stop automatically at the point an exception is thrown.

    Secondly, when you compare strings in Java, use the equals() method rather than the == operator. The equals() method compares object values. The == operator compares the values of the references that point to the objects. You can have two equal objects with different reference values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a project that simulates Stock Exchange Market. I am exposing stock
I'm currently creating a stock management system that uses multiple warehouses (with sub locations)
I've been creating rpg game and now I'm doing stock-market for my players that
I'm working on creating a binary search algorithm in C that searches for a
I'm working on a project that involves creating EXTRA tables in the same DB
Creating a website using simple html(not html5) and css but got stock in mouseover
Through a java program I am creating a xml of stock holders. The generated
I am creating a linked list for a class project that stores some stock
I'm trying out the HighStock library for creating stock charts. To fill the chart
I was looking at stockcharts - http://code.google.com/p/stock-chart for creating candlestickgraphs in my application. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.