Beginner/Intermediate Java Code - NullPointerException and wrong output.
First off, this IS a homework assignment I have been working on.
I believe I'm close to finishing but can't seem to figure out this small bit.
The input:
Quote:
buy 100 share(s) at $20 each;buy 20 share(s) at $24 each;buy 200 share(s) at $36 each;sell 150 share(s) at $30 each;buy 50 share(s) at $25 each;sell 200 share(s) at $35 each;
The output should be $1070 (using FIFO protocol).
I can see the problem being something to do with my sell() method when trying to perform Stocklist.peek()...
I've tried different variations of code and seem to be stuck..
Any hints/help would be greatly appreciated.
Code :
import java.util.*;
import java.text.*;
public class Stocks {
private int shares;
private int price;
private int temp;
private int finalPrice;
private int finalShares;
private static int total;
private Queue<Stocks> StockList = new LinkedList<Stocks>();
private static NumberFormat nf = NumberFormat.getCurrencyInstance();
public Stocks()
{
shares = 0;
price = 0;
}
public Stocks(int shares, int price)
{
this.shares = shares;
this.price = price;
}
public int getShares()
{
return this.shares;
}
public int getPrice()
{
return this.price;
}
public void setShares(int shares)
{
this.shares = shares;
}
public void setPrice(int price)
{
this.price = price;
}
public void sell() {
int sharesToSell = this.getShares();
int priceToSell = this.getPrice();
while (!StockList.isEmpty() == false) { // **Something wrong starting here.
int numShares = StockList.peek().getShares(); // **Throws NullPointerException
int sharePrice = StockList.peek().getPrice();
while (numShares < sharesToSell || numShares == sharesToSell) {
temp = sharesToSell - numShares; // Remaining shares to sell
finalShares = sharesToSell - temp; // # Shares selling at price
finalPrice = priceToSell - sharePrice; // Shares sold at adjusted price
total += (finalPrice * finalShares); // Calculates total
System.out.println(total);
} sharesToSell = temp;
if (numShares > sharesToSell) {
temp = numShares - sharesToSell; // Remaining shares that were bought
finalPrice = priceToSell - sharePrice; // Shares sold at adjusted price
total += (finalPrice * sharesToSell); // Adds to running total
StockList.peek().setShares(temp);
}
}
}
public void buy() {
int numShares = this.getShares();
int priceToBuy = this.getPrice();
Stocks newStock = new Stocks(numShares,priceToBuy);
StockList.add(newStock); // adds stock to list
total += (numShares * priceToBuy);
}
public static int getTotal() { // gets total profit (or loss)
return total;
}
// *****MAIN METHOD*****
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter transaction sequence:");
String input = scan.nextLine().trim();
String[] inputArray = new String[50];
String[] inputArray2 = new String[50];
String[] inputArray3 = new String[50];
int numShares, sharePrice;
inputArray = input.split(";"); // Splits input at ";"
// and enters into inputArray
for (String i : inputArray) { // Checks for BUY or SELL
if (i.toUpperCase().contains("BUY")) {
inputArray2 = i.split(" ");
inputArray2[4] = inputArray2[4].substring(1); // Removes $ sign
try {
numShares = Integer.parseInt(inputArray2[1]);
sharePrice = Integer.parseInt(inputArray2[4]);
Stocks newStock = new Stocks(numShares,sharePrice);
newStock.buy();
} catch (NumberFormatException e) {
System.out.println("Number Format Error");
return;
}
}
else if (i.toUpperCase().contains("SELL")) {
inputArray3 = i.split(" ");
inputArray3[4] = inputArray3[4].substring(1);
try {
numShares = Integer.parseInt(inputArray3[1]);
sharePrice = Integer.parseInt(inputArray3[4]);
Stocks newStock = new Stocks(numShares,sharePrice);
newStock.sell(); // **Must be problem with sell() method.
} catch (NumberFormatException e) {
System.out.println("Number Format Error");
return;
}
} else {
System.out.println("An error has been found.");
}
}
System.out.println();
System.out.println("Total:"+nf.format(getTotal()));
}
}
Re: Beginner/Intermediate Java Code - NullPointerException and wrong output.
Code :
int numShares = StockList.peek().getShares();
Most probably, this StockList.peek() is returning a null and you want a null to call getShares(). That is actually the problem. Good Luck
Re: Beginner/Intermediate Java Code - NullPointerException and wrong output.
I think what I'm not understanding is why the the StockList.peek() returns null?
Shouldn't the list contain Stocks from the buy() method?
Re: Beginner/Intermediate Java Code - NullPointerException and wrong output.
Quote:
Originally Posted by
tt52
I think what I'm not understanding is why the the StockList.peek() returns null?
Shouldn't the list contain Stocks from the buy() method?
Did you try debugging your program?
If you don't know debugging, place print statements to find the value of your queue(list) at different locations.