gpt4 book ai didi

java - 抛出 NullPointerException 并且程序未按预期终止

转载 作者:行者123 更新时间:2023-12-02 07:33:47 25 4
gpt4 key购买 nike

我正在创建一个证券交易所类型的程序,到目前为止,我已经让输入正常工作,以便它可以正确接收用户的命令。然而,它对输入的作用并没有按预期工作。我困惑的第一件事是为什么当我运行代码时它会抛出 NullPointerException。

基本上,程序将接受 3 个内容的输入,后面是它们之间的空格。例如,我想以每股 20 美元的价格购买 30 股,我会输入如下内容:

b 30 20

然后它会将其分成 3 部分并将其存储到一个数组中。之后,它将比较数组的第一个索引以查看程序应该执行的操作,在本例中它将购买,因此它将调用 buy 方法并将份额金额和份额值存储到我的 CircularArrayQueue 中。

它获取存储到节点中的份额值和份额数量,但是当我尝试使用 CirularArrayQueue 调用排队方法以将节点存储到队列中时,它给了我一个 NullPointerException。

我遇到的另一个问题是程序终止。当程序发现输入的第一个索引值为“q”时,程序应该终止。我做了一个 while 循环,声明当 boolean 退出为 false 时它将循环。然后在 while 循环中,我做了一个 if 语句检查 stockParts[0] 的值是否为“q”。如果是这样,它将把 quit 的值更改为 true,以便它可以结束循环,但由于某种原因它不会终止并且仍在循环。

我已经在这些问题上摸索了几个小时了,但我似乎找不到问题的根源。有人可以帮我解决这个问题吗?以下是我的主类和 CircularArrayQueue 类的代码:

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
}
}

最佳答案

您尚未初始化引用 Q。由于它是一个字段变量,因此默认初始化为 null。

    CircleArrayQueue Q;

当你遇到这样的问题时,你必须调试它。信息来源之一是异常的堆栈跟踪,它会告诉您异常是在哪里引发的。您还可以要求开发环境中的调试器在引发异常时自动停止。

其次,当您在 Java 中比较字符串时,请使用 equals() 方法而不是 == 运算符。 equals() 方法比较对象值。 == 运算符比较指向对象的引用的值。您可以拥有两个具有不同引用值的相等对象。

关于java - 抛出 NullPointerException 并且程序未按预期终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12552621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com